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

public class BookedObject : IBookable
{
    protected Dictionary<int, List<OfferTime>> bookedTimes = new Dictionary<int, List<OfferTime>>();

    public BookedObject()
    {
        Calendar.Instance.SubscribeOnWeekEnded(WeekEnded);
    }

    //to test!!!!
    public virtual bool CanBook(DateTime start, DateTime endTime)
    {
        foreach (var weekData in bookedTimes)
        {
            foreach(var offerTime in weekData.Value)
                if (offerTime.startTime >= start && offerTime.endTime <= endTime)
                    return false;
        }

        return true;
    }

    public virtual void Book(DateTime start, DateTime end)
    {
        int weekNumber = Calendar.Instance.WeekCountToTime(start);

        if (!bookedTimes.ContainsKey(weekNumber))
            bookedTimes.Add(weekNumber, new List<OfferTime>());

        bookedTimes[weekNumber].Add(new OfferTime { startTime = start, endTime = end });
    }

    public virtual void UnBook(DateTime start)
    {
        int weekNumber = Calendar.Instance.WeekCountToTime(start);

        if(bookedTimes.ContainsKey(weekNumber))
            bookedTimes[weekNumber].RemoveAll(x => x.startTime == start);
    }

    public void WeekEnded(int week) => bookedTimes.Remove(week);

    //public void OnDestroy()
    //{
    //    Calendar.Instance.UnSubscribeOnWeekEnded(WeekEnded);
    //}
}
