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

using Modbus.Device;

namespace 头罩视野.Services
{
    public class Function
    {
        ModbusMaster modbusMaster;
        IModbusMaster modbusmodbusMaster;
        DataChange dc = new DataChange();
        public enum ButtonType
        {
            复归型,
            切换型,
            置位型,
            复位型
        }
        public enum DataType
        {
            整形,
            浮点型
        }
        public Function(ModbusMaster modbusMaster_in)
        {
            this.modbusMaster = modbusMaster_in;
        }

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

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

            }

        }

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

            //}
            catch (Exception )
            {

            }

        }
        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))
                        {

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

                            modbusmodbusMaster.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,3, desc: "请输入值", showMask: false))
                        {

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

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

        }


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

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

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

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

        }
    }
}
