using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine;
using static ENVQuery;

/// <summary>
/// 设备-查询
/// </summary>
[AddComponentMenu("设备查询"), Serializable]
public class DeviceQuery : MonoBehaviour
{

    public DeviceList deviceList;

    

    private void Awake()
    {

    }

    private async void Start()
    {
        //await initAsync("");
    }

    public async Task initAsync(string token)
    {
        var jsonResult = await CombineJSON.GetJson_POST("https://jsonplaceholder.typicode.com/posts",token);

        Root root = JsonConvert.DeserializeObject<Root>(jsonResult);

        foreach (var item in root.data)
        {
            if (item.id == deviceList.id)
            {
                deviceList = item;
                break;
            }
        }
    }

    [ContextMenu("添加板卡、端口信息脚本")]
    public void addscript()
    {
        //添加端口信息脚本
        for (int i = 0; i < transform.childCount; i++)
        {
            var go = transform.GetChild(i);

            if (deviceList.type == 2 && deviceList.deviceType == 3)
            {
                if (!GetComponent<TmsCardQuery>())
                    gameObject.AddComponent<TmsCardQuery>();
                if (!go.GetComponent<PortQuery>())
                    go.AddComponent<PortQuery>();
            }
        }
    }

    #region JSON
    [Serializable]
    public class DeviceList
    {
        /// <summary>
        /// id
        /// </summary>
        [Tooltip("")] public string id;
        /// <summary>
        /// 名称
        /// </summary>
        [Tooltip("名称")] public string deviceName;
        /// <summary>
        /// 类型：1-机柜；2-设备
        /// </summary>
        [Tooltip("类型：1-机柜；2-设备")] public int type;
        /// <summary>
        /// 类型
        /// </summary>
        [Tooltip("类型")] public string typeStr;
        /// <summary>
        /// 投运时间
        /// </summary>
        [Tooltip("")] public int operationTime;
        /// <summary>
        /// 状态：1-正常；2-禁用；3-锁定
        /// </summary>
        [Tooltip("状态：1-正常；2-禁用；3-锁定")] public int status;
        /// <summary>
        /// 负责人
        /// </summary>
        [Tooltip("")] public string directorName;
        /// <summary>
        /// 手机号
        /// </summary>
        [Tooltip("")] public string phone;
        /// <summary>
        /// 机柜型号
        /// </summary>
        [Tooltip("")] public string machineModel;
        /// <summary>
        /// 机柜位置
        /// </summary>
        [Tooltip("")] public string machinePosition;
        /// <summary>
        /// 生产厂家
        /// </summary>
        [Tooltip("")] public string manufacturer;
        /// <summary>
        /// 电源属性
        /// </summary>
        [Tooltip("")] public string powerProperties;
        /// <summary>
        /// 
        /// </summary>
        [Tooltip("")] public string maintenanceUnit;
        /// <summary>
        /// 设备类型：1-机框；2-机槽；3-板卡
        /// </summary>
        [Tooltip("设备类型：1-机框；2-机槽；3-板卡")] public int deviceType;
        /// <summary>
        /// 
        /// </summary>
        [Tooltip("")] public string deviceTypeStr;
        /// <summary>
        /// 设备编号
        /// </summary>
        [Tooltip("设备编号")] public string deviceCode;
        /// <summary>
        /// 机框高度
        /// </summary>
        [Tooltip("")] public string machineFrameHigh;
        /// <summary>
        /// 机框宽度
        /// </summary>
        [Tooltip("")] public string machineFrameWide;
        /// <summary>
        /// 机框厚度
        /// </summary>
        [Tooltip("")] public string machineFrameThick;
        /// <summary>
        /// 插槽数量
        /// </summary>
        [Tooltip("插槽数量")] public int slotNum;
        /// <summary>
        /// 插槽排列方式
        /// </summary>
        [Tooltip("")] public string slotSort;
        /// <summary>
        /// 安装方式
        /// </summary>
        [Tooltip("")] public string installMethod;
        /// <summary>
        /// 所属机框
        /// </summary>
        [Tooltip("所属机框")] public string affiliationFrame;
        /// <summary>
        /// 父插槽名称
        /// </summary>
        [Tooltip("父插槽名称")] public string parentSlotName;
        /// <summary>
        /// 插槽类型
        /// </summary>
        [Tooltip("插槽类型")] public string slotType;
        /// <summary>
        /// 占用状态
        /// </summary>
        [Tooltip("")] public string occupyStatus;
        /// <summary>
        /// 所属机槽名称
        /// </summary>
        [Tooltip("所属机槽名称")] public string affiliationSlot;
        /// <summary>
        /// 端口数量
        /// </summary>
        [Tooltip("端口数量")] public int portNum;
        /// <summary>
        /// 板卡功能
        /// </summary>
        [Tooltip("")] public string cardFunction;
        /// <summary>
        /// 板卡类型
        /// </summary>
        [Tooltip("")] public string cardType;
        /// <summary>
        /// 运行状态
        /// </summary>
        [Tooltip("")] public string runStatus;
        /// <summary>
        /// 
        /// </summary>
        [Tooltip("")] public string tmsId;
        /// <summary>
        /// 备注
        /// </summary>
        [Tooltip("")] public string remark;
        /// <summary>
        /// 
        /// </summary>
        [Tooltip("")] public int createTime;
        /// <summary>
        /// 
        /// </summary>
        [Tooltip("")] public string createName;

    }


    [Serializable]
    public class Root
    {
        /// <summary>
        /// 
        /// </summary>
        public string code;
        /// <summary>
        /// 操作成功
        /// </summary>
        public string message;
        /// <summary>
        /// 
        /// </summary>
        public List<DeviceList> data;
        /// <summary>
        /// 
        /// </summary>
        public string serverTime;
    }
    #endregion
}
