using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Common;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PTMedicalInsurance.Business.Core.SelfServiceMachine.Process.ReadCard
{
    class ReadCardProcess : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            outParam = "";
            string sOutPar;
            Global.pat.bChangePIN = false;
            outParam = "";
            JObject joInput = new JObject();
            try
            {
                STA sta = new STA();
                object[] o = new object[2];

                sta.ReadCard(o);

                if (int.Parse(o[0].ToString()) != 0)
                {
                    outParam = o[1].ToString();
                    return Exception(-1, "", outParam);
                }
                else
                {
                    if (Global.pat.isWorkInjury)
                    {          
                        if (trade1101(out outParam) != 0)
                        {
                            return Exception(-1, "", outParam);
                        }
                        else
                        {
                            JObject joRtn = JObject.Parse(outParam);
                            Global.pat.psn_no = JsonHelper.getDestValue(joRtn, "output.baseinfo.psn_no");
                            if (tradeQ002(out outParam) != 0)
                            {
                                return Exception(-1, "", outParam);
                            }
                            else
                            {
                                joRtn["output"]["workinjury"] = new JArray();
                                joRtn["output"]["workinjury"] = JArray.Parse(JsonHelper.getDestValue(JObject.Parse(outParam), "output.gsxx"));
                                outParam = joRtn.ToString();
                                return Exception(-1, "", outParam);
                            }
                        }
                    }
                    else
                    {
                        if (trade1101(out outParam) != 0)
                        {
                            return Exception(-1, "", outParam);
                        }

                        return Success(outParam);
                    }
                }
            }
            catch (Exception ex)
            {
                return Error("异常：" + ex.Message);
            }
        }

        private int trade1101(out string outParam)
        {
            outParam = "";
            string errorMsg = "";

            JObject joInput = new JObject();
            JObject joRtn = new JObject();

            #region【1101获取身份信息】
            JObject joData = new JObject();
            joData.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
            joData.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
            joData.Add("card_sn", Global.pat.card.SN);
            //开始时间 
            if (string.IsNullOrEmpty(Global.pat.begntime))
            {
                Global.pat.begntime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            joData.Add("begntime", Global.pat.begntime);
            joData.Add("psn_cert_type", Global.pat.certType);
            joData.Add("certno", Global.pat.certNO); //证件号码
            joData.Add("psn_name", Global.pat.name);

            joInput.Add("data", joData);

            joRtn = invoker.invokeCenterService("1101", JsonHelper.setCenterInpar("1101", joInput));
            if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
            {
                outParam = "获取病人信息失败：" + errorMsg;
                return -1;
            }
            else
            {
                //joRtn.Add("cardinfo",joCardInfo);
                outParam = joRtn.ToString();
                Global.pat.insuplc_admdvs = JsonHelper.getDestValue(JObject.Parse(outParam), "output.insuinfo[0].insuplc_admdvs"); //参保地
                return 0;
            }
            #endregion
        }

        private int tradeQ002(out string outParam)
        {
            outParam = "";
            string errorMsg = "";

            JObject joInput = new JObject();
            JObject joRtn = new JObject();
            JObject joData = new JObject();
            joData.Add("psn_no", Global.pat.psn_no);

            joInput.Add("data", joData);
            joRtn = invoker.invokeCenterService("Q002", JsonHelper.setCenterInpar("Q002", joInput));
            if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
            {
                outParam = "获取病人信息失败：" + errorMsg;
                return -1;
            }
            else
            {
                outParam = joRtn.ToString();
                return 0;
            }
        }
    }

}
