using HKLib.Dto;
using HKLib.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HKCardManager.UserPages
{
    public partial class MealSegmentSetPage : UserControl
    {
        public MealSegmentSetPage()
        {
            InitializeComponent();
            this.SizeChanged += MealSegmentSetPage_SizeChanged;
            Task.Factory.StartNew(() =>
            {
                var res = HKLibHelper.GetTime();
                if (res != null)
                {
                    this.Invoke(() =>
                    {
                        dateTimePicker1.Value = res.AMStartTime;
                        dateTimePicker4.Value = res.AMEndTime;
                        dateTimePicker2.Value = res.PMStartTime;
                        dateTimePicker5.Value = res.PMEndTime;
                        dateTimePicker3.Value = res.ATStartTime;
                        dateTimePicker6.Value = res.ATEndTime;
                    });
                }
                else MessageLogNotify.GetInstance.ShowError("获取餐段时间失败！");
            });
        }

        private void MealSegmentSetPage_SizeChanged(object? sender, EventArgs e)
        {
            panel1.Left = (this.Width - panel1.Width) / 2;
            panel1.Top = (this.Height - panel1.Height) / 2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var data = new TimeSpilte()
            {
                AMStartTime = dateTimePicker1.Value,
                AMEndTime = dateTimePicker4.Value,
                PMStartTime = dateTimePicker2.Value,
                PMEndTime = dateTimePicker5.Value,
                ATStartTime = dateTimePicker3.Value,
                ATEndTime = dateTimePicker6.Value
            };
            Task.Factory.StartNew(() =>
            {
                this.Invoke(() => { button1.Enabled = false; });
                if (HKLibHelper.SetTime(data))
                {
                    this.Invoke(() =>
                    {
                        dateTimePicker1.Value = data.AMStartTime;
                        dateTimePicker4.Value = data.AMEndTime;
                        dateTimePicker2.Value = data.PMStartTime;
                        dateTimePicker5.Value = data.PMEndTime;
                        dateTimePicker3.Value = data.ATStartTime;
                        dateTimePicker6.Value = data.ATEndTime;
                    });
                    MessageLogNotify.GetInstance.Show("设置餐段时间成功");
                }
                else
                {
                    MessageLogNotify.GetInstance.ShowError("设置时间段失败，请重试！");
                }
                this.Invoke(() => { button1.Enabled = true; });
            });
        }
    }
}
