using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using SC.XR.Unity.Module_Device;
using Newtonsoft.Json;

public class HttpSocket : MonoSingleton<HttpSocket>
{
    /// <summary>
    ///  同步当前位置
    /// </summary>
    /// <param name="pos">位置</param>
    /// <param name="PointionTs">时间</param>
    /// <param name="CallBack"></param>
    public void SendIpToPointPos(Vector3 pos, Action<string> CallBack)
    {
        SendNetConnectPointCloud sendNet = new SendNetConnectPointCloud();

        sendNet.sn = SendSN.GetSN();
        //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;

        sendNet.projectId = DataManager.Instance.ProjectID;
        sendNet.position.x = pos.x;
        sendNet.position.y = pos.y;
        sendNet.position.z = pos.z;
        sendNet.positionType = "PointCloud";
        string jsonData = JsonConvert.SerializeObject(sendNet);
        //Debug.Log("/sn/position"+jsonData);

        HttpTool.Instance.Post("/sn/position", jsonData, CallBack);
    }



    /// <summary>
    /// 向服务器发送眼镜的剩余电量以及电池状态
    /// 在start函数中使用InvokeRepeating60秒调用一次
    /// </summary>
    /// <param name="CallBack"></param>
    public void SendIpToElectric()
    {
        StartCoroutine(WaitSendElectric());
    }
    IEnumerator WaitSendElectric()
    {
        while (true)
        {
            CalElectric();
            yield return new WaitForSeconds(60f);
        }
    }
    private void CalElectric()
    {
        int electricity = (int)((SystemInfo.batteryLevel) * 100f);
        BatteryStatus batterystate = SystemInfo.batteryStatus;
        string state = "";
        switch (batterystate)
        {
            case BatteryStatus.Unknown:
                state = "无法确定设备的电池状态";
                break;
            case BatteryStatus.Charging:
                state = "设备已插入并正在充电";
                break;
            case BatteryStatus.Discharging:
                state = "设备已拔出并放电";
                break;
            case BatteryStatus.NotCharging:
                state = "设备已插入,但无法充电";
                break;
            case BatteryStatus.Full:
                state = "设备已插入并且电池已充满";
                break;
            default:
                state = "无法确定设备的电池状态";
                break;
        }
        SendNetConnectElectricity sendNet = new SendNetConnectElectricity();

        sendNet.sn = SendSN.GetSN();
        //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;

        sendNet.projectId = DataManager.Instance.ProjectID;
        electricity = 100;
        sendNet.electricity = electricity;
        sendNet.status = state;
        string jsonData = JsonMapper.ToJson(sendNet);
        Debug.Log(jsonData);
        HttpTool.Instance.Post("/sn/electricity", jsonData, CallBack);
    }


    /// <summary>
    ///  进入触发区域后向后台发送信息
    /// </summary>
    /// <param name="pointId"></param>
    public void SendIpToPointTrigger(string pointId, string name, Action<string> CallBack)
    {
        SendNetConnectPointTrigger sendNet = new SendNetConnectPointTrigger();

        sendNet.sn =SendSN.GetSN();
        //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;

        sendNet.projectId = DataManager.Instance.ProjectID;
        sendNet.name = name;
        string jsonData = JsonMapper.ToJson(sendNet);

        Debug.Log(jsonData);
        HttpTool.Instance.Post("/sn/viewpoint", jsonData, CallBack);
    }


    public void SendSnInfo(SceneValue scene, int index, Action<int, string> CallBack)
    {
        SendSnInfo sendNet = new SendSnInfo();

        sendNet.sn =SendSN.GetSN();
        //sendNet.sn = API_GSXR_Slam.SlamManager.plugin.SN;

        sendNet.projectId = scene.id;
        string jsonData = JsonMapper.ToJson(sendNet);

        Debug.Log(jsonData);
        HttpTool.Instance.PostSnInfo("/client/snInfo", jsonData, index, CallBack);
    }


    private void CallBack(string message)
    {
        Debug.Log(message);
    }
}


public class SendNetConnectPointCloud
{

    public string sn { get; set; }
    public int projectId { get; set; }

    public string positionType { get; set; }

    public Position position { get; set; }

    public SendNetConnectPointCloud()
    {
        position = new Position();
    }
}

public class Position
{


    /// <summary>
    /// 
    /// </summary>
    public double x { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public double y { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public double z { get; set; }
}

public class SendNetConnectElectricity
{
    public string sn { get; set; }
    public int projectId { get; set; }
    public int electricity { get; set; }
    public string status { get; set; }
}

public class SendNetConnectPointTrigger
{
    public int projectId { get; set; }
    public string sn { get; set; }
    public string name { get; set; }
}

public class SendSnInfo
{
    public string sn { get; set; }
    public int projectId { get; set; }
}