using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataVAPI.Tool.IOT
{
    /// <summary>
    /// IOT Model
    /// </summary>
    public class IotModel<T>
    {
        public string id { get; set; } = Guid.NewGuid().ToString();
        public string version { get; set; } = "1.0";
        public T @params { get; set; }
        public string method { get; set; } = "thing.event.property.post";
    }
    /// <summary>
    /// IOT 接收数据Model
    /// </summary>
    public class ReceiveModel
    {
        /// <summary>
        /// 设备详细信息
        /// </summary>
        public DevBase deviceContext { get; set; }
        /// <summary>
        /// 设备属性
        /// </summary>
        public ReceiveSXModel props { get; set; }
        /// <summary>
        /// 设备上下线状态
        /// </summary>
        public Vls status { get; set; }

    }
    /// <summary>
    /// 接收数据Model
    /// </summary>
    public class ReceiveSXModel
    {
        /// <summary>
        /// 告警消息
        /// </summary>
        public Vls GJXX { get; set; }
        /// <summary>
        /// 扩展属性
        /// </summary>
        public Vls KZSX { get; set; }
        /// <summary>
        /// 基本属性
        /// </summary>
        public Vls JBSX { get; set; }
        /// <summary>
        /// 节点状态
        /// </summary>
        public Vls NodeStatus { get; set; }
        /// <summary>
        /// 日志消息
        /// </summary>
        public Vls SZXX { get; set; }
    }
    /// <summary>
    /// IOT 设备属性 Model
    /// </summary>
    public class IOTDevSXModel
    {
        /// <summary>
        /// 设备一状态
        /// </summary>
        public string Device1 { get; set; }
        /// <summary>
        /// 设备二状态
        /// </summary>
        public string Device2 { get; set; }
        /// <summary>
        /// 设备三状态
        /// </summary>
        public string Device3 { get; set; }
        /// <summary>
        /// 节点状态
        /// </summary>
        public string NodeStatus { get; set; }
        ///// <summary>
        ///// 运行日志
        ///// </summary>
        //public string SZXX { get; set; }
        ///// <summary>
        ///// 运行告警
        ///// </summary>
        //public string GJXX { get; set; }
        /// <summary>
        /// Model
        /// </summary>
        public IOTDevSXModel()
        {

        }
        /// <summary>
        /// 序列化为JSON
        /// </summary>
        /// <returns></returns>
        public string Tojson()
        {
            try
            {
                IotModel<IOTDevSXModel> iotModel = new IotModel<IOTDevSXModel> { @params = this };
                string json = Tools.JsonConvertTools(iotModel);
                return json;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
        ///// <summary>
        ///// 设置基本属性
        ///// </summary>
        ///// <param name="devSX"></param>
        ///// <returns></returns>
        //public void SetJBSX(DevSX devSX)
        //{
        //    try
        //    {
        //        JBSX = Tools.JsonConvertTools(devSX);
        //    }
        //    catch (Exception ex)
        //    {
        //        JBSX = string.Empty;
        //    }
        //}
        ///// <summary>
        ///// 设置基本属性状态
        ///// </summary>
        ///// <param name="devSX"></param>
        ///// <returns></returns>
        //public void SetJBSXStatus(DevSXBase sXBase, bool Status)
        //{
        //    try
        //    {
        //        if (sXBase == null) return;
        //        DevSX dev = Tools.JsonToObjectTools<DevSX>(JBSX);
        //        dev.data?.ForEach(x =>
        //        {
        //            if (x.SXMC == sXBase.SXMC && x.SXLX == sXBase.SXLX)
        //            {
        //                x.SXStatus = Status;
        //            }
        //        });
        //        JBSX = Tools.JsonConvertTools(dev);
        //    }
        //    catch (Exception ex)
        //    {
        //        JBSX = string.Empty;
        //    }
        //}
        ///// <summary>
        ///// 设置扩展属性
        ///// </summary>
        ///// <param name="devSX"></param>
        //public void SetKZSX(DevSX devSX)
        //{
        //    try
        //    {
        //        KZSX = Tools.JsonConvertTools(devSX);
        //    }
        //    catch (Exception ex)
        //    {
        //        KZSX = string.Empty;
        //    }
        //}
        ///// <summary>
        ///// 设置扩展属性状态
        ///// </summary>
        ///// <param name="devSX"></param>
        ///// <returns></returns>
        //public void SetKZSXStatus(DevSXBase sXBase, bool Status)
        //{
        //    try
        //    {
        //        if (sXBase == null) return;
        //        DevSX dev = Tools.JsonToObjectTools<DevSX>(KZSX);
        //        dev.data?.ForEach(x =>
        //        {
        //            if (x.SXMC == sXBase.SXMC && x.SXLX == sXBase.SXLX)
        //            {
        //                x.SXStatus = Status;
        //            }
        //        });
        //        KZSX = Tools.JsonConvertTools(dev);
        //    }
        //    catch (Exception ex)
        //    {
        //        KZSX = string.Empty;
        //    }
        //}
    }
    /// <summary>
    /// 告警消息
    /// </summary>
    public class AlarmMessage
    {
        public List<AlarmModel> data { get; set; }
    }
    /// <summary>
    /// 告警Model
    /// </summary>
    public class AlarmModel : DeviceBase
    {
        /// <summary>
        /// 告警程度：提示 一般 严重
        /// </summary>
        public string AlarmCD
        {
            get { return _AlarmCD; }
            set
            {
                _AlarmCD = value;
                if (_AlarmCD == "提示") DeviceColor = new ALYColor { r = 13, g = 254, b = 73, a = 1 };
                else if (_AlarmCD == "一般") DeviceColor = new ALYColor { r = 245, g = 216, b = 13, a = 1 };
                else if (_AlarmCD == "严重") DeviceColor = new ALYColor { r = 245, g = 13, b = 13, a = 1 };
            }
        }
        private string _AlarmCD { get; set; }
        /// <summary>
        /// 颜色
        /// </summary>
        public ALYColor DeviceColor { get; set; }
    }
    /// <summary>
    /// 设备上报属性
    /// </summary>
    public class DevSX
    {
        public List<DevSXBase> data { get; set; }
    }
    /// <summary>
    /// 设备基本属性Model
    /// </summary>
    public class DevSXBase
    {
        /// <summary>
        /// 属性名称
        /// </summary>
        public string SXMC { get; set; }
        /// <summary>
        /// 属性类型
        /// </summary>
        public string SXLX { get; set; }
        /// <summary>
        /// 属性状态
        /// </summary>
        private bool _SXStatus { get; set; }
        public bool SXStatus
        {
            get { return _SXStatus; }
            set
            {
                _SXStatus = value;
                if (_SXStatus)
                {
                    SXZT = "正常";
                }
                else
                {
                    SXZT = "异常";
                }
            }
        }
        private string _SXZT { get; set; }
        /// <summary>
        /// 属性状态：正常 异常
        /// </summary>
        public string SXZT
        {
            get { return _SXZT; }
            set
            {
                _SXZT = value;
                if (_SXZT == "正常")
                {
                    SXYC = new ALYColor { r = 13, g = 254, b = 73, a = 1 };//绿色
                    SXYCMS = "";
                    YCZT = "";
                    ButtonText = "";
                    ButtonYC = new ALYColor { r = 0, g = 0, b = 0, a = 0 };
                }
                else if (_SXZT == "异常")
                {
                    SXYC = new ALYColor { r = 245, g = 13, b = 13, a = 1 };//红色
                    ButtonText = "详情";
                    ButtonYC = new ALYColor { r = 48, g = 109, b = 201, a = 0.18 };
                }
            }
        }
        /// <summary>
        /// 属性异常描述
        /// </summary>
        public string SXYCMS { get; set; }
        public string YCZT { get; set; }
        /// <summary>
        /// 属性颜色
        /// </summary>
        public ALYColor SXYC { get; set; }
        /// <summary>
        /// 按钮文字
        /// </summary>
        public string ButtonText { get; set; }
        /// <summary>
        /// 按钮颜色
        /// </summary>
        public ALYColor ButtonYC { get; set; }
        public DevSXBase()
        {
            SXZT = "";
            SXYC = new ALYColor { r = 13, g = 254, b = 73, a = 1 };//绿色
            SXYCMS = "";
            YCZT = "";
            ButtonText = "";
            ButtonYC = new ALYColor { r = 0, g = 0, b = 0, a = 0 };
        }
    }

    /// <summary>
    /// 设备颜色
    /// </summary>
    public class ALYColor
    {
        public int r { get; set; }
        public int g { get; set; }
        public int b { get; set; }
        public double a { get; set; }
    }

    /// <summary>
    /// 变量
    /// </summary>
    public class Vls
    {
        public long time { get; set; }
        public string value { get; set; }
    }
}
