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 System.Threading;

namespace GcDevicePc.Controls
{
    public partial class PictureMenu : UserControl
    {
        /// <summary>
        /// 普通按钮的点击委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void OrdBthClickHandle(object sender, EventArgs e);
        /// <summary>
        /// 开始，停止，结束的点击委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void OptBthClickHandle(object sender, EventArgs e);

        /// <summary>
        /// 系统点击委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public delegate void SysBthClickHandle(object sender, EventArgs e);

        private int pmoptbtnstatus = 0;
        [Description("修改此值，可修改OPT的状态"), Category("自定义属性")]
        public int PMOptbtnstatus
        {
            get
            {
                return pmoptbtnstatus;
            }
            set
            {
                pmoptbtnstatus = value;
                if (pmoptimage != null && pmoptimage.Images.Count >= 9)
                {
                    switch (pmoptbtnstatus)
                    {
                        case 0:
                            ((PictureBox)PMPanel.Controls[0]).Image = pmoptimage.Images[6];
                            ((PictureBox)PMPanel.Controls[0]).Enabled = false;
                            ((PictureBox)PMPanel.Controls[1]).Image = pmoptimage.Images[7];
                            ((PictureBox)PMPanel.Controls[1]).Enabled = false;
                            ((PictureBox)PMPanel.Controls[2]).Image = pmoptimage.Images[8];
                            ((PictureBox)PMPanel.Controls[2]).Enabled = false;
                            break;
                        case 3:
                            ((PictureBox)PMPanel.Controls[0]).Image = pmoptimage.Images[6];
                            ((PictureBox)PMPanel.Controls[0]).Enabled = false;
                            ((PictureBox)PMPanel.Controls[1]).Image = pmoptimage.Images[1];
                            ((PictureBox)PMPanel.Controls[1]).Enabled = true;
                            ((PictureBox)PMPanel.Controls[2]).Image = pmoptimage.Images[2];
                            ((PictureBox)PMPanel.Controls[2]).Enabled = true;
                            break;
                        case 4:
                            ((PictureBox)PMPanel.Controls[0]).Image = pmoptimage.Images[0];
                            ((PictureBox)PMPanel.Controls[0]).Enabled = true;
                            ((PictureBox)PMPanel.Controls[1]).Image = pmoptimage.Images[7];
                            ((PictureBox)PMPanel.Controls[1]).Enabled = false;
                            ((PictureBox)PMPanel.Controls[2]).Image = pmoptimage.Images[8];
                            ((PictureBox)PMPanel.Controls[2]).Enabled = false;
                            break;
                    }
                }
                this.Invalidate();
            }
        }

        private int pmsysbtnstatus = 0;
        [Description("修改此值，可修改SYS的状态"), Category("自定义属性")]
        public int PMSysbtnstatus
        {
            get
            {
                return pmsysbtnstatus;
            }
            set
            {
                pmsysbtnstatus = value;
                if (pmsysimage != null && pmsysimage.Images.Count >= 2)
                {
                    switch (pmsysbtnstatus)
                    {
                        case 0:
                            ((PictureBox)PMPanel.Controls[14]).Image = pmsysimage.Images[0]; 
                            break;
                        case 1:
                            ((PictureBox)PMPanel.Controls[14]).Image = pmsysimage.Images[1];

                           
                            break;
                    }
                }
                this.Invalidate();
            }
        }

        private ImageList pmoptimage; 
        [Description("修改此值，可修改OPT的图标"), Category("自定义属性")]
        public ImageList PMOptimage                  // 控件的自定义属性值  
        {
            get
            {
                return pmoptimage;
            }
            set
            {
                pmoptimage = value;
                this.Invalidate();
            }
        }

        private ImageList pmordimage;
        [Description("修改此值，可修改ORD的图标"), Category("自定义属性")]
        public ImageList PMOrdimage                  // 控件的自定义属性值  
        {
            get
            {
                return pmordimage;
            }
            set
            {
                pmordimage = value;
                this.Invalidate();
            }
        }

        private ImageList pmsysimage;
        [Description("修改此值，可修改SYS的图标"), Category("自定义属性")]
        public ImageList PMSysimage                  // 控件的自定义属性值  
        {
            get
            {
                return pmsysimage;
            }
            set
            {
                pmsysimage = value;
                this.Invalidate();
            }
        }


        private int pmheight = 62;
        [Description("修改此值，可修改高度"), Category("自定义属性")]
        public int PMHeight                  // 控件的自定义属性值  
        {
            get
            {
                return pmheight;
            }
            set
            {
                pmheight = value;
                this.Invalidate();  
            }
        }

        private int pmwidth = 57; 
        [Description("修改此值，可修改宽度"), Category("自定义属性")]
        public int PMWidth                 // 控件的自定义属性值  
        {
            get
            {
                return pmwidth;
            }
            set
            {
                pmwidth = value;
                this.Invalidate();
            }
        }

        private int ordbtnnum = 10;  
        [Description("修改此值，可修改按钮数"), Category("自定义属性")]
        public int ORDBtnnum                 // 控件的自定义属性值  
        {
            get
            {
                return ordbtnnum;
            }
            set
            {
                ordbtnnum = value;
                this.Invalidate();
            }
        }

        private  int optbtnnum = 3; 
        [Description("只读显示操作按钮数量"), Category("自定义属性")]
        public int OPTBtnnum                 // 控件的自定义属性值  
        {
            get
            {
                return optbtnnum;
            }
            set
            {
                optbtnnum = value;
                this.Invalidate();
            }
        }

        private int sysbtnnum = 1;
        [Description("修改此值，可修改系统按钮"), Category("自定义属性")]
        public int SYSBtnnum                 // 控件的自定义属性值  
        {
            get
            {
                return sysbtnnum;
            }
            set
            {
                sysbtnnum = value;
                this.Invalidate();
            }
        }

        private string statustext = "未连接";
        [Description("修改此值，可修改显示值"), Category("自定义属性")]
        public string StatusText                 // 控件的自定义属性值  
        {
            get
            {
                return statustext;
            }
            set
            {
                statustext = value;
                if(PMPanel.Controls.Count > 10)
                    ((HMIStatus)PMPanel.Controls[10]).ShowText = statustext;
                this.Invalidate();
            }
        }

        private Boolean waingshow = true;
        [Description("修改此值，可修改显示颜色"), Category("自定义属性")]
        public Boolean WaingShow                 // 控件的自定义属性值  
        {
            get
            {
                return waingshow;
            }
            set
            {
                waingshow = value;
                if (PMPanel.Controls.Count > 10)
                    ((HMIStatus)PMPanel.Controls[10]).WaingColor = waingshow;
                this.Invalidate();
            }
        }

        public PictureMenu()
        {
            InitializeComponent();
            PMPanel.Height = this.Height;
            PMPanel.Width = this.Width;
        }

        public event OrdBthClickHandle PMOrdBtnClicked;
        public event OptBthClickHandle PMOptBtnClicked;
        public event SysBthClickHandle PMSysBtnClicked;

        private void PictureMenu_Load(object sender, EventArgs e)
        {

            int i;
            PMPanel.Height = this.Height;
            PMPanel.Width = this.Width;
            for(i = 0; i < optbtnnum; i++)
            {
                PictureBox optbtn = new PictureBox();
                optbtn.Tag = i;
                optbtn.Name = "optbth_" + i;
                optbtn.Height = pmheight;
                optbtn.Width = pmwidth;
                if (pmoptimage != null && pmoptimage.Images.Count > 0)
                    optbtn.Image = pmoptimage.Images[i];
                optbtn.Click += Opt_Click;
                this.PMPanel.Controls.Add(optbtn);
            }

            for (i = 0; i < ordbtnnum; i++)
            {
                PictureBox ordbtn = new PictureBox();
                ordbtn.Tag = i;
                ordbtn.Name = "ordbth_" + i;
                ordbtn.Height = pmheight;
                ordbtn.Width = pmwidth;
                if (pmordimage != null && pmordimage.Images.Count > 0)
                    ordbtn.Image = pmordimage.Images[i];
                ordbtn.Click += Ord_Click;

                ordbtn.MouseDown += new MouseEventHandler(btn_MouseDown);
                ordbtn.MouseHover += new EventHandler(btn_MouseHover);
                ordbtn.MouseLeave += new EventHandler(btn_MouseLeave);
                ordbtn.MouseUp += new MouseEventHandler(btn_MouseUp); 

                //ordbtn.MouseHover += new EventHandler();
                this.PMPanel.Controls.Add(ordbtn);

                if (ordbtnnum > 3 && ordbtnnum - i == 4)
                {
                    HMIStatus hmipanel = new HMIStatus();
                    this.PMPanel.Controls.Add(hmipanel);
                }
            }

            for( i = 0; i < sysbtnnum; i++)
            {
                PictureBox sysbtn = new PictureBox();
                sysbtn.Tag = i;
                sysbtn.Name = "sysbth_" + i;
                sysbtn.Height = pmheight;
                sysbtn.Width = pmwidth;
                if (pmsysimage != null && pmsysimage.Images.Count > 0)
                    sysbtn.Image = pmsysimage.Images[i];
                sysbtn.Click += Sys_Click;
                this.PMPanel.Controls.Add(sysbtn);
            }
        }

        private void btn_MouseUp(object sender, MouseEventArgs e)
        {
            PictureBox ordbtn = sender as PictureBox;
            int tagnum = Convert.ToInt32(ordbtn.Tag);
            ordbtn.Image = pmordimage.Images[tagnum + ordbtnnum];
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            PictureBox ordbtn = sender as PictureBox;
            int tagnum = Convert.ToInt32(ordbtn.Tag);
            ordbtn.Image = pmordimage.Images[tagnum];
        }

        private void btn_MouseHover(object sender, EventArgs e)
        {
            PictureBox ordbtn = sender as PictureBox;
            int tagnum = Convert.ToInt32(ordbtn.Tag);
            ordbtn.Image = pmordimage.Images[tagnum + ordbtnnum];
        }

        private void btn_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox ordbtn = sender as PictureBox;
            int tagnum = Convert.ToInt32(ordbtn.Tag);
            ordbtn.Image = pmordimage.Images[tagnum];
        }

        void Opt_Click(object sender, EventArgs e)
        {
            if (PMOptBtnClicked != null)
            {
                PictureBox optbtn = sender as PictureBox;
                if(optbtn.Enabled == true)
                    PMOptBtnClicked(sender, e);
            }
        }

        void Ord_Click(object sender, EventArgs e)
        {
            if (PMOrdBtnClicked != null)
            {
                PMOrdBtnClicked(sender, e);              
            }
        }

        void Sys_Click(object sender, EventArgs e)
        {
            if (PMSysBtnClicked != null)
            {
                PMSysBtnClicked(sender, e);   
            }    
        }
    }
}
