using Costumes;
using Offers;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Managers
{
    public class CostumesManager : MonoBehaviour
    {
        [SerializeField] private Costumes.Config.CostumeConfig costumeConfig;

        protected List<Costume> costumes = new List<Costume>();

        List<Costume> freeCostumes = new List<Costume>();

        DataManager dataManager;

        private void Start()
        {
            dataManager = DataManager.Instance;

            dataManager.SubscribeOnLoad(delegate
            {
                GenerateCostumesFromSave();
            });
        }

        public int GetCostumesCount() => costumes.Count;

        public void BookCostumes(int count, DateTime eventStart, DateTime eventEnd)
        {
            freeCostumes.Shuffle();

            for (int i = 0; i < count; i++)
                freeCostumes[i].Book(eventStart, eventEnd);
        }

        public bool IsFreeCostumes(int count, DateTime eventStart, DateTime eventEnd)
        {
            freeCostumes = new List<Costume>();

            foreach(var costume in costumes) 
                if(costume.CanBook(eventStart, eventEnd))
                    freeCostumes.Add(costume);

            return freeCostumes.Count >= count;
        }

        public CostumeData GenerateCostumeData(CostumeType type)
        {
            CostumeData data = new CostumeData();
            data.ID = Guid.NewGuid();
            data.Type = type;
            return data;
        }

        public List<CostumeData> GenerateCostumeDatas(List<CostumeStartData> startData)
        {
            List<CostumeData> costumes = new List<CostumeData>();

            foreach(var costumeStartData in startData)
                for (int i = 0; i < costumeStartData.amount; i++)
                    costumes.Add(GenerateCostumeData(costumeStartData.Type));

            return costumes;
        }

        public void UnBookCostume(Offer offer)
        {
            costumes.ForEach(x => x.UnBook(offer.StartTime));
        }
        
        public void BuyCostume(CostumeType type)
        {
            var data = costumeConfig.data.Find(x => x.type == type);

            dataManager.Budget -= data.buyPrice;

            var costume = new Costume();
            costume.SetData(GenerateCostumeData(type));

            costumes.Add(costume);

            dataManager.Costumes.Add(costume.Data);
        }

        public void SellCostume(CostumeType type)
        {
            var data = costumeConfig.data.Find(x => x.type == type);

            dataManager.Budget += data.sellPrice;

            var costumeToRemove = costumes.Find(x => x.Data.Type == type);
            dataManager.Costumes.Remove(costumeToRemove.Data);
            costumes.Remove(costumeToRemove);

            // reasign another costume to bookedTimes
        }

        public List<(CostumeType, int)> GetCostumeByType()
        {
            List<(CostumeType, int)> costumesByType = new List<(CostumeType, int)>();

            foreach (var type in Enum.GetValues(typeof(CostumeType)))
            {
                (CostumeType, int) data;
                data.Item1 = (CostumeType)type;
                data.Item2 = costumes.FindAll(x => x.Data.Type == (CostumeType)type).Count;
                costumesByType.Add(data);
            }

            return costumesByType;
        }

        public void GenerateCostumesFromSave()
        {
            costumes.Clear();

            var allCostumes = dataManager.Costumes;

            foreach (var costumeData in allCostumes)
            {
                var costume = new Costume();
                costume.SetData(costumeData);
                costumes.Add(costume);
            }
        }
    }
}