/*
 * Created by JacobKay - 2022.08.24
 */
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ZTools
{
    public class ZCalendarController
    {
        public int Year { set; get; }
        public int Month { set; get; }
        public int Day { set; get; }
        /// 
        /// 当前是否在区间选择状态
        /// 
        private bool isInRange = false;
        public bool IsInRange { get { return isInRange; } }
        private string week;
        private DateTime now;
        private int days;
        /// 
        /// 当前选中的位置
        /// 
        public Vector3 pos;
        private int lastMonthDays;
        private int nextMonthDays;
        public ZCalendar zCalendar;
        public ZCalendarModel zCalendarModel;
        public DateTime nowTime = DateTime.Today;
        private int lastMonthEmptyDays;
        bool isShow = true;
        public bool isInit = false;
        /// 
        /// 保存文字颜色
        /// 
        public Color greyColor;
        public System.Globalization.ChineseLunisolarCalendar cncld = new System.Globalization.ChineseLunisolarCalendar();
        /// 
        /// 农历月
        /// 
        public string[] lunarMonths = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "腊" };
        public string[] lunarDaysT = { "初", "十", "廿", "三" };
        /// 
        /// 农历日
        /// 
        public string[] lunarDays = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
        DateTime monthFirstDay;
        /// 
        /// 初始化
        /// 
        /// 
        public void Init()
        {
            zCalendarModel.zCalendarController = this;
            zCalendarModel.Init();
            if (zCalendarModel.isStaticCalendar) return;
            // 动态日历,可关闭
            if (zCalendarModel.isPopupCalendar)
            {
                zCalendarModel.btnClose.onClick.AddListener(() =>
                {
                    Hide();
                });
            }
            zCalendarModel.btnLastYear.onClick.AddListener(LastYear);
            zCalendarModel.btnNextYear.onClick.AddListener(NextYear);
            zCalendarModel.btnLastMonth.onClick.AddListener(LastMonth);
            zCalendarModel.btnNextMonth.onClick.AddListener(NextMonth);
        }
        /// 
        /// 按照规定时间初始化日历
        /// 
        public void InitDate(DateTime date)
        {
            now = date;
            DestroyAllChildren();
            UpdateYear();
            UpdateMonth();
            UpdateDays();
            UpdateData();
            if (!isInit)
            {
                isInit = true;
                zCalendar.DateComplete();
            }
        }
        void LastYear()
        {
            now = now.AddYears(-1);
            DestroyAllChildren();
            UpdateYear();
            UpdateMonth();
            UpdateDays();
            UpdateData();
        }
        void NextYear()
        {
            now = now.AddYears(1);
            DestroyAllChildren();
            UpdateYear();
            UpdateMonth();
            UpdateDays();
            UpdateData();
        }
        void LastMonth()
        {
            now = now.AddMonths(-1);
            DestroyAllChildren();
            UpdateYear();
            UpdateMonth();
            UpdateDays();
            UpdateData();
        }
        void NextMonth()
        {
            now = now.AddMonths(1);
            DestroyAllChildren();
            UpdateYear();
            UpdateMonth();
            UpdateDays();
            UpdateData();
        }
        List dayItemList = new List();
        /// 
        /// 如果是区间日历,选择时间时,需要判断当前日期选择状态
        /// 
        /// 
        public void ChangeRangeType(ZCalendarDayItem dayItem)
        {
            isInRange = !isInRange;
            if (dayItemList.Count >= 2)
            {
                dayItemList.Clear();
            }
            if (dayItemList.Count == 0)
            {
                dayItemList.Add(dayItem);
            }
            else
            {
                int compare = DateTime.Compare(dayItemList[0].dateTime, dayItem.dateTime);
                if (compare <= 0)
                {
                    dayItemList.Add(dayItem);
                }
                else
                {
                    dayItemList.Insert(0, dayItem);
                }
            }
            if (!isInRange)
            {
                zCalendar.RangeCalendar(dayItemList[0], dayItemList[1]);
            }
        }
        /// 
        /// 显示日历
        /// 
        public void Show()
        {
            if (pos != null && !isShow)
            {
                Debug.Log("劲来了");
                isShow = true;
                zCalendar.transform.localPosition = pos;
            }
        }
        /// 
        /// 隐藏日历
        /// 
        public void Hide()
        {
            if (isShow && !isInRange)
            {
                //isShow = false;
                Debug.Log("hide");
                zCalendar.transform.gameObject.SetActive(false);
                //zCalendar.transform.localPosition = new Vector3(pos.x, 5000, pos.z);
            }
        }
        /// 
        /// 查询年数据
        /// 
        void UpdateYear()
        {
            Year = now.Year;
        }
        /// 
        /// 查询月数据
        /// 
        void UpdateMonth()
        {
            Month = int.Parse(now.Month.ToString("00"));
        }
        /// 
        /// 返回要查询那天
        /// 
        /// 
        void UpdateDays()
        {
            days = DateTime.DaysInMonth(Year, Month);
            if (Day == 0)
            {
                Day = now.Day;
            }
            else if (Day > days)
            {
                Day = days;
            }
        }
        /// 
        /// 更新显示月份
        /// 
        void UpdateData()
        {
            zCalendarModel.SetTimeTxt(Year, Month);
            FillLastMonth();
            for (int i = 0; i < days; i++)
            {
                AddDayItem(monthFirstDay.AddDays(i));
            }
            FillNextMonth();
        }
        /// 
        /// 自动填充上个月内容
        /// 
        void FillLastMonth()
        {
            monthFirstDay = new DateTime(Year, Month, 1);
            lastMonthEmptyDays = GetLastMonthDays();
            if (zCalendarModel.autoFillDate)
            {
                for (int i = lastMonthEmptyDays; i > 0; i--)
                {
                    AddDayItem(monthFirstDay.AddDays(-i));
                }
            }
            else
            {
                for (int i = 0; i < lastMonthEmptyDays; i++)
                {
                    ZCalendarDayItem dayItem = zCalendarModel.Instantiate();
                    dayItem.zCalendarController = this;
                    dayItem.CloseClickAble();
                }
            }
        }
        /// 
        /// 添加下个月的时间
        /// 
        void FillNextMonth()
        {
            int nextMonthDays = 42 - (lastMonthEmptyDays + days);
            if (nextMonthDays != 0)
            {
                if (zCalendarModel.autoFillDate)
                {
                    DateTime lastDay = monthFirstDay.AddDays(days);
                    for (int i = 0; i < nextMonthDays; i++)
                    {
                        AddDayItem(lastDay.AddDays(i));
                    }
                }
            }
        }
        /// 
        /// 添加日期对象
        /// 
        void AddDayItem(DateTime dateTime)
        {
            ZCalendarDayItem dayItem = zCalendarModel.Instantiate();
            dayItem.zCalendarController = this;
            dayItem.Init(dateTime, nowTime);
            zCalendar.UpdateDate(dayItem);
            if (!isInRange && dayItemList.Count > 0)
            {
                dayItem.IsRangeDayItem(dayItemList[0], dayItemList[1]);
            }
        }
        /// 
        /// 判断上一个月有几天
        /// 
        /// 
        int GetLastMonthDays()
        {
            string firstWeek = new DateTime(Year, Month, 1).DayOfWeek.ToString();
            return (int)Enum.Parse(typeof(DayOfWeek), firstWeek);
        }
        /// 
        /// 删除所有内容
        /// 
        void DestroyAllChildren()
        {
            List lst = new List();
            int count = zCalendarModel.dayContent.childCount;
            for (int i = 0; i < count; i++)
            {
                Transform child = zCalendarModel.dayContent.GetChild(i);
                lst.Add(child);
            }
            for (int i = 0; i < lst.Count; i++)
            {
                MonoBehaviour.Destroy(lst[i].gameObject);
            }
        }
    }
}