using MetroFramework.Forms;
using System;
using System.Windows.Forms;

namespace HSUCO_Cargo_Garage_Operation_Program.CustomForm
{
    public partial class Promotion : MetroForm
    {
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public Promotion(string title, string startName, string endName)
        {
            InitializeComponent();
            this.Text = title;
            metroLabel1.Text = startName;
            metroLabel2.Text = endName;
        }

        void metroButton_Promotion_Click(object sender, EventArgs e)
        {
            StartDate = metroDateTime_Start.Value;
            EndDate = metroDateTime_End.Value;
            
            DialogResult = DialogResult.OK;
        }

        void metroButton_Cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        void Promotion_Load(object sender, EventArgs e)
        {
            var year = DateTime.Now.Year + 1;
            metroDateTime_Start.Value = DateTime.Parse(year + "-01-01");
            metroDateTime_End.Value = DateTime.Parse(year + "-12-31");
        }

        private void metroDateTime_Start_ValueChanged(object sender, EventArgs e)
        {
            if (metroDateTime_Start.Value.Day != 1)
            {
                metroDateTime_Start.Value = new DateTime(metroDateTime_Start.Value.Year, metroDateTime_Start.Value.Month, 1);
            }
            if(metroDateTime_Start.Value.Year > metroDateTime_End.Value.Year)
            {
                metroDateTime_End.Value = new DateTime(metroDateTime_Start.Value.Year, metroDateTime_Start.Value.Month, DateTime.DaysInMonth(metroDateTime_Start.Value.Year, metroDateTime_Start.Value.Month));
            }
            int lastDay = DateTime.DaysInMonth(metroDateTime_End.Value.Year, metroDateTime_End.Value.Month);
            if (metroDateTime_End.Value.Day != lastDay)
            {
                metroDateTime_End.Value = new DateTime(metroDateTime_End.Value.Year, metroDateTime_End.Value.Month, lastDay);
            }

        }
    }
}