using Blue;
using LitJson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;

public class InspectionService : IInspectionService
{
    public InspectionInfo InspectionInfo { get; private set; }

    public void OnInit()
    {

    }

    public void Get()
    {
        CoroutineSystem.Instance.StartCoroutine(
            HttpTool.Instance.SendHttp(HttpActionLang.inspection_begin, "", message =>
            {
                JObject jobject = JObject.Parse(message);
                if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
                {
                    Debug.LogError("data:" + jobject["data"].ToString());
                    InspectionInfo = JsonConvert.DeserializeObject<InspectionInfo>(jobject["data"].ToString());
                    /*
                    Debug.LogError($"ID:{InspectionInfo.id}");
                    foreach (var item in InspectionInfo.items)
                    {
                        Debug.LogError($"key:{item.Key},Value:{item.Value}");
                    }
                    Debug.LogError($"Steps:{InspectionInfo.steps.Count}");
                    foreach (var item in InspectionInfo.steps)
                    {
                        Debug.LogError($"key:{item.Key}");
                        Debug.LogError($"normal:{item.Value.normal}");
                        Debug.LogError($"lable:{item.Value.label}");
                        Debug.LogError($"images:{item.Value.images}");
                    }
                    */
                }
            }));
    }

    public void Set(int ItemID,bool Normal,string Label,int InspectionId)
    {
        JsonData data = new JsonData();
        data["itemId"] = ItemID;
        data["normal"] = Normal;
        data["label"] = Label;
        data["inspectionId"] = InspectionId;
        CoroutineSystem.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.inspection_step, data.ToJson(), message =>
        {
            JObject jobject = JObject.Parse(message);
            if (jobject["code"].ToString() == "200" && !string.IsNullOrWhiteSpace(jobject["data"].ToString()))
            {
                //Debug.LogError($"Message:{message}+finished:{jobject["data"]["finished"]}");
            }
        }));
    }
}