using Newtonsoft.Json.Linq;
using SufeiUtil;
using SXLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HttpHelper = SufeiUtil.HttpHelper;

namespace MOKA_Factory_Tools
{
    internal class SanhuaMethod
    {
        public static SanhuaMESLoginInfo sanhuaLoginInfo = new SanhuaMESLoginInfo();

        public static bool CheckLogin(out string message)
        {
            message = string.Empty;
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem() {URL = sanhuaLoginInfo.url + "api/MESTestCollect/CheckLogin", Encoding = Encoding.UTF8,Method = "post", PostEncoding = Encoding.UTF8, KeepAlive = false};
            item.ContentType = "application/json;charset=utf-8";
            item.Postdata = string.Format("{{\"password\":\"{0}\",\"resourceCode\":\"{1}\",\"staffCode\":\"{2}\"}}", sanhuaLoginInfo.password, sanhuaLoginInfo.resourceCode, sanhuaLoginInfo.staffCode);
            HttpResult result = http.GetHtml(item);
            if (result.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Log.WriteErrorLog(string.Format("CheckLogin System abnormality, staffCode={0} password={1} resourceCode={2}", sanhuaLoginInfo.staffCode, sanhuaLoginInfo.password, sanhuaLoginInfo.resourceCode));
                return false;
            }

            string ParseText = result.Html;
            Log.WriteInfoLog(string.Format("CheckLogin Html:{0}", result.Html));

            try
            {
                JObject jObject = JObject.Parse(ParseText);
                bool success = jObject["success"].Value<bool>();
                message = jObject["message"].Value<string>();

                if (!success)
                {
                    Log.WriteErrorLog("\r\nCheckLogin Error:" + result.StatusDescription + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
                    return false;
                }

                Log.WriteInfoLog("\r\nCheckLogin OK:" + result.StatusDescription + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
            }
            catch (Exception e)
            {
                message = e.Message;
                Log.WriteErrorLog("\r\nCheckLogin Error:" + e.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
                return false;
            }

            return true;
        }

        public static bool ProductRouteCheck(string productSN, out string message)
        {
            message = string.Empty;
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem() { URL = sanhuaLoginInfo.url + "api/MESTestCollect/ProductRouteCheck", Encoding = Encoding.UTF8, Method = "post", PostEncoding = Encoding.UTF8, KeepAlive = false };
            item.ContentType = "application/json;charset=utf-8";
            item.Postdata = string.Format("{{\"jigEdition\":\"{0}\",\"resourceCode\":\"{1}\",\"staffCode\":\"{2}\",\"productSN\":\"{3}\"}}", 
                sanhuaLoginInfo.jigEdtion, sanhuaLoginInfo.resourceCode, sanhuaLoginInfo.staffCode, productSN);
            HttpResult result = http.GetHtml(item);
            if (result.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Log.WriteErrorLog(string.Format("ProductRouteCheck System abnormality, productSN={0}", productSN));
                return false;
            }

            string ParseText = result.Html;

            try
            {
                JObject jObject = JObject.Parse(ParseText);
                bool success = jObject["success"].Value<bool>();
                message = jObject["message"].Value<string>();

                if (!success)
                {
                    Log.WriteErrorLog("\r\nProductRouteCheck Error:" + result.StatusDescription + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
                    return false;
                }

                Log.WriteInfoLog("\r\nProductRouteCheck OK:" + result.StatusDescription + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
            }
            catch (Exception e)
            {
                message = e.Message;
                Log.WriteErrorLog("\r\nProductRouteCheck Error:" + e.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
                return false;
            }

            return true;
        }

        public static bool UploadIdcData(string productSN, string acasId, out string message)
        {
            message = string.Empty;
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem() { URL = sanhuaLoginInfo.url + "api/MESTestCollect/UploadIdcData", Encoding = Encoding.UTF8, Method = "post", PostEncoding = Encoding.UTF8, KeepAlive = false };
            item.ContentType = "application/json;charset=utf-8";
            item.Postdata = string.Format("{{\"jigEdition\":\"{0}\",\"resourceCode\":\"{1}\",\"staffCode\":\"{2}\",\"productSN\":\"{3}\",\"attr\":{{\"ACASID\":\"{4}\"}}}}",
                sanhuaLoginInfo.jigEdtion, sanhuaLoginInfo.resourceCode, sanhuaLoginInfo.staffCode, productSN, acasId);
            HttpResult result = http.GetHtml(item);
            if (result.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Log.WriteErrorLog(string.Format("UploadIdcData System abnormality, productSN={0}, ", productSN));
                return false;
            }

            string ParseText = result.Html;

            try
            {
                JObject jObject = JObject.Parse(ParseText);
                bool success = jObject["success"].Value<bool>();
                message = jObject["message"].Value<string>();

                if (!success)
                {
                    Log.WriteErrorLog("\r\nUploadIdcData Error:" + result.StatusDescription + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
                    return false;
                }

                Log.WriteInfoLog("\r\nUploadIdcData OK:" + result.StatusDescription + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
            }
            catch (Exception e)
            {
                message = e.Message;
                Log.WriteErrorLog("\r\nUploadIdcData Error:" + e.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
                return false;
            }

            return true;
        }

        public static bool ProductMoveNext(string productSN, string result, string defectCode, out string message)
        {
            message = string.Empty;
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem() { URL = sanhuaLoginInfo.url + "api/MESTestCollect/ProductMoveNext", Encoding = Encoding.UTF8, Method = "post", PostEncoding = Encoding.UTF8, KeepAlive = false };
            item.ContentType = "application/json;charset=utf-8";
            item.Postdata = string.Format("{{\"jigEdition\":\"{0}\",\"resourceCode\":\"{1}\",\"staffCode\":\"{2}\",\"productSN\":\"{3}\",\"defectCode\":\"{4}\",\"result\":\"{5}\"}}",
                sanhuaLoginInfo.jigEdtion, sanhuaLoginInfo.resourceCode, sanhuaLoginInfo.staffCode, productSN, defectCode, result);
            HttpResult hResult = http.GetHtml(item);
            if (hResult.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Log.WriteErrorLog(string.Format("System abnormality, productSN={0} ", productSN));
                return false;
            }

            string ParseText = hResult.Html;

            try
            {
                JObject jObject = JObject.Parse(ParseText);
                bool success = jObject["success"].Value<bool>();
                message = jObject["message"].Value<string>();

                if (!success)
                {
                    Log.WriteErrorLog("\r\nProductMoveNext Error:" + hResult.StatusDescription + hResult.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
                    return false;
                }

                Log.WriteInfoLog("\r\nProductMoveNext OK:" + hResult.StatusDescription + hResult.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
            }
            catch (Exception e)
            {
                message = e.Message;
                Log.WriteErrorLog("\r\nProductMoveNext Error:" + e.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
                return false;
            }

            return true;
        }
    }
}
