using Managers;
using Offers;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace UI.EmailWindow
{
    public class OppenedMessageController : MonoBehaviour
    {
        [SerializeField] Button rejectButton;
        [SerializeField] Button acceptButton;

        [SerializeField] Image clientIcon;
        [SerializeField] TextMeshProUGUI clientName;
        [SerializeField] TextMeshProUGUI offerCreatedTime;
        [SerializeField] TextMeshProUGUI studioName;
        [SerializeField] TextMeshProUGUI messageText;
        [SerializeField] TextMeshProUGUI animatorsCount;
        [SerializeField] TextMeshProUGUI eventDate;
        [SerializeField] TextMeshProUGUI eventDuration;
        [SerializeField] TextMeshProUGUI rewardText;

        Offer oppenedOffer;

        public void SetData(Offer offer)
        {
            if (oppenedOffer != null)
                oppenedOffer.UnSubscribeOnCompleted(BlockActions);

            gameObject.SetActive(true);

            oppenedOffer = offer;

            oppenedOffer.SubscribeOnOutdated(BlockActions);

            rejectButton.interactable = offer.OfferState == OfferState.Created;
            acceptButton.interactable = GameManager.Instance.CanAcceptOffer(offer);

            int eventDurationValue = (int)(offer.EndTime - offer.StartTime).TotalMinutes;

            clientIcon.sprite = offer.CustomerIcon;
            clientName.text = offer.ClientName;
            offerCreatedTime.text = NumbersFormatter.FormatNumber(offer.OfferCreatedTime.Hour) + ":" 
                                    + NumbersFormatter.FormatNumber(offer.OfferCreatedTime.Minute);
            studioName.text = DataManager.Instance.AgencyName;
            messageText.text = offer.Description;
            animatorsCount.text = offer.AnimatorsCount.ToString();
            eventDate.text = NumbersFormatter.FormatNumber(offer.StartTime.Day) + "." 
                        + NumbersFormatter.FormatNumber(offer.StartTime.Month) + "." 
                        + NumbersFormatter.FormatNumber(offer.StartTime.Year);
            eventDuration.text = NumbersFormatter.FormatNumber(eventDurationValue / 60) + "h "
                + NumbersFormatter.FormatNumber(eventDurationValue % 60) + "m";
            rewardText.text = offer.Reward.ToString() + "$";
        }

        void BlockActions()
        {
            rejectButton.interactable = false;
            acceptButton.interactable = false;
        }

        public void RejectOffer()
        {
            GameManager.Instance.RejectOffer(oppenedOffer);

            BlockActions();
            Sound.SoundController.Instance.Play(SoundType.UIClick);
        }

        public void AcceptOffer()
        {
            GameManager.Instance.AcceptOffer(oppenedOffer);

            BlockActions();
            Sound.SoundController.Instance.Play(SoundType.UIClick);
        }
    }
}