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 CowainHmi
{
    public partial class Cylinder : UserControl
    {
        private string[] cyNameList;
        [Category("PCHMI"), Description("气缸名称列表")]
        public string[] CyNameList
        {
            get
            {
                return this.cyNameList;
            }
            set
            {
                this.cyNameList = value;
            }
        }

        private string cyIdAddr = "";
        [Category("PCHMI"), Description("气缸编号地址")]
        public string CyIdAddr
        {
            get
            {
                return cyIdAddr;
            }
            set
            {
                cyIdAddr = value;
            }
        }


        private string dataAddr = "";
        [Category("PCHMI"), Description("气缸控制DB地址")]
        public string DataAddr
        {
            get
            {
                return dataAddr;
            }
            set
            {
                dataAddr = value;
            }
        }

        private string errAddr = "";
        [Category("PCHMI"), Description("气缸错误DB地址")]
        public string ErrAddr
        {
            get
            {
                return errAddr;
            }
            set
            {
                errAddr = value;
            }
        }
        private string paramAddr = "";
        [Category("PCHMI"), Description("气缸参数DB地址")]
        public string ParamAddr
        {
            get
            {
                return paramAddr;
            }
            set
            {
                paramAddr = value;
            }
        }
        private int id = 0;
        public Cylinder()
        {
            InitializeComponent();

        }

        private void CbxCySelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            //PCHMI.RT.SEND_UINT16(0, CyIdAddr, (ushort)(this.cbxCySelect.SelectedIndex + 1));
            SetAddress();
        }
        private string cyName;
        private void SetAddress()
        {
            id = this.cbxCySelect.SelectedIndex;
            cyName = $"SV{(id + 1).ToString("D3")}-气缸{id}";
            this.btnToWork.开关功能.地址[0] = $"{DataAddr}.{id * 6}.0";
            this.btnToWork.指示灯地址 = $"{DataAddr}.{id * 6}.0";
            this.btnToBase.开关功能.地址[0] = $"{DataAddr}.{id * 6}.1";
            this.btnToBase.指示灯地址 = $"{DataAddr}.{id * 6}.1";
            this.btnWorkSafe.开关功能.地址[0] = $"{DataAddr}.{id * 6 + 1}.2";
            this.btnWorkSafe.监控地址 = $"{DataAddr}.{id * 6 + 1}.2";
            this.btnBaseSafe.开关功能.地址[0] = $"{DataAddr}.{id * 6 + 1}.1";
            this.btnBaseSafe.监控地址 = $"{DataAddr}.{id * 6 + 1}.1";
            this.btnWorkSensor.开关功能.地址[0] = $"{ParamAddr}.{id * 10 + 8}.1";
            this.btnWorkSensor.监控地址 = $"{ParamAddr}.{id * 10 + 8}.1";
            this.btnBaseSensor.开关功能.地址[0] = $"{ParamAddr}.{id * 10 + 8}.0";
            this.btnBaseSensor.监控地址 = $"{ParamAddr}.{id * 10 + 8}.0";
            this.btnWorkUnlock.开关功能.地址[0] = $"{DataAddr}.{id * 6}.4";
            this.btnWorkUnlock.监控地址 = $"{DataAddr}.{id * 6}.4";
            this.btnBaseUnlock.开关功能.地址[0] = $"{DataAddr}.{id * 6}.4";
            this.btnBaseUnlock.监控地址 = $"{DataAddr}.{id * 6}.4";
            this.txtWorkDoneTime.监控地址 = $"{ParamAddr}.{id * 10}";
            this.txtBaseDoneTime.监控地址 = $"{ParamAddr}.{id * 10 + 2}";
            this.txtWorkErrTime.监控地址 = $"{ParamAddr}.{id * 10 + 4}";
            this.txtBaseErrTime.监控地址 = $"{ParamAddr}.{id * 10 + 6}";
            this.lblToWork.监控地址 = $"{DataAddr}.{id * 6 + 4}.0";
            this.lblToBase.监控地址 = $"{DataAddr}.{id * 6 + 4}.1";
            this.lblInWork.监控地址 = $"{DataAddr}.{id * 6 + 4}.2";
            this.lblInBase.监控地址 = $"{DataAddr}.{id * 6 + 4}.3";
            this.lblWorkSafe.监控地址 = $"{DataAddr}.{id * 6 + 1}.0";
            this.lblBaseSafe.监控地址 = $"{DataAddr}.{id * 6 + 0}.7";
            this.lblNotWork.监控地址 = $"{ErrAddr}.{id * 2 + 0}.2";
            this.lblNotBase.监控地址 = $"{ErrAddr}.{id * 2 + 0}.3";
            this.lblManWork.监控地址 = $"{ErrAddr}.{id * 2 + 0}.0";
            this.lblManBase.监控地址 = $"{ErrAddr}.{id * 2 + 0}.1";
        }

        private void Cylinder_Load(object sender, EventArgs e)
        {
            id = PCHMI.VL.GET_UINT16(0, CyIdAddr) - 1;//读气缸编号
            if (id < 0)
            {
                PCHMI.RT.SEND_UINT16(0, CyIdAddr, 1);
                id = 0;
            }
            this.cbxCySelect.DataSource = CyNameList;
            this.cbxCySelect.SelectedIndex = id;
            this.cbxCySelect.SelectedIndexChanged += CbxCySelect_SelectedIndexChanged;
            SetAddress();
        }       

        private void btnWorkSafe_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.btnWorkSafe.Value == 0)
            {
                PCHMI.OPTLOG.WLOG($"手动解除了气缸去工作位安全保护，气缸名称：{cyName}");
            }

        }

        private void btnBaseSafe_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.btnBaseSafe.Value == 0)
            {
                PCHMI.OPTLOG.WLOG($"手动解除了气缸去原位安全保护，气缸名称：{cyName}");
            }

        }

        private void btnBaseSensor_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.btnBaseSensor.Value == 0)
            {
                PCHMI.OPTLOG.WLOG($"手动按下了气缸原位屏蔽按钮，气缸名称：{cyName}");
            }
            else
            {
                PCHMI.OPTLOG.WLOG($"手动解除了气缸原位屏蔽按钮，气缸名称：{cyName}");
            }
        }

        private void btnWorkSensor_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.btnWorkSensor.Value == 0)
            {
                PCHMI.OPTLOG.WLOG($"手动按下了气缸工作位屏蔽按钮，气缸名称：{cyName}");
            }
            else
            {
                PCHMI.OPTLOG.WLOG($"手动解除了气缸工作位屏蔽按钮，气缸名称：{cyName}");
            }
        }

        private void btnBaseUnlock_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.btnBaseUnlock.Value == 0)
            {
                PCHMI.OPTLOG.WLOG($"清除了气缸去原位手动记忆，气缸名称：{cyName}");
            }

        }

        private void btnWorkUnlock_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.btnWorkUnlock.Value == 0)
            {
                PCHMI.OPTLOG.WLOG($"清除了气缸去工作位手动记忆，气缸名称：{cyName}");
            }
        }
    }
}
