using AssistClient.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AssistClient
{
    public static class WebApi
    {
        private static bool debug=false;
        /// <summary>
        /// date,sn
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static JObject getDefectFromSN(JObject req)
        {
            if (debug)
            {
                //JObject data = new JObject()
                //{
                //    {$"Defect_{sn}_1_X111_Y222",JArray.Parse(File.ReadAllText(@"d:\\test.json")) },
                //};
                //return new JObject()
                //{
                //    {"code",200},
                //    {"data",data},
                //};

            }  
             
            JObject resp = HttpUtil.postSync($"http://{Config.ServerIP}:{Config.ServerPort}" + "/api/get_defect_from_sn", req.ToString());
            if (!resp.Value<bool>("success"))//框架库内
                throw new Exception(resp.Value<string>("data"));//框架库内

            //成功接收返回
            JObject obj = JObject.Parse(resp.Value<string>("data"));
            //if (obj.Value<int>("code") != 200)
            //    throw new Exception(resp.Value<string>("data"));

            return obj;
        }
        /// <summary>
        /// date,sn,file_name
        /// </summary>
        /// <param name="file_name"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static JObject getDefectBmpBase64(JObject req)
        {
            if (debug)
            { 
                //return new JObject()
                //{
                //    {"code",200},
                //    {"data",Convert.ToBase64String(File.ReadAllBytes(Config.Defect_SavePath+"\\"+ file_name))},
                //};

            } 

            JObject resp = HttpUtil.postSync($"http://{Config.ServerIP}:{Config.ServerPort}" + "/api/get_defect_pic", req.ToString());
            if (!resp.Value<bool>("success"))//框架库内
                throw new Exception(resp.Value<string>("data"));//框架库内

            //成功接收返回
            JObject obj = JObject.Parse(resp.Value<string>("data"));
            //if (obj.Value<int>("code") != 200)
            //    throw new Exception(resp.Value<string>("data"));

            return obj;
        }
    }
}
