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

namespace Managers
{
    public class OffersManager : MonoBehaviour
    {
        [SerializeField] private IconConfig customersIcons;

        protected List<Offer> offers = new List<Offer>();

        List<Offer> acceptedOffers = new List<Offer>();
        List<Offer> createdOffers = new List<Offer>();
        List<Offer> completedOffers = new List<Offer>();

        DataManager dataManager;

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

            dataManager.SubscribeOnLoad(delegate
            {
                var allOffers = dataManager.Offers;

                // generate offers
                foreach (var offerData in allOffers)
                {
                    var offer = new Offer();
                    offer.SetData(offerData, customersIcons.GetSpriteByID(offerData.customerIconID));
                    offers.Add(offer);
                }

                acceptedOffers = offers.FindAll(x => x.IsAccepted());
                createdOffers = offers.FindAll(x => x.OfferState == OfferState.Created);
                completedOffers = offers.FindAll(x => x.OfferState == OfferState.Completed);

                createdOffers.ForEach(offer => offer.SubscribeOnOutdated(() => RemoveOffer(offer)));
                acceptedOffers.ForEach(offer => offer.SubscribeOnCompleted(() => CompleteOffer(offer)));
            });
        }

        public void AcceptOffer(Offer offer)
        {
            offer.AcceptOffer();
            acceptedOffers.Add(offer);
            createdOffers.Remove(offer);
            offer.SubscribeOnCompleted(() => CompleteOffer(offer));
        }

        public void RejectOffer(Offer offer)
        {
            offer.Reject();
            RemoveOffer(offer);
            createdOffers.Remove(offer);
        }

        public void CompleteOffer(Offer offer)
        {
            acceptedOffers.Remove(offer);
            completedOffers.Add(offer);
            GameManager.Instance.SendOrderNotification(offer);
            DataManager.Instance.Budget += offer.Reward;
            DataManager.Instance.EarnedMoney += offer.Reward;
        }

        public void AddNewOffer(Offer offer)
        {
            offers.Add(offer);
            createdOffers.Add(offer);
            dataManager.Offers.Add(offer.GetData());

            offer.SubscribeOnOutdated(() => RemoveOffer(offer));
        }

        void RemoveOffer(Offer offer)
        {
            offers.Remove(offer);
            dataManager.Offers.Remove(offer.GetData());
            createdOffers.Remove(offer);
        }

        public void CloseOffer(Offer offer)
        {
            RemoveOffer(offer);
            completedOffers.Remove(offer);
        }

        public void CancelOffer(Offer offer)
        {
            offer.CancelOffer();
            RemoveOffer(offer);
            acceptedOffers.Remove(offer);
        }

        public List<Offer> GetCreatedOffers() => createdOffers;

        public List<Offer> GetAcceptedOffers() => acceptedOffers;

        public List<Offer> GetCompletedOffers() => completedOffers;
    }
}