using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class DeviceCloneData
{
    public string name;
    public int floorId;
    public DeviceType type;
    public Vector3 postion;
}
public class DeviceObjManager : MonoBehaviour
{
    public static DeviceObjManager Instance;
    public GameObject Point;
    public List<DeviceCloneData> NowPointList = new List<DeviceCloneData>();

    private string objId = "objlistid";

    private void Awake()
    {
        Instance = this;
    }
    private void Update()
    {
        if (Input.GetKey(KeyCode.Q))
        {
            if (Input.GetKeyDown(KeyCode.Delete))
            {
                if (DevicePanelManager.Instance.MenuType == DeviceType.空)
                {
                    NowPointList.Clear();
                    SaveObjs();
                    return;
                }
                for (int i = 0; i < NowPointList.Count; i++)
                {
                    if (NowPointList[i].type == DevicePanelManager.Instance.MenuType)
                    {
                        NowPointList.RemoveAt(i);
                        i--;
                    }
                }
                SaveObjs();
            }
        }
    }
    private void Start()
    {
        LoadDeviceObjs();
    }
    public DeviceObj GetDeviceByName(string dname)
    {
        DeviceObj result = null;
        foreach (Transform item in transform)
        {
            if (item.name == dname)
            {
                result = item.GetComponent<DeviceObj>();
                break;
            }
        }
        return result;
    }
    public void AddObj(string objName)
    {
        foreach (Transform item in transform)
        {
            if (item.name == objName && item.GetComponent<DeviceObj>())
            {
                var obj = new DeviceCloneData();
                obj.name = item.name;
                obj.postion = item.position;
                obj.floorId = item.GetComponent<DeviceObj>().floorId;
                obj.type = item.GetComponent<DeviceObj>().BindType;
                bool has = false;
                foreach (var data in NowPointList)
                {
                    if (data.name == obj.name)
                    {
                        data.postion = obj.postion;
                        data.type = obj.type;
                    }
                }
                if (!has)
                {
                    NowPointList.Add(obj);
                }
                break;
            }
        }
        SaveObjs();
    }
    public void DelObj(string objName)
    {
        foreach (var item in DevicePanelManager.Instance.BindObjectList)
        {
            if (item.ObjName == objName)
            {
                item.ObjName = "";
                break;
            }
        }
        DevicePanelManager.Instance.SaveDeviceObjConfig();
        for (int i = 0; i < NowPointList.Count; i++)
        {
            if (NowPointList[i].name == objName)
            {
                NowPointList.Remove(NowPointList[i]);
                break;
            }
        }
        SaveObjs();
    }
    public void SaveObjs()
    {
        var json = JsonConvert.SerializeObject(NowPointList);
        string url = string.Format(HttpManager.Instance.PostBreakPointsById, objId);
        if (Application.platform == RuntimePlatform.WebGLPlayer)
        {
            url = url.Substring(url.IndexOf("api") - 1);
        }
        HttpManager.Instance.Post(url, json, () =>
        {
            Debug.Log("保存点位成功!");
        });
    }

    private void LoadDeviceObjs()
    {
        try
        {
            string url = string.Format(HttpManager.Instance.GetBreakPointsById, objId);
            Debug.Log($"the url is :{ url}");
            if (Application.platform == RuntimePlatform.WebGLPlayer)
            {
                url = url.Substring(url.IndexOf("api") - 1);
            }
            HttpManager.Instance.Get<string>(url, d =>
            {
                NowPointList = JsonConvert.DeserializeObject<List<DeviceCloneData>>(d);
                foreach (Transform item in transform)
                {
                    Destroy(item.gameObject);
                }
                foreach (var item in NowPointList)
                {
                    GameObject go = Instantiate(Point, item.postion, Quaternion.identity, transform);
                    go.GetComponent<DeviceObj>().CreateIcon(item.type, item.floorId);
                    go.name = item.name;
                    SelectionManager.Instance.Sets.Add(go);
                }
            });
        }
        catch
        {
            Debug.LogError("暂无数据!");
        }
    }
}