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

namespace PTMedicalInsurance.Helper
{
    class InvokeComCenter : IInvokeCenter
    {
        string progID1 = "ybjk.interface";
        System.Type YinHaiComType;
        object YinHaiComInstance;

        public int Business(string infno, ref  string input, ref string output)
        {
            YinHaiComType = System.Type.GetTypeFromProgID(progID1);

            JObject jo = new JObject();
            jo.Add("transNo", infno);
            jo.Add("transType", "passwordCheck");
            jo.Add("timestamp", DateTime.Now.ToString("yyyyMMddHHmmss"));
            jo.Add("businessType", Global.pat.card.ecBizType);
            jo.Add("data", JObject.Parse(input));
            string inpar = jo.ToString();
            
            try
            {
                // 创建Com的实例
                if (YinHaiComType != null)
                {
                    //创建实例
                    YinHaiComInstance = Activator.CreateInstance(YinHaiComType);
                    //设置需要设置的参数值
                    //设置需要设置的参数值
                    object[] ParamArray = new object[3];
                    ParamArray[0] = infno;
                    ParamArray[1] = inpar;
                    ParamArray[2] = "";
                    ParameterModifier[] ParamMods = new ParameterModifier[1];
                    ParamMods[0] = new ParameterModifier(3); // 初始化为接口参数的个数
                                                             //ParamMods[0][0] = false;
                                                             //ParamMods[0][1] = false; 
                    ParamMods[0][2] = true;

                    YinHaiComType.InvokeMember("xjyb_call", // 接口函数名
                        BindingFlags.Default | BindingFlags.InvokeMethod,
                    null,
                    YinHaiComInstance, // 调用的COM组件
                    ParamArray, // 参数数组
                    ParamMods, // 指定返回参数的ParameterModifier数组
                    null,
                    null);
                    output = ParamArray[2].ToString();
                    return 0;
                }
                else
                {
                    output = "YinHaiComType加载失败！";
                    return 1;
                }
            }
            catch (Exception ex)
            {
                string outMes = ex.Message;
                if (ex.InnerException != null)
                {
                    outMes += ex.InnerException.Message;
                }
                output = outMes;
                return -1;
            }
            finally
            {
                Global.writeLog("InvokeComCenter.Business", inpar,output);
            }
        }



        public int BusinessExt(string inputData, ref string outputData, ref string pErrMsg)
        {
            throw new NotImplementedException();
        }

        public int DownloadFile(string inputData, ref string outputData)
        {
            throw new NotImplementedException();
        }

        public int Init(ref string pErrMsg)
        {
            return 0;
        }

        public int UploadFile(string inputData, ref string outputData, ref string pErrMsg)
        {
            throw new NotImplementedException();
        }
    }
}
