using 智慧物流软件系统.ViewModel;
using Ping9719.WpfEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 智慧物流软件系统
{
    /// <summary>
    /// 设备监控
    /// </summary>
    public class ExcelDeviceReadModel
    {
        public string 名称 { get; set; }
        public string 组名 { get; set; }
        public string 类型 { get; set; }
        public string 地址 { get; set; }
        public string 单位 { get; set; }

        public static List<DeviceReadModel> GetDatas(string excelPath = null, string sheetName = "设备监控")
        {
            if (string.IsNullOrEmpty(excelPath))
                excelPath = LocalFile.PlcDotPath;

            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceReadModel>(excelPath, sheetName)
                .Where(o => !string.IsNullOrWhiteSpace(o.名称))
                .Select(o => new DeviceReadModel()
                {
                    Name = o.名称,
                    ExcelTag = o,
                }).ToList();
        }
    }

    /// <summary>
    /// 设备控制
    /// </summary>
    public class ExcelDeviceWriteModel
    {
        public string 名称 { get; set; }
        public string 组名 { get; set; }
        public string 类型 { get; set; }
        public string 读地址 { get; set; }
        public string 写地址 { get; set; }
        public string 点动切换 { get; set; }
        public string 单位 { get; set; }

        public static List<DeviceWriteModel> GetDatas(string excelPath = null, string sheetName = "设备控制")
        {
            if (string.IsNullOrEmpty(excelPath))
                excelPath = LocalFile.PlcDotPath;

            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceWriteModel>(excelPath, sheetName)
                .Where(o => !string.IsNullOrWhiteSpace(o.名称))
                .Select(o => new DeviceWriteModel()
                {
                    Name = o.名称,
                    ExcelTag = o,
                }).ToList();
        }
    }

    /// <summary>
    /// 设备气缸
    /// </summary>
    public class ExcelDeviceUrnModel
    {
        public string 名称 { get; set; }
        public string 组名 { get; set; }
        public string 推地址 { get; set; }
        public string 回地址 { get; set; }
        public string 推到位地址 { get; set; }
        public string 回到位地址 { get; set; }
        public string 点动切换 { get; set; }

        public static List<DeviceUrnModel> GetDatas(string excelPath = null, string sheetName = "设备气缸")
        {
            if (string.IsNullOrEmpty(excelPath))
                excelPath = LocalFile.PlcDotPath;

            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceUrnModel>(excelPath, sheetName)
                .Where(o => !string.IsNullOrWhiteSpace(o.名称))
                .Select(o => new DeviceUrnModel()
                {
                    Name = o.名称,
                    ExcelTag = o,
                }).ToList();
        }
    }

    /// <summary>
    /// 设备伺服
    /// </summary>
    public class ExcelDeviceServoModel
    {
        public string 名称 { get; set; }
        public string 组名 { get; set; }
        public string 手动速度获取 { get; set; }
        public string 手动速度设置 { get; set; }
        public string 自动速度获取 { get; set; }
        public string 自动速度设置 { get; set; }
        public string 当前位置获取 { get; set; }
        public string 位置点动加 { get; set; }
        public string 位置点动减 { get; set; }
        public string 位置移动 { get; set; }

        public static List<DeviceServoModel> GetDatas(string excelPath = null, string sheetName = "设备伺服")
        {
            if (string.IsNullOrEmpty(excelPath))
                excelPath = LocalFile.PlcDotPath;

            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceServoModel>(excelPath, sheetName)
                .Where(o => !string.IsNullOrWhiteSpace(o.名称))
                .Select(o => new DeviceServoModel()
                {
                    Name = o.名称,
                    ExcelTag = o,
                }).ToList();
        }
    }

    /// <summary>
    /// 设备报警
    /// </summary>
    public class ExcelDeviceAlarmModel
    {
        public string 名称 { get; set; }
        public string 地址 { get; set; }
        public string 类型 { get; set; }
        public string 触发 { get; set; }
        public string 级别 { get; set; }
        public string 解决方式 { get; set; }

        public static List<ExcelDeviceAlarmModel> GetDatas(string excelPath = null, string sheetName = "设备报警")
        {
            if (string.IsNullOrEmpty(excelPath))
                excelPath = LocalFile.PlcDotPath;

            return MiniExcelLibs.MiniExcel.Query<ExcelDeviceAlarmModel>(excelPath, sheetName).Where(o => !string.IsNullOrWhiteSpace(o.名称)).ToList();
        }

        /// <summary>
        /// 是否触发警报
        /// </summary>
        public bool IsOnAlarm(object? val)
        {
            try
            {
                if (val == null)
                    return false;

                string vs = val.ToString().ToLower();
                if (类型.Trim().ToLower().Contains("bool"))
                {
                    if (vs == 触发.Trim().ToLower())
                        return true;
                    else
                        return false;
                }
                else if (类型.Trim().ToLower().Contains("int"))
                {
                    if (string.IsNullOrWhiteSpace(触发))
                        return false;
                    if (!触发.Contains(vs))
                        return false;

                    foreach (var item in 触发.Split(';'))
                    {
                        var kv = item.Split(':');
                        if (kv.Length != 2)
                            continue;

                        if (kv[0] == vs)
                            return true;
                    }
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }

    public class IotDevice
    {
        public static Action<IotDevice> UserChange;

        /// <summary>
        /// 控件名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 控件类型
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 控件值
        /// </summary>
        public object Val { get; set; }
        /// <summary>
        /// 功能
        /// </summary>
        public string Funct { get; set; }
        /// <summary>
        /// 操作方式（点动、切换）
        /// </summary>
        public string Mode { get; set; }
    }
}
