using System;
using System.Collections.Generic;
using UnityEngine;
using WS;

public static class HttpCom
{
    public static Dictionary<string, string> comData = new Dictionary<string, string>();
    /// <summary>VR课程授权</summary>
    public static void GetCommandLine()
    {
        return;
        string CommandLine = Environment.CommandLine;
        //CommandLine = "copro://taskId=&courseId=91&course=B737-300 H Training&model=T&token=fySg1CSYMzUy020dSM%2BY14slnijap9XDcM340dYICrw%3D";
        string data = CommandLine.Replace("copro://", "");
        string[] keyValuePairs = data.Split('&');
        foreach (string pair in keyValuePairs)
        {
            string[] parts = pair.Split('=');
            if (parts.Length == 2)
            {
                string key = parts[0];
                string value = parts[1];
                comData[key] = value;
                //Debug.Log(key + "   " + value);
            }
        }
        Debug.Log(DateTime.Now.ToString());
        RequestComponent.Get<string>(GameManager.Instance.Data.httpBase + "?token=" + comData["token"], VRRoot);

    }

    private static void VRRoot(ResponseOutput<string> arg0)
    {
        if (arg0.code == 0)
        {
            GameManager.Instance.Data.User.UserName = arg0.um;
        }
        else
        {
            Debug.LogError($"code:{arg0.code}  {arg0.msg}");
            //Application.Quit();
        }
    }


    public static void UploadGrades()
    {
        return;
        var grades = new Grades();
        grades.m = "api";
        grades.md = comData["model"];
        grades.course = int.Parse(comData["comData"]);
        grades.branch = 1;
        grades.data = new CourseResultData();
        grades.data.ST = GameManager.Instance.ProcessManager.ST.ToString();
        grades.data.ET = GameManager.Instance.ProcessManager.ET.ToString();
        TimeSpan timeDifference = GameManager.Instance.ProcessManager.ET - GameManager.Instance.ProcessManager.ST;
        long millisecondsDifference = (long)timeDifference.TotalMilliseconds;
        grades.data.TT = millisecondsDifference.ToString();
        grades.token = comData["token"];
        if (grades.md == "E")
        {
            var taskData = GameManager.Instance.ProcessManager.GetAllVTaskRecord();
            float score = 0;
            for (int i = 0; i < taskData.Count; i++)
            {
                if (!taskData[i].IsError)
                {
                    if (i == 0)
                    {
                        grades.data.detail = taskData[i].Score.ToString();
                    }
                    else if (i == taskData.Count - 1)
                    {
                        grades.data.detail += taskData[i].Score.ToString();
                    }
                    else
                    {
                        grades.data.detail += taskData[i].Score.ToString() + "/";
                    }
                    score += taskData[i].Score;
                }
                else
                {
                    if (i == 0)
                    {
                        grades.data.detail = "0";
                    }
                    else if (i == taskData.Count - 1)
                    {
                        grades.data.detail += "0";
                    }
                    else
                    {
                        grades.data.detail += "0" + "/";
                    }
                }
            }
            grades.data.score = score.ToString();
        }
        RequestComponent.Post<string>(GameManager.Instance.Data.httpBase + "?XXXX", LitJson.JsonMapper.ToJson(grades), UpdataGradesResult);
    }

    private static void UpdataGradesResult(ResponseOutput<string> arg0)
    {
       
        if (arg0.code == 0)
        {
            Debug.Log(arg0.msg);
        }
        else if(arg0.code == 1000)
        {
            Debug.LogError(arg0.msg);
        }
        else if (arg0.code == 2000)
        {
            Debug.LogError(arg0.msg);
            UploadGrades();
        }
    }


}
public class Grades
{
    public string m { get; set; }
    public string md { get; set; }
    public int course { get; set; }
    public int branch { get; set; }
    public CourseResultData data { get; set; }
    public string token { get; set; }
}
public class CourseResultData
{
    public string ST { get; set; }
    public string ET { get; set; }
    public string TT { get; set; }
    public string score { get; set; }
    public string detail { get; set; }
}
