using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ryCommon;

namespace TimeClock.Controls
{
    public partial class Rili : UserControl
    {
        private List<LunarDay> list_lunar = new List<LunarDay>();
        public Rili()
        {
            InitializeComponent();
            ryCommon.ListX list = new ListX(PnlDay)
            {
                MaxColCount = 7,
                spaceX = 1,
                spaceY = 1
            };
            int day_width = 0;
            for (int i = 0; i < 35; i++)
            {
                LunarDay lunarday = new LunarDay
                {
                    Day = i + 1,
                    Lunar_Day = (i + 1).ToString(),
                    //Size = new Size(120, 120)
                };
                lunarday.Click += Lunarday_Click;
                if (day_width == 0)
                {
                    day_width = lunarday.Width;
                }
                list_lunar.Add(lunarday);
                list.Add(lunarday);
            }
            list.ReDraw();
            for(int i=0;i<7;i++)
            {
                Label lbl_week = new Label
                {
                    AutoSize = false,
                    TextAlign = ContentAlignment.MiddleCenter,
                    Text = "星期" + RyDate.GetWeekName(i + 1),
                    Left = i * day_width,
                    Width = day_width,
                    Parent = PnlWeek
                };
            }
            DateTime = DateTime.Now;
        }

        private void Lunarday_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            LunarDay lunarday=(LunarDay)sender;
            //if(lunarday.Date.Month!=DateTime.Month)
            //{
            //    DateTime = lunarday.Date;
            //}
            for (int i = 0; i < list_lunar.Count; i++)
            {
                if(list_lunar[i]==lunarday)
                {
                    list_lunar[i].Selected=true;
                }
                else { list_lunar[i].Selected = false ; }
            }
        }

        private DateTime _DateTime = DateTime.Now;
        public DateTime DateTime
        {
            get { return _DateTime; }
            set
            {
                if (_DateTime != value)
                {
                    _DateTime = value;
                    ChangeDate(value);
                }
            }

        }
        private void ChangeDate(DateTime dt)
        {
            ProcUse = true;
            NumYear.Value = dt.Year;
            CbbMonth.SelectedIndex = dt.Month - 1;
            ProcUse = false;
            var month_date= RyDate.GetMonthStart(dt);
            var start_week = RyDate.GetWeek_index(month_date);
            var dt_now = DateTime.Now;
            for (int i = 0; i <35; i++)
            {
                var dt_2 = month_date.AddDays(i+1- start_week);
                var lunar = list_lunar[i];
                lunar.Day = dt_2.Day;
                var ChinaHoliday= ChinaDate.GetChinaHoliday(dt_2);
                var WorldHoliday = ChinaDate.GetHoliday(dt_2);
                var JieqiHoliday = ChinaDate.GetSolarTerm(dt_2);

                bool Holiday = false;
                string Holiday_Str = "";
                if (ChinaHoliday != "")
                { Holiday_Str = ChinaHoliday;}
                if (WorldHoliday != "")
                { Holiday_Str += " " + WorldHoliday; }
                if (JieqiHoliday!="")
                { Holiday_Str += " " + JieqiHoliday; }
                if (Holiday_Str!="")
                {
                    lunar.Lunar_Day = Holiday_Str.Trim(); lunar.Lunar_Day_ForeColor = Color.Orange; Holiday = true;
                }
                else
                {
                    var lunar_day_str = ChinaDate.GetDay(dt_2);
                    if (lunar_day_str == "初一")
                    {
                        lunar.Lunar_Day = ChinaDate.GetMonth(dt_2);
                    }
                    else
                    {
                        lunar.Lunar_Day = lunar_day_str;
                    }
                }
                lunar.Date = dt_2;
                if (dt_2.Month!=dt.Month)//不是当月
                {
                    lunar.Day_ForeColor = Color.FromArgb(190,190,190);
                    if (!Holiday)
                    { lunar.Lunar_Day_ForeColor = Color.FromArgb(190, 190, 190); }
                    lunar.BackColor = Color.White;
                }
                else
                {
                    if (dt_2.Date == dt_now.Date)
                    {
                        lunar.BackColor = Color.FromArgb(47, 152, 222);
                        lunar.Day_ForeColor = Color.White;
                        if (!Holiday)
                        { lunar.Lunar_Day_ForeColor = Color.White; }
                    }
                    else
                    {
                        lunar.Day_ForeColor = Color.Black;
                        if (!Holiday)
                        { lunar.Lunar_Day_ForeColor = Color.Black; }
                        lunar.BackColor = Color.White;
                    }
                }
            }
        }
        bool ProcUse = false;
        private void NumYear_ValueChanged(object sender, EventArgs e)
        {
            if (ProcUse) { return; }
            DateTime = new DateTime(NumYear.Value.ToInt(), CbbMonth.SelectedIndex + 1, 1);
        }

        private void CbbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProcUse) { return; }
            DateTime = new DateTime(NumYear.Value.ToInt(), CbbMonth.SelectedIndex + 1, 1);
        }

        private void BtnMonthPrev_Click(object sender, EventArgs e)
        {
            DateTime = DateTime.AddMonths(-1);
        }

        private void BtnMonthNext_Click(object sender, EventArgs e)
        {
            DateTime = DateTime.AddMonths(1);
        }

        private void BtnToday_Click(object sender, EventArgs e)
        {
            DateTime = DateTime.Now;
        }
    }
}
