using HKLib.DB;
using HKCardManager.UserPages;

namespace HKCardManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            splitContainer1.SplitterDistance = 240;
            Task.Factory.StartNew(() =>
            {
                DbContext.InitTable();
            });
            ShowPage(new PersonnelEntryPage());
            this.SizeChanged += Form1_SizeChanged;
            this.button1.SizeChanged += Button1_SizeChanged;
            MessageLogNotify.GetInstance.Info = new Action<string, Brush>((s, br) =>
             {
                 this.Invoke(() => { AddListToListBox(listBox1, s, br, true); });
             });
        }

        private void Write(string msg)
        {
            //bool条件默认为配置文件里面定义,可以自定义任何保存数据条件
            //bool temp = Convert.ToBoolean(iniFile.IniReadValue("设置", "保存运行信息", AppDomain.CurrentDomain.BaseDirectory + "设置.ini"));
            //if (temp)
            //{
            //    string logPath = Path.GetDirectoryName(Application.ExecutablePath);
            //    //  System.IO.StreamWriter sw = System.IO.File.AppendText(logPath + "/" + DateTime.Now.ToString("yyMMdd") + ".txt");
            //    System.IO.StreamWriter sw = System.IO.File.AppendText(logPath + "\\log\\" + DateTime.Now.ToString("yyMMdd") + ".txt");

            //    sw.WriteLine(DateTime.Now.ToString("HH:mm:ss  ") + msg);
            //    sw.Close();
            //    sw.Dispose();
            //}

        }

        private void AddListToListBox(ListBox rtbox, string text, Brush foreColor, bool addTime = true)
        {
            rtbox.DrawMode = DrawMode.OwnerDrawVariable;
            rtbox.Font = new Font("楷体", 12);
            if (rtbox.ItemHeight != 24)
            {
                rtbox.ItemHeight = 1;
                rtbox.DrawItem += ((sender, e) => //控件重绘事件
                {
                    if (e.Index == -1) return;
                    e.DrawBackground();
                    ListBoxItem item = (ListBoxItem)rtbox.Items[e.Index];
                    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    {
                        e.DrawFocusRectangle();
                        Brush b = Brushes.Khaki;//设置选中项颜色
                        e.Graphics.FillRectangle(b, e.Bounds);
                    }
                    if (item != null)
                    {
                        Brush brush = item.ForeColor;
                        Rectangle r = e.Bounds;
                        r.Height = e.Bounds.Height;
                        r.Width = e.Bounds.Width + 100;

                        e.Graphics.DrawString(item.Text, e.Font, brush, r, StringFormat.GenericDefault);
                    }
                    else
                    {
                        e.Graphics.DrawString(item.Text, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
                    }
                });
            }


            rtbox.ItemHeight = 25;//设置单行高度
            if (rtbox.InvokeRequired)
            {
                rtbox.Invoke(new Action<ListBox, string, Brush, bool>((rtb, str, color, addtime) => AddListToListBox(rtb, str, color, addtime)), rtbox, text, foreColor, addTime);
                return;
            }

            //text = string.Format("{0}-{1}", rtbox.Items.Count.ToString("000"), text);

            if (addTime)
            {
                //text = string.Format("[{0}]:{1}", DateTime.Now.ToString("HH:mm:ss"), text);
                text = $"{DateTime.Now.ToString("HH:mm:ss")}：{text}";
            }

            if (rtbox.Items.Count > 0)
                rtbox.Items.Insert(0, new ListBoxItem(text, foreColor));
            else
                rtbox.Items.Add(new ListBoxItem(text, foreColor));

            try
            {
                Write(text);

            }
            catch { }

            //清除过多的数据
            //if (rtbox.Items.Count > 100)
            //{
            //    int count = rtbox.Items.Count - 100;

            //    List<object> temp = new List<object>();

            //    for (int i = 0; i < count; i++)
            //    {
            //        temp.Add(rtbox.Items[i]);
            //    }

            //    foreach (object obj in temp)
            //    {
            //        rtbox.Items.Remove(obj);
            //    }
            //}
            //rtbox.SelectedIndex = rtbox.Items.Count - 1;
        }





        private void Button1_SizeChanged(object? sender, EventArgs e)
        {
            groupBox1.Width = button1.Width;
            groupBox2.Width = button1.Width;
            groupBox3.Width = button1.Width;
            groupBox4.Width = button1.Width;

            button2.Left = 10;
            button2.Width = (groupBox1.Width - 20);

            button3.Left = 10;
            button3.Width = (groupBox1.Width - 20);

            button4.Left = 10;
            button4.Width = (groupBox2.Width - 20);

            button5.Left = 10;
            button5.Width = (groupBox2.Width - 20);

            button6.Left = 10;
            button6.Width = (groupBox2.Width - 20);

            button7.Left = 10;
            button7.Width = (groupBox2.Width - 20);

            button9.Left = 10;
            button9.Width = (groupBox3.Width - 20);

            button10.Left = 10;
            button10.Width = (groupBox3.Width - 20);

            button11.Left = 10;
            button11.Width = (groupBox3.Width - 20);

            button12.Left = 10;
            button12.Width = (groupBox4.Width - 20);
        }



        UserControl tempUserControl;
        private void Form1_SizeChanged(object? sender, EventArgs e)
        {
            if (tempUserControl != null)
            {
                tempUserControl.Width = this.panel1.Width;
                tempUserControl.Height = this.panel1.Height;
            }
            splitContainer1.SplitterDistance = 240;

            if (this.WindowState == FormWindowState.Maximized || this.WindowState == FormWindowState.Normal)
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(100);
                    this.Invoke(() =>
                    {
                        if (tempUserControl != null)
                        {
                            tempUserControl.Width = this.panel1.Width;
                            tempUserControl.Height = this.panel1.Height;
                        }
                        splitContainer1.SplitterDistance = 240;
                    });
                });
            }
        }

        //private void 人员录入ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    new PersonnelEntry().ShowDialog();
        //}

        //private void 机构录入ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    new InstitutionalEntry().ShowDialog();
        //}

        //private void 发卡ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    new Hairpin().ShowDialog();
        //}

        //private void 销户ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    Cancellation cancellation = new Cancellation();
        //    cancellation.Text = "销户";
        //    cancellation.ShowDialog();
        //}

        //private void 挂失ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    Cancellation cancellation = new Cancellation();
        //    cancellation.Text = "挂失";
        //    cancellation.ShowDialog();
        //}

        //private void 解挂ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    Cancellation cancellation = new Cancellation();
        //    cancellation.Text = "解挂";
        //    cancellation.ShowDialog();
        //}

        //private void 月餐段汇总表ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    DailyConsumptionTable dailyConsumptionTable = new DailyConsumptionTable();
        //    dailyConsumptionTable.Text = "月餐段汇总表";
        //    dailyConsumptionTable.ShowDialog();
        //}

        //private void 计次就餐天消费表ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    DailyConsumptionTable dailyConsumptionTable = new DailyConsumptionTable();
        //    dailyConsumptionTable.Text = "计次就餐天消费表";
        //    dailyConsumptionTable.ShowDialog();
        //}

        //private void 计次就餐消费明细表ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    DailyConsumptionTable dailyConsumptionTable = new DailyConsumptionTable();
        //    dailyConsumptionTable.Text = "计次就餐消费明细表";
        //    dailyConsumptionTable.ShowDialog();
        //}

        //private void 餐段设置ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    new MealSegmentSet().ShowDialog();
        //}

        //人员录入
        private void button2_Click(object sender, EventArgs e)
        {
            ShowPage(new PersonnelEntryPage());
        }

        //机构录入
        private void button3_Click(object sender, EventArgs e)
        {
            ShowPage(new InstitutionalEntryPage());
        }

        //发卡
        private void button5_Click(object sender, EventArgs e)
        {
            ShowPage(new CarMangerPage());
        }

        //销户，挂失，解挂
        private void button4_Click(object sender, EventArgs e)
        {
            ShowPage(new CancellationPage(), ((Button)sender)?.Tag.ToString());
        }

        //月餐段汇总表
        private void button11_Click(object sender, EventArgs e)
        {
            ShowPage(new ReportFormPage(), ((Button)sender)?.Tag.ToString());
        }

        //餐段设置
        private void button12_Click(object sender, EventArgs e)
        {
            ShowPage(new MealSegmentSetPage());
        }

        private void ShowPage(UserControl userControl, string? name = "")
        {
            panel1.Controls.Clear();
            userControl.Width = panel1.Width;
            userControl.Height = panel1.Height;
            tempUserControl = userControl;
            userControl.Tag = name;
            userControl.Show();
            panel1.Controls.Add(userControl);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            ShowPage(new AdvertisingSetPage());
        }
    }
}