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 ucPressCtrl : UserControl
    {
        private string pressDBOut = "DB3401";
        [Category("PCHMI"), Description("输出DB地址")]
        public string PressDBOut
        {
            get
            {
                return pressDBOut;
            }
            set
            {
                pressDBOut = value;
            }
        }

        private string pressDBIn = "DB3400";
        [Category("PCHMI"), Description("输入DB地址")]
        public string PressDBIn
        {
            get
            {
                return pressDBIn;
            }
            set
            {
                pressDBIn = value;
            }
        }

        public ucPressCtrl()
        {
            InitializeComponent();
        }

        private void ProgramAdds()
        {
            this.btnEnable.开关功能.地址[0] = $"{PressDBIn}.0.2";
            this.btnEnable.指示灯地址 = $"{PressDBIn}.0.2";

            this.btnRef.指示灯地址 = $"{PressDBIn}.4";
            this.btnTareForce.指示灯地址 = $"{PressDBIn}.4";
            this.btnGoHome.指示灯地址 = $"{PressDBIn}.4";
            this.btnJogDec.指示灯地址 = $"{PressDBIn}.4";
            this.btnJogAdd.指示灯地址 = $"{PressDBIn}.4";
            this.btnJogAddSafe.指示灯地址 = $"{PressDBIn}.0.6";
            this.btnJogDecSafe.指示灯地址 = $"{PressDBIn}.0.7";
            this.btnRelease.开关功能.地址[0] = $"{PressDBIn}.0.0";
            this.btnRelease.监控地址 = $"{PressDBIn}.0.0";
            this.反馈控制权.指示灯地址 = $"{PressDBOut}.4.2";
            this.btnJogSafe.开关功能.地址[0] = $"{PressDBIn}.1.0";
            this.btnJogSafe.监控地址 = $"{PressDBIn}.1.0";

            this.lblEnable.指示灯地址 = $"{PressDBOut}.0.0";
            this.lblRef.指示灯地址 = $"{PressDBOut}.0.6";
            this.在程序原点.指示灯地址 = $"{PressDBOut}.0.7";
            this.lblErr.指示灯地址 = $"{PressDBOut}.1.1";

            this.程序号设置.监控地址 = $"{PressDBIn}.8";
            this.btnProgChange.监控地址 = $"{PressDBIn}.4";

            this.txtActPrognum.监控地址 = $"{PressDBOut}.26";

            this.程序结束.开关功能.地址[0] = $"{PressDBIn}.0.4";
            this.程序结束.指示灯地址 = $"{PressDBIn}.0.4";
            this.btnGoSafe.开关功能.地址[0] = $"{PressDBIn}.1.2";
            this.btnGoSafe.监控地址 = $"{PressDBIn}.1.2";
            this.btnGoto.监控地址 = $"{PressDBIn}.4";

            this.暂停.开关功能.地址[0] = $"{PressDBIn}.0.5";
            this.暂停.监控地址 = $"{PressDBIn}.0.5";

            this.lblGoSafe.指示灯地址 = $"{PressDBIn}.1.1";
        }
        private void btnJogSafe_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.btnJogSafe.Value == 0)
            {
                PCHMI.OPTLOG.WLOG($"手动解除了励星压机1Jog保护，可能发生安全事故");
            }
        }
        private  void btnJogAdd_MouseDown(object sender, MouseEventArgs e)
        {
            PCHMI.OPTLOG.WLOG($"手动按下了励星压机1Jog Add");
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 10);
        }

        private  void btnJogAdd_MouseUp(object sender, MouseEventArgs e)
        {
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 0);
        }

        private  void btnJogDec_MouseDown(object sender, MouseEventArgs e)
        {
            PCHMI.OPTLOG.WLOG($"手动解除了励星压机1Jog Dec");
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 11);
        }

        private  void btnJogDec_MouseUp(object sender, MouseEventArgs e)
        {
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 0);
        }

        private void btnRelease_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.btnRelease.Value == 0)
            {
                PCHMI.OPTLOG.WLOG($"手动解除了励星压机1控制权");
            }
        }

        private async void btnProgChange_MouseDown(object sender, MouseEventArgs e)
        {
            PCHMI.OPTLOG.WLOG($"手动按下励星压机1切换程序");
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 0);
            await Task.Delay(500);
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 20);

        }

        private async void btnTareForce_MouseDown(object sender, MouseEventArgs e)
        {
            PCHMI.OPTLOG.WLOG($"手动按下励星压机1去皮");
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 0);
            await Task.Delay(500);
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 40);
        }

        private async void btnRef_MouseDown(object sender, MouseEventArgs e)
        {
            PCHMI.OPTLOG.WLOG($"手动按下励星压机1回机械原点");
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 0);
            await Task.Delay(500);
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 1);
        }

        private async void btnGoHome_MouseDown(object sender, MouseEventArgs e)
        {
            PCHMI.OPTLOG.WLOG($"手动按下励星压机1回程序原点");
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 0);
            await Task.Delay(500);
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 2);
        }

        private async void btnGoto_MouseDown(object sender, MouseEventArgs e)
        {
            PCHMI.OPTLOG.WLOG($"手动按下励星压机1执行压装按钮，程序号：{this.txtActPrognum.Value}");
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 0);
            await Task.Delay(500);
            PCHMI.RT.SET_INT16(0, $"{PressDBIn}.4", 30);
        }

        private void ucPressCtrl_Load(object sender, EventArgs e)
        {
            ProgramAdds();
        }
    }
}
