using Modbus.Device;
using Modbus;
using Sunny.UI;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace 自救器呼吸器综合检验仪
{
    public class Function
    {
        ModbusMaster master;
        IModbusMaster modbusMaster;
        DataChange dc = new DataChange();
        public enum ButtonType
        {
            复归型,
            切换型,
            置位型,
            复位型
        }
        public enum DataType
        {
            整形,
            浮点型
        }
        public Function(ModbusMaster master_in)
        {
            this.master = master_in;
        }

        public Function(IModbusMaster modbusMaster)
        {
            this.modbusMaster = modbusMaster;
        }

        public void BtnClickFunction(ButtonType buttonType, ushort address)
        {
            try
            {
                switch (buttonType)
                {
                    case ButtonType.复归型:
                        master.WriteSingleCoil(1, address, true);
                        Thread.Sleep(100);
                        master.WriteSingleCoil(1, address, false);
                        Thread.Sleep(100);
                        break;
                    case ButtonType.切换型:
                        if (master.ReadCoils(1, address, 1)[0])
                        {
                            master.WriteSingleCoil(1, address, false); Thread.Sleep(100);
                        }
                        else
                        { master.WriteSingleCoil(1, address, true); Thread.Sleep(100); }
                        break;
                    case ButtonType.置位型:
                        master.WriteSingleCoil(1, address, true);
                        Thread.Sleep(100);
                        break;
                    case ButtonType.复位型:
                        master.WriteSingleCoil(1, address, false);
                        Thread.Sleep(100);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {

            }

        }

        public void BtnClickFunctionForNew(ButtonType buttonType, ushort address)
        {
            try
            {
                switch (buttonType)
                {
                    case ButtonType.复归型:
                        modbusMaster.WriteSingleCoil(1, address, true);
                        Thread.Sleep(100);
                        modbusMaster.WriteSingleCoil(1, address, false);
                        Thread.Sleep(100);
                        break;
                    case ButtonType.切换型:
                        if (modbusMaster.ReadCoils(1, address, 1)[0])
                        {
                            modbusMaster.WriteSingleCoil(1, address, false); Thread.Sleep(100);
                        }
                        else
                        { modbusMaster.WriteSingleCoil(1, address, true); Thread.Sleep(100); }
                        break;
                    case ButtonType.置位型:
                        modbusMaster.WriteSingleCoil(1, address, true);
                        Thread.Sleep(100);
                        break;
                    case ButtonType.复位型:
                        modbusMaster.WriteSingleCoil(1, address, false);
                        Thread.Sleep(100);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {

            }

        }
        public void WriteToPLC(string inPutValue, ushort address, DataType dataType)
        {

            try
            {
                switch (dataType)
                {
                    case DataType.浮点型:
                        double value = inPutValue.ToDouble();
                        if (UIInputDialog.ShowInputDoubleDialog(ref value, UIStyle.Inherited, desc: "请输入值", showMask: false))
                        {

                            master.WriteMultipleRegisters(1, address, dc.SplitFloatToUShortArray((float)value));
                        }
                        break;
                    case DataType.整形:
                        int value_int = inPutValue.ToInt();
                        if (UIInputDialog.ShowInputIntegerDialog(ref value_int, UIStyle.Inherited, desc: "请输入数据："))
                        {

                            master.WriteMultipleRegisters(1, address, dc.intToushorts(value_int));
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作失败！" + "\n" + "\n" + ex.Message, "错误");
            }

        }

        public void WriteToPLCForNew(string inPutValue, ushort address, DataType dataType)
        {
            try
            {
                //KeyboardHelper.ShowSoftKeyboard();
                switch (dataType)
                {
                    case DataType.浮点型:
                        double value = inPutValue.ToDouble();

                        if (UIInputDialog.ShowInputDoubleDialog(ref value, UIStyle.Inherited, desc: "请输入值", showMask: false))
                        {

                            modbusMaster.WriteMultipleRegisters(1, address, dc.SplitFloatToUShortArray((float)value));
                        }
                        break;
                    case DataType.整形:
                        int value_int = inPutValue.ToInt();
                        if (UIInputDialog.ShowInputIntegerDialog(ref value_int, UIStyle.Inherited, desc: "请输入数据："))
                        {
                            modbusMaster.WriteMultipleRegisters(1, address, dc.intToushorts(value_int));
                        }
                        break;
                    default:
                        break;
                }

                //KeyboardHelper.HideSoftKeyboard();
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作失败！" + "\n" + "\n" + ex.Message, "错误");
            }

        }
    }
}
