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

namespace Offers
{
    public class OfferSpawner : MonoBehaviour
    {
        [SerializeField] IconConfig clientsIcons;
        [SerializeField] OfferTimesConfig timesToAcceptOffer;
        [SerializeField] OfferTimesConfig eventTimes;
        [SerializeField] TextsConfig offersDescriptions;

        [SerializeField] int minOffersPerDay = 100;
        [SerializeField] int maxOffersPerDay = 250;

        private int offerID;

        List<DateTime> timesToSpawnOffers = new List<DateTime>();

        private void Start()
        {
            GenerateTimes();
            Calendar.Instance.SubscribeOnTimer(SpawnOfferAtTime);
            Calendar.Instance.SubscribeOnDayEnded(GenerateTimes);

            DataManager.Instance.SubscribeOnLoad(delegate
            {
                offerID = DataManager.Instance.OfferSpawnerID;
            });
        }

        void GenerateTimes()
        {
            timesToSpawnOffers.Clear();
            int timesCount = UnityEngine.Random.Range(minOffersPerDay, maxOffersPerDay);

            for (int i = 0; i < timesCount; i++)
                timesToSpawnOffers.Add(Calendar.Instance.GetRandomWorkTime());
        }

        void SpawnOfferAtTime(DateTime dateTime)
        {
            foreach (DateTime time in timesToSpawnOffers)
                if(time.Hour == dateTime.Hour && time.Minute == dateTime.Minute)
                {
                    CreateOffer();
                    timesToSpawnOffers.Remove(time);
                    break;
                }
        }

        [ContextMenu("CreateOffer")]
        public void CreateOffer()
        {
            var clientData = NamesGenerator.GenerateBasePeopleStats();
            var clientIcon = clientsIcons.GetRandomIcon(clientData.sex);

            var offerData = new OfferData();

            offerData.customerName = clientData.name + " " + clientData.surname;
            offerData.customerIconID = clientIcon.id;
            offerData.offerCreatedTime = Calendar.Instance.GetCurrentTime();
            offerData.outdatedTime = offerData.offerCreatedTime.AddMinutes(timesToAcceptOffer.durations[UnityEngine.Random.Range(0, timesToAcceptOffer.durations.Count)]);
            offerData.startTime = Calendar.Instance.GetRandomData();
            offerData.endTime = offerData.startTime.AddMinutes(eventTimes.durations[UnityEngine.Random.Range(0, eventTimes.durations.Count)]);
            offerData.description = offersDescriptions.texts[UnityEngine.Random.Range(0, offersDescriptions.texts.Count)];
            offerData.animatorsCount = 1;
            offerData.reward = CalculateOfferReward(offerData);
            offerData.offerNumber = offerID;

            offerID += 1;
            DataManager.Instance.OfferSpawnerID = offerID;

            var offer = new Offer();
            offer.SetData(offerData, clientIcon.icon);

            GameManager.Instance.AddNewOffer(offer);

            Debug.Log("offer created");
        }

        public int CalculateOfferReward(OfferData offer)
        {
            int baseOfferReward = UnityEngine.Random.Range(3, 10);
            int plannedCoefficient = Mathf.Clamp(100 - (int)(offer.startTime - Calendar.Instance.GetCurrentTime()).TotalHours, 1, 100);

            return offer.animatorsCount * baseOfferReward * (int) (offer.endTime - offer.startTime).TotalHours * plannedCoefficient;
        }
    }
}