using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PaintingPC
{
    public partial class FrmTemp1 : Form
    {
        public static string ColorName = "";
        public string station = "";

        public FrmTemp1()
        {
            InitializeComponent();
        }

        private void FrmTemp1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

            UpdateSoftware();
            InitPage();
        }

        public void InitPage()
        {
            try
            {
                ColorName = "";

                string title = ConfigurationManager.AppSettings["Position"].ToString();
                station = ConfigurationManager.AppSettings["Station"].ToString();

                labTitle.Text = title;

                Thread t = new Thread(new ThreadStart(TimeGo));
                t.Start();

                InitButton(200,150);
            }
            catch (Exception ex)
            { }
        }

        private void TimeGo()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 10;
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            labTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            Thread.Sleep(500);
        }

        private void labTime_Click(object sender, EventArgs e)
        {
            Environment.Exit(0); 
        }

        private void InitButton(int sizeW, int sizeH)
        {
            panel2.Controls.Clear();

            DataTable dt = Function.GetCarTypeAll();
            if (dt != null && dt.Rows.Count > 0)
            {
                Label[] dLb = new Label[dt.Rows.Count];
                int row = 0, col = 1;
                row = panel2.Size.Height / sizeH;
                col = panel2.Size.Width / sizeW;

                List<Point> points = new List<Point>();
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        Point p = new Point();
                        p.X = 9 + j * sizeW;
                        p.Y = 9 + i * sizeH;
                        points.Add(p);
                    }
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dLb[i] = new Label();
                    dLb[i].Text = dt.Rows[i][0].ToString();
                    dLb[i].Tag = dt.Rows[i][0].ToString();
                    dLb[i].Font = new Font("微软雅黑", 24, FontStyle.Bold);
                    dLb[i].Size = new Size(sizeW, sizeH);
                    //dLb[i].Location = new Point(9 + row * (dLb[i].Size.Width + 10), 9 + col * (dLb[i].Size.Height + 10));
                    dLb[i].Location = points[i];
                    dLb[i].BorderStyle = BorderStyle.FixedSingle;
                    dLb[i].BackColor = Color.Transparent;
                    dLb[i].TextAlign = ContentAlignment.MiddleCenter;
                    panel2.Controls.Add(dLb[i]);
                    dLb[i].Click += new EventHandler(dLbLable_Click);
                }
            }
        }

        void dLbLable_Click(object sender, EventArgs e)
        {
            

            Label label = (Label)sender;
            string type = label.Tag.ToString();

            this.Tag = type;

            InitColor(type);
        }

        private void InitColor(string type)
        {
            //根据车型ID查询颜色,生成按钮于panel2
            DataTable dt = Function.GetColorByType(type);
            panel2.Controls.Clear();

            if (dt != null && dt.Rows.Count > 0)
            {
                int sizeW = 200, sizeH = 150;

                Label[] dLb = new Label[dt.Rows.Count];
                int row = 0, col = 1;
                row = panel2.Size.Height / sizeH;
                col = panel2.Size.Width / sizeW;

                List<Point> points = new List<Point>();
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        Point p = new Point();
                        p.X = 9 + j * sizeW;
                        p.Y = 9 + i * sizeH;
                        points.Add(p);
                    }
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dLb[i] = new Label();
                    dLb[i].Text = dt.Rows[i][0].ToString();
                    dLb[i].Tag = dt.Rows[i][0].ToString();
                    dLb[i].Font = new Font("微软雅黑", 24, FontStyle.Bold);
                    dLb[i].Size = new Size(sizeW, sizeH);
                    //dLb[i].Location = new Point(9 + row * (dLb[i].Size.Width + 10), 9 + col * (dLb[i].Size.Height + 10));
                    dLb[i].Location = points[i];
                    dLb[i].BorderStyle = BorderStyle.FixedSingle;
                    dLb[i].BackColor = Color.Transparent;
                    dLb[i].TextAlign = ContentAlignment.MiddleCenter;
                    panel2.Controls.Add(dLb[i]);
                    dLb[i].Click += new EventHandler(dLbColor_Click);
                }
            }
        }

        void dLbColor_Click(object sender, EventArgs e)
        {
            Label lab = (Label)sender;
            ColorName = lab.Text.Trim();

            panel2.Controls.Clear();

            Label label1 = new Label();
            label1.Font = new System.Drawing.Font("微软雅黑", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            label1.Location = new System.Drawing.Point(9, 18);
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size(122, 53);
            label1.TabIndex = 1;
            label1.Text = "扫码:";
            label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

            TextBox textBox1 = new TextBox();
            textBox1.Font = new System.Drawing.Font("微软雅黑", 26F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            textBox1.Location = new System.Drawing.Point(134, 18);
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size(754, 53);
            textBox1.TabIndex = 0;
            textBox1.KeyDown += textBox1_KeyDown;

            panel2.Controls.Add(label1);
            panel2.Controls.Add(textBox1);
            textBox1.Focus();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextBox tb = (TextBox)sender;
                tb.SelectAll();
                Function.SavePaintLoad(tb.Text.Trim(), ColorName);
            }
        }

        private void UpdateSoftware()
        {
            var serverUrl = "http://10.60.101.59:8013/";
            var updateXmlFileName = "PaintingUpdate.xml";
            var updater = new AutoUpdater();
            if (updater.CheckUpdateLoad(serverUrl, updateXmlFileName))
            {
                Environment.Exit(0);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void labTitle_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        /// <summary>
        /// 加载颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                string type = this.Tag.ToString();
                InitColor(type);
            }
            else
            {
                MessageBox.Show("请先选择车型");
                InitButton(200, 150);
            }
        }

        /// <summary>
        /// 加载车型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.Tag = null;
            InitButton(200, 150);
        }


    }
}