using Managers;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Offers
{
    public class Offer
    {
        protected OfferData _data;
        //protected Review _review;

        protected Sprite customerIcon;

        protected UnityEvent onCompleted = new UnityEvent();
        protected UnityEvent onOutdated = new UnityEvent();

        public int AnimatorsCount => _data.animatorsCount;

        public DateTime StartTime => _data.startTime;

        public DateTime EndTime => _data.endTime;

        public Sprite CustomerIcon => customerIcon;

        public string ClientName => _data.customerName;

        public string Description => _data.description;

        public DateTime OfferCreatedTime => _data.offerCreatedTime;

        public int Reward => _data.reward;

        public int OfferNumber => _data.offerNumber;

        public OfferState OfferState => _data.state;

        public Offer()
        {
            OfferCreated();
        }

        public void SetData(OfferData data, Sprite icon)
        {
            _data = data;
            customerIcon = icon;
        }

        public OfferData GetData() => _data;

        public bool IsAccepted() => _data.state == OfferState.Active;

        public void OfferCreated() => Calendar.Instance.SubscribeOnTimer(CalendarTimeChanged);

        public void AcceptOffer()
        {
            _data.state = OfferState.Active;

            ////////////////////////////////////////////////////////////////////////
            //_data.startTime = Calendar.Instance.GetCurrentTime().AddMinutes(20);
            //_data.endTime = Calendar.Instance.GetCurrentTime().AddMinutes(30);
            ////////////////////////////////////////////////////////////////////////
        }

        public void Reject()
        {
            Calendar.Instance.UnSubscribeOnTimer(CalendarTimeChanged);
            _data.state = OfferState.Rejected;
        }

        public void CancelOffer() => Calendar.Instance.UnSubscribeOnTimer(CalendarTimeChanged);

        public void SubscribeOnCompleted(UnityAction callback) => onCompleted.AddListener(callback);

        public void UnSubscribeOnCompleted(UnityAction callback) => onCompleted.RemoveListener(callback);

        public void SubscribeOnOutdated(UnityAction callback) => onOutdated.AddListener(callback);

        public void UnSubscribeOnOutdated(UnityAction callback) => onOutdated.RemoveListener(callback);

        public void CalendarTimeChanged(DateTime dateTime)
        {
            if (_data == null)
            {
                Debug.LogError(" data is null");
                return;
            }

            if (_data.state == OfferState.Created && _data.outdatedTime <= dateTime)
            {
                Debug.Log("Outdated");
                _data.state = OfferState.Outdated;
                onOutdated.Invoke();

                return;
            }

            if (_data.state == OfferState.Active && _data.endTime <= dateTime)
            {
                Debug.Log("completed");
                _data.state = OfferState.Completed;
                onCompleted.Invoke();

                return;
            }
        }
    }
}