using IoT3D.Framework;
using MessagePack;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

public class MessageInfo
{
    [JsonProperty("topic")]
    public string Topic { get; set; }
    public string Message { get; set; }
}

public class TopicMessage
{
    public string SubscribeTopic { get; set; }
    public string Data { get; set; }
}

public class Message
{
    [JsonProperty("l")]
    public string Topic { get; set; }
    [JsonProperty("t")]
    public int TopicType { get; set; }
    [JsonProperty("d")]
    public string Data { get; set; }
}

public class EquipBaseData : INotifyPropertyChanged
{
    private int state;

    [JsonProperty("m_iEquipNo")]
    public int EquipNo { get; set; }
    [JsonProperty("m_EquipNm")]
    public string EquipName { get; set; }
    [JsonProperty("m_State")]
    public int State { get => state; set { state = value; RaisePropertyChanged(nameof(State)); } }
    [JsonProperty("m_Bufang")]
    public string Bufang { get; set; }
    [JsonProperty("m_related_video")]
    public string RelatedVideo { get; set; }
    [JsonProperty("m_related_pic")]
    public string RelatedPic { get; set; }
    [JsonProperty("m_ZiChanID")]
    public string ZiChanID { get; set; }
    [JsonProperty("m_PlanNo")]
    public string PlanNo { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class YcItemData : INotifyPropertyChanged
{
    public YcItemData()
    { }
    public YcItemData(myProxy_YCItem o)
    {
        EquipNo = o.m_iEquipNo;
        YcNo = o.m_iYCNo;
        YcName = o.m_YCNm;
        Unit = o.m_Unit;

       // UnityEngine.Debug.LogError(o.m_YCValue.GetType().Type);
        if (Double.TryParse(o.m_YCValue.ToString(), out double value))
        {
            YcValue.DoubleValue = value;
            //UnityEngine.Debug.LogError(YcValue.StringValue);
        }
        else
        {
            if (o.m_YCValue.GetType() == typeof(string) || o.m_YCValue.GetType() == typeof(DateTime))
            {
                YcValue.StringValue = o.m_YCValue.ToString();
                //UnityEngine.Debug.LogError(YcValue.StringValue);
            }
            else
            {
                YcValue = JsonConvert.DeserializeObject<RealTimeValue>(o.m_YCValue.ToString());
            }
        }

        IsAlarm = o.m_IsAlarm;
        AdviceMsg = o.m_AdviceMsg;
        HasHistoryCcurve = o.m_bHasHistoryCcurve;
        Bufang = o.m_Bufang;

        RelatedPic = o.related_pic;
        RelatedVideo = o.related_video;
        PlanNo = o.PlanNo;
        ZiChanID = o.ZiChanID;
    }


    [JsonProperty("m_iEquipNo")]
    public int EquipNo { get; set; }
    [JsonProperty("m_Bufang")]
    public bool Bufang { get; set; }
    [JsonProperty("m_iYCNo")]
    public int YcNo { get; set; }
    [JsonProperty("m_YCNm")]
    public string YcName { get; set; }
    [JsonProperty("m_YCValue")]
    private RealTimeValue ycValue = new RealTimeValue();
    private int equipState;

    [JsonProperty("m_Unit")]
    public string Unit { get; set; }
    [JsonProperty("m_related_video")]
    public string RelatedVideo { get; set; }
    [JsonProperty("m_related_pic")]
    public string RelatedPic { get; set; }
    [JsonProperty("m_ZiChanID")]
    public string ZiChanID { get; set; }
    [JsonProperty("m_PlanNo")]
    public string PlanNo { get; set; }
    [JsonProperty("m_IsAlarm")]
    public bool IsAlarm { get; set; }
    [JsonProperty("m_AdviceMsg")]
    public string AdviceMsg { get; set; }
    [JsonProperty("m_bHasHistoryCcurve")]
    public bool HasHistoryCcurve { get; set; }
    [JsonProperty("equipState")]
    public int EquipState 
    { 
        get => equipState; 
        set
        { 
            if(value!=equipState)
            {
                equipState = value;
                RaisePropertyChanged(nameof(EquipState));
            }
        }
    }
    public RealTimeValue YcValue { get => ycValue; set { ycValue = value; RaisePropertyChanged(nameof(YcValue)); } }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class YxItemData : INotifyPropertyChanged
{
    public YxItemData()
    { }
    public YxItemData(myProxy_YXItem o)
    {
        EquipNo = o.m_iEquipNo;
        YxNo = o.m_iYXNo;
        YxName = o.m_YXNm;

        if (bool.TryParse(o.m_YXValue.ToString(), out bool value))
        {
            YxValue.Temp = value ? 1 : -1;
            YxValue.BoolValue = value;
        }
        else
        {
            if (o.m_YXValue.GetType() == typeof(string))
            {
                YxValue.StringValue = o.m_YXValue.ToString();
            }
            else
            {
                YxValue = JsonConvert.DeserializeObject<RealTimeValue>(o.m_YXValue.ToString());
            }
        }
        State = o.m_YXState;
        IsAlarm.Temp = Convert.ToBoolean(o.m_IsAlarm) == true ? 1 : -1;
        AdviceMsg = "";
        Bufang = o.m_Bufang;
        HasHistoryCcurve = o.m_bHasHistoryXcurve;
        RelatedPic = o.related_pic;
        RelatedVideo = o.related_video;
        PlanNo = o.PlanNo;
        ZiChanID = o.ZiChanID;
    }
    [JsonProperty("m_iEquipNo")]
    public int EquipNo { get; set; }
    [JsonProperty("m_Bufang")]
    public bool Bufang { get; set; }
    [JsonProperty("m_iYXNo")]
    public int YxNo { get; set; }
    [JsonProperty("m_YXNm")]
    public string YxName { get; set; }

    private RealTimeValue yxValue = new RealTimeValue();
    [JsonProperty("m_related_video")]
    public string RelatedVideo { get; set; }
    [JsonProperty("m_related_pic")]
    public string RelatedPic { get; set; }
    [JsonProperty("m_ZiChanID")]
    public string ZiChanID { get; set; }
    [JsonProperty("m_PlanNo")]
    public string PlanNo { get; set; }
    [JsonProperty("m_IsAlarm")]
    public YXAlarm IsAlarm = new YXAlarm();
    private string state;
    private int equipState;

    [JsonProperty("m_AdviceMsg")]
    public string AdviceMsg { get; set; }
    [JsonProperty("m_bHasHistoryXcurve")]
    public bool HasHistoryCcurve { get; set; }
    [JsonProperty("equipState")]
    public int EquipState 
    { 
        get => equipState; 
        set 
        {
            if (value != equipState)
            {
                equipState = value;
                RaisePropertyChanged(nameof(EquipState));
            }
        } 
    }
    [JsonProperty("m_YXState")]
    public string State { get => state; set { state = value; RaisePropertyChanged(nameof(State)); } }
    [JsonProperty("m_YXValue")]
    public RealTimeValue YxValue { get => yxValue; set { yxValue = value; RaisePropertyChanged(nameof(YxValue)); } }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class RealTimeEventMessage
{
    [JsonProperty("Related_video")]
    public string RelatedVideo { get; set; }
    [JsonProperty("ZiChanID")]
    public string ZiChanID { get; set; }
    [JsonProperty("PlanNo")]
    public string PlanNo { get; set; }
    [JsonProperty("User_confirm")]
    public string UserConfirm { get; set; }
    [JsonProperty("bConfirmed")]
    public bool IsConfirmed { get; set; }
    [JsonProperty("Time")]
    public DateTime Time { get; set; }
    [JsonProperty("Ycyxno")]
    public int YcYxNo { get; set; }
    [JsonProperty("Type")]
    public string Type { get; set; }
    [JsonProperty("Proc_advice_Msg")]
    public string ProcAdviceMsg { get; set; }
    [JsonProperty("Wavefile")]
    public string Wavefile { get; set; }
    [JsonProperty("Related_pic")]
    public string RelatedPic { get; set; }
    [JsonProperty("DT_Confirm")]
    public DateTime ConfirmTime { get; set; }
    [JsonProperty("EventMsg")]
    public string EventMsg { get; set; }
    [JsonProperty("Level")]
    public int Level { get; set; }
    [JsonProperty("Equipno")]
    public int EquipNo { get; set; }
    [JsonProperty("GUID")]
    public string Guid { get; set; }
}

public class SetEventMessage
{
    [JsonProperty("bRecord")]
    public bool IsRecord { get; set; }
    [JsonProperty("bCanRepeat")]
    public bool IsCanRepeat { get; set; }
    [JsonProperty("bShowDlg")]
    public bool IsShowDlg { get; set; }
    [JsonProperty("Client_Instance_GUID")]
    public string ClientInstanceGUID { get; set; }
    public string Description { get; set; }
    public bool IsCj { get; set; }
    public bool IsWaitSetParm { get; set; }
    [JsonProperty("isQRTrigger")]
    public bool IsQRTrigger { get; set; }
    public string RequestId { get; set; }
    [JsonProperty("m_SetNo")]
    public int SetNo { get; set; }
    [JsonProperty("CJ_EqpNo")]
    public int CJEquipNo { get; set; }
    [JsonProperty("CJ_SetNo")]
    public int CJSetNo { get; set; }
    [JsonProperty("sysExecutor")]
    public string SysExecutor { get; set; }
    [JsonProperty("bOnlyDelayType")]
    public bool OnlyDelayType { get; set; }
    [JsonProperty("iDelayTime")]
    public int DelayTime { get; set; }
    public string UserIPandPort { get; set; }
    public string Value { get; set; }
    public string Type { get; set; }
    public string MinorInstruct { get; set; }
    public string MainInstruct { get; set; }
    public int StartTickCount { get; set; }
    public int WaitingTime { get; set; }
    public int EquipNo { get; set; }
    public string Executor { get; set; }
    [JsonProperty("csReserve1")]
    public string Reserve1 { get; set; }
    [JsonProperty("csReserve2")]
    public string Reserve2 { get; set; }
    [JsonProperty("csReserve3")]
    public string Reserve3 { get; set; }
    public bool? WaitSetParmIsFinish { get; set; }
    [JsonProperty("bStopSetParm")]
    public bool StopSetParm { get; set; }
}

public class RealTimeValue: INotifyPropertyChanged
{
    private string stringValue;
    private double? doubleValue;
    private int? temp;

    [JsonProperty("s")]
    public string StringValue { get => stringValue; set { stringValue = value; RaisePropertyChanged(nameof(StringValue)); } }
    [JsonProperty("f")]
    public double? DoubleValue { get => doubleValue; set { doubleValue = value; RaisePropertyChanged(nameof(DoubleValue)); } }
    [JsonProperty("temp")]
    public int? Temp { get => temp; set { temp = value; RaisePropertyChanged(nameof(Temp)); } }
    [JsonProperty("b")]
    public bool BoolValue { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class YXAlarm
{
    [JsonProperty("temp")]
    public int Temp = 0;//-1Ϊfalse1Ϊtrue
    [JsonProperty("b")]
    public bool B = true;
}
public class EquipStateData
{
    [JsonProperty("m_iEquipNo")]
    public int EquipNo { get; set; }
    [JsonProperty("m_State")]
    public EquipState State { get; set; }
}

public class EquipDataItem
{
    //public EquipState equipState { get; set; }
    //public EquipState ycItemDict { get; set; }
    //public EquipState yxItemDict { get; set; }
    //public EquipState setItemDict { get; set; }
}
public class EquipSetItem
{

}
public class YxpItem
{
    /// <summary>
    /// վ
    /// </summary>
    public int StaN { get; set; }

    /// <summary>
    /// 豸
    /// </summary>
    public int EquipNo { get; set; }

    /// <summary>
    /// ңź
    /// </summary>
    public int YxNo { get; set; }

    /// <summary>
    /// ң	
    /// </summary>
    public string YxNm { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string ProcAdvice { get; set; }

    /// <summary>
    /// ҳ
    /// </summary>
    public string RelatedPic { get; set; }

    /// <summary>
    /// Ƶ
    /// </summary>
    public string RelatedVideo { get; set; }

    /// <summary>
    /// ʲ
    /// </summary>
    public string ZiChanID { get; set; }
    public string ZiChanName { get; set; }

    /// <summary>
    /// Ԥ
    /// </summary>
    public string PlanNo { get; set; }

    /// <summary>
    /// ʵʱ״̬
    /// </summary>
    /// 
    public bool State { get; set; }

    /// <summary>
    /// ʵʱֵ
    /// </summary>
    /// 
    public string Value { get; set; }

}
public class YcpItem
{
    /// <summary>
    /// վ
    /// </summary>
    public int StaN { get; set; }

    /// <summary>
    /// 豸
    /// </summary>
    public int EquipNo { get; set; }

    /// <summary>
    /// ң
    /// </summary>
    public int YcNo { get; set; }

    /// <summary>
    /// ң	
    /// </summary>
    public string YcNm { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public string ProcAdvice { get; set; }

    /// <summary>
    /// ҳ
    /// </summary>
    public string RelatedPic { get; set; }

    /// <summary>
    /// Ƶ
    /// </summary>
    public string RelatedVideo { get; set; }

    /// <summary>
    /// ʲ
    /// </summary>
    public string ZiChanID { get; set; }

    public string ZiChanName { get; set; }

    /// <summary>
    /// Ԥ
    /// </summary>
    public string PlanNo { get; set; }

    /// <summary>
    /// ʵʱ״̬
    /// </summary>
    public bool State { get; set; }

    /// <summary>
    /// ʵʱֵ
    /// </summary>
    /// 
    public string Value { get; set; }

    /// <summary>
    /// ʵʱֵ
    /// </summary>
    /// 
    public string Unit { get; set; }

}
public class SetParm
{
    [JsonProperty("sta_n")]
    public int StaN { get; set; }
    [JsonProperty("equip_no")]
    public int EquipNo { get; set; }
    [JsonProperty("set_no")]
    public int SetNo { get; set; }
    [JsonProperty("set_nm")]
    public string SetNm { get; set; }
    [JsonProperty("set_type")]
    public string SetType { get; set; }
}
public class YcpDataTable
{
    public int equip_no { get; set; }
    public int yc_no { get; set; }
    public string yc_nm { get; set; }
    public string Reserve1 { get; set; }
    public string Reserve2 { get; set; }
    public string Reserve3 { get; set; }
}
public class EquipSetParm
{
    public int sta_n { get; set; }
    public int equip_no { get; set; }
    public int set_no { get; set; }
    public string set_nm { get; set; }
    public string set_type { get; set; }
    public string main_instruction { get; set; }
    public string minor_instruction { get; set; }
    public string record { get; set; }
    public string action { get; set; }
    public string value { get; set; }
    public string canexecution { get; set; }

    public string voiceKeys { get; set; }
    public string enableVoice { get; set; }
    public int qr_equip_no { get; set; }
    public string reserve1 { get; set; }
    public string reserve2 { get; set; }
    public string reserve3 { get; set; }
}
public class MyCurveCommonData
{
    public DateTime Datetime { get; set; }
    public double Value { get; set; }
}

public struct myCurveData
{
    public DateTime datetime;
    public double value;
    public CurveDataState state;
}

public enum CurveDataState
{
    Normal = 1,
    NoRecord = 2
}
public struct mySeriesPoint
{
    public double datetime;
    public byte mySeriesPointType;
    public double value;
}