using Newtonsoft.Json;
using PTMedicalInsurance.Variables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace PTMedicalInsurance.CardReaders
{
    class NationECCodeAPI
    {
        private string url { get; set; }

        //电子凭证
        [DllImport("NationECCode.dll", EntryPoint = "NationEcTrans", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        static extern IntPtr NationEcTrans(StringBuilder strUrl, StringBuilder InData, StringBuilder OutData);

        public NationECCodeAPI(string _url)
        {
            url = _url;
        }

        public int NationEcTrans(NationECInput nationECInput, out string output )
        {
            output = "";
            try
            {
                StringBuilder sbURL = new StringBuilder(url);
                StringBuilder sbInput = new StringBuilder(JsonConvert.SerializeObject(nationECInput));
                StringBuilder sbOut = new StringBuilder(40960);
                IntPtr pInt = NationEcTrans(sbURL, sbInput, sbOut);
                output = sbOut.ToString();


                string str = Marshal.PtrToStringAnsi(pInt);

                if (str == "0000")
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                output += ex.Message;   
                throw;
            }
            finally
            {
                Global.writeLog($"NationEcTrans({url})", JsonConvert.SerializeObject(nationECInput),output);
            }
        }
    }

    class NationECInput
    {
        /// <summary>
        /// 机构代码
        /// </summary>
        public string orgId { get; set; }
        /// <summary>
        /// 交易类型
        /// </summary>
        public string transType { get; set; }
        /// <summary>
        /// 请求参数
        /// </summary>
        public NationECData data { get; set; }
        /// <summary>
        /// 扩展参数
        /// </summary>
        public string extra { get; set; }
    }

    class NationECData
    {
        /// <summary>
        /// 医保定点机构代码
        /// </summary>
        public string orgId { get; set; }
        /// <summary>
        /// 用码业务类型
        /// </summary>
        public string businessType { get; set; }
        /// <summary>
        /// 用码业务信息
        /// </summary>
        public string businessInfo { get; set; }

        /// <summary>
        /// 收款员编号
        /// </summary>
        public object operatorId { get; set; }
        /// <summary>
        /// 收款员姓名
        /// </summary>
        public string operatorName { get; set; }
        /// <summary>
        /// 医保科室编号
        /// </summary>
        public string officeId { get; set; }
        /// <summary>
        /// 科室名称
        /// </summary>
        public string officeName { get; set; }
       
        /// <summary>
        /// 扩展参数
        /// </summary>
        public string extData { get; set; }

    }

    class EcQueryData:NationECData
    {
        /// <summary>
        /// 人脸照片 字符 100k
        /// </summary>
        public string photoData { get; set; }
        /// <summary>
        /// 设备类型
        /// </summary>
        public string deviceType { get; set; }
    }

    class QrcodeGetData : NationECData
    {
        /// <summary>
        /// 定点医药机构本次业务流水号
        /// </summary>
        public string outBizNo { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        public string deviceType { get; set; }
    }

    class EcAuthData:NationECData
    {
        /// <summary>
        /// 定点医药机构本次业务流水号 不可重复，每次请求都需要唯一
        /// </summary>
        public string outBizNo { get; set; }
    }
    class AuthCheckData : NationECData
    {
        /// <summary>
        /// 定点医药机构本次业务流水号 不可重复，每次请求都需要唯一
        /// </summary>
        public string outBizNo { get; set; }

        /// <summary>
        /// 实 人 认证 业 务流水号  医保综合服务终端返回的授权码
        /// </summary>
        public string authNo { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        public string deviceType { get; set; }
    }

    /// <summary>
    /// 医保结算数据实体（继承自 NationECData）
    /// </summary>
    class SettleNotifyData : NationECData
    {
        /// <summary>
        /// 核身或者刷脸的医疗机构业务流水号（必填）
        /// 需要传核身接口或者电子凭证接口里的流水号
        /// </summary>
        public string outBizNo { get; set; }

        /// <summary>
        /// 医保/自费结算状态（必填）
        /// SUCCESS:结算成功, FAIL:结算失败, 
        /// REFUND_SUCCESS:退款成功, REFUND_FAIL:退款失败
        /// </summary>
        public string medicalSettleState { get; set; }

        /// <summary>
        /// 实人认证业务流水号（可空）
        /// </summary>
        public string authNo { get; set; }

        // operatorId 和 operatorName 已在父类中定义

        /// <summary>
        /// 总费用（必填）字符型（金额建议为分或保留小数）
        /// </summary>
        public string totalFee { get; set; }

        /// <summary>
        /// 业务场景（必填）
        /// register:挂号窗口, settle:诊间, drugStore:药店
        /// </summary>
        public string bizType { get; set; }

        /// <summary>
        /// 身份证号码（可空）
        /// </summary>
        public string idNo { get; set; }

        /// <summary>
        /// 姓名（可空）
        /// </summary>
        public string userName { get; set; }

        /// <summary>
        /// 结算时间（必填）格式如：yyyy-MM-dd HH:mm:ss
        /// </summary>
        public string setlTime { get; set; }

        /// <summary>
        /// 定点机构名称（必填）
        /// </summary>
        public string hospitalName { get; set; }

        // officeId 和 officeName 已在父类中定义

        /// <summary>
        /// 医生姓名（可空）
        /// </summary>
        public string doctorName { get; set; }

        /// <summary>
        /// 失败原因（可空）
        /// </summary>
        public string failReason { get; set; }

        // ========== 以下字段在 medicalSettleState=SUCCESS 或 REFUND_SUCCESS 时必填 ==========

        /// <summary>
        /// 医保单据流水号（成功时必填）
        /// </summary>
        public string medicalSettleNo { get; set; }

        /// <summary>
        /// 自费费用（成功时必填）
        /// </summary>
        public string ownAmt { get; set; }

        /// <summary>
        /// 医保报销费用（成功时必填）
        /// </summary>
        public string hifAmt { get; set; }

        /// <summary>
        /// 个人帐户支出（成功时必填）
        /// </summary>
        public string acctAmt { get; set; }

        /// <summary>
        /// 统筹基金支出（成功时必填）
        /// </summary>
        public string hifpAmt { get; set; }

        /// <summary>
        /// 大额医疗保险支出（可空）
        /// </summary>
        public string hifmiAmt { get; set; }

        /// <summary>
        /// 公务员补助（可空）
        /// </summary>
        public string cvlservAmt { get; set; }

        /// <summary>
        /// 医疗救助（可空）
        /// </summary>
        public string maAmt { get; set; }

        /// <summary>
        /// 单病种定点医疗机构垫支（可空）
        /// </summary>
        public string hosPreAmt { get; set; }

        /// <summary>
        /// 药品超标扣款金额（可空）
        /// </summary>
        public string medOverLmtAmt { get; set; }

        /// <summary>
        /// 扶贫救助（可空）
        /// </summary>
        public string mafAmt { get; set; }

        /// <summary>
        /// 历史起付公务员返还（可空）
        /// </summary>
        public string cvlservDedcAmt { get; set; }

        /// <summary>
        /// 帐户余额（可空）
        /// </summary>
        public string balance { get; set; }
    }
}
