using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aop.Api.Util;
using GraphQL;
using Library;
using LitJson;
using MySystem;
using MySystem.Models.Main;

namespace MySystem
{
    /// <summary>
    /// 好哒新建商户参数赋值
    /// </summary>
    public class AddNewMerchantSetUtil
    {
        public static AddNewMerchant SetValue(MerchantAddInfo AddInfo)
        {
            AddNewMerchant addNewMerchant = new AddNewMerchant();
            // addNewMerchant.brh = HaoDaHelper.BrhCode; //机构号(正式)
            if (AddInfo.BrandId == 1) addNewMerchant.brh = HaodaParam.BrhCode;
            if (AddInfo.BrandId == 0) addNewMerchant.brh = HaodaUnionParam.BrhCode;
            if (!string.IsNullOrEmpty(AddInfo.MchtNo))
            {
                addNewMerchant.mchtNo = AddInfo.MchtNo; //收款商户号
            }
            addNewMerchant.accountNo = AddInfo.AccountNumber; //银行卡号
            if (AddInfo.AccountType == "1")
            {
                addNewMerchant.accountName = AddInfo.CertLegalPerson; //开户名（账户名称）
            }
            else
            {
                addNewMerchant.accountName = AddInfo.AccountName; //开户名（账户名称）
            }
            addNewMerchant.accountType = AddInfo.AccountType; //账户类型 1 对私 0 对公
            addNewMerchant.zbankNo = AddInfo.ZbankNo; //开户银行支行号 参考支行号表（联系对接人提供）
            addNewMerchant.zbankRegionCode = AddInfo.ZbankRegionCode; //开户银行支行区域码 参考区域码表（联系对接人提供）

            if (AddInfo.AccountType == "1" && AddInfo.LicenceType == "0")
            {
                addNewMerchant.openBankAccName = AddInfo.OpenBankAccName; //开户许可证-开户名 企业商户必填(个体工商户对私不填)
                addNewMerchant.openBankAccout = AddInfo.OpenBankAccout; //开户许可证-银行卡号 企业商户必填(个体工商户对私不填)
                addNewMerchant.openBankRegionCode = AddInfo.OpenBankRegionCode; //开户许可证-银行地区码(地区码) 企业商户必填(个体工商户对私不填)
                addNewMerchant.openCollectBankCode = AddInfo.OpenCollectBankCode; //开户许可证-银行代码(总行行号) 企业商户必填(个体工商户对私不填)
                addNewMerchant.openUnionNo = AddInfo.OpenUnionNo; //开户许可证-开户支行 企业商户必填(个体工商户对私不填)
            }
            addNewMerchant.channelKind = AddInfo.ChannelKind; //商户类型 有执照填000002无执照填000001
            if (!string.IsNullOrEmpty(AddInfo.OutMchtNo))
            {
                addNewMerchant.outMchtNo = AddInfo.OutMchtNo; //慧掌柜商户号 通过查询商户审核状态接口会返回该慧掌柜商户号，后续如果新的收款商户号是属于同一个慧掌柜商户时则必填
            }
            addNewMerchant.mchtName = AddInfo.CertMerchantName; //商户名称 无营业执照商户命名规则：商户_姓名 有营业执照商户命名规则：与营业执照商户名一致
            addNewMerchant.mchtCnShortName = AddInfo.MerchantShortname; //商户简称
            addNewMerchant.address = AddInfo.BizStoreAddress; //商户门店地址 详细地址即可，不需要附带省市区
            addNewMerchant.alipayRate = "0.38"; //支付宝费率，值0.38，0.33等
            addNewMerchant.wechatPayRate = "0.38"; //微信费率，值0.38，0.33等
            addNewMerchant.unionPayRate = "0.38"; //银联二维码费率，值0.38，0.33等
            addNewMerchant.areaNo = AddInfo.AreaNo; //经营地区区域码 参考区域码表，填6位地区码（联系对接人提供）
            addNewMerchant.businessId = AddInfo.BusinessId; //经营范围 参考经营范围表（联系对接人提供）
            if (!string.IsNullOrEmpty(AddInfo.LicenseNumber))
            {
                addNewMerchant.licNo = AddInfo.LicenseNumber; //营业执照号 有营业执照必填
            }
            addNewMerchant.businessTerm = AddInfo.PeriodBegin.Value.ToString("yyyyMMdd") + "-" + AddInfo.PeriodEnd.Value.ToString("yyyyMMdd"); //营业执照有效期 格式：20200101-20300101，长期的填 20200101-99991231
            addNewMerchant.licenceType = AddInfo.LicenceType; //营业执照类型 0-企业法人营业执照 1-个体工商户营业执照 2-党政，机关及事业单位 3-其他组织
            addNewMerchant.userCardNo = AddInfo.IdCardNumber; //商户法人身份证号
            addNewMerchant.userPhone = AddInfo.MobilePhone; //法人手机号

            if (AddInfo.ChannelKind == "000001")
            {
                addNewMerchant.resserveMobile = AddInfo.ResserveMobile; //银行预留手机号 channelkind为000001时必填
            }
            addNewMerchant.userName = AddInfo.CertLegalPerson; //商户法人姓名
            if (AddInfo.CardPeriodEnd.Value.Year == 2050)
            {
                addNewMerchant.periodValidity = AddInfo.CardPeriodBegin.Value.ToString("yyyyMMdd") + "-" + "长期"; //法人身份证有效期 格式：20150101-20200101，若结束日期为长期填20150101-长期
            }
            else
            {
                addNewMerchant.periodValidity = AddInfo.CardPeriodBegin.Value.ToString("yyyyMMdd") + "-" + AddInfo.CardPeriodEnd.Value.ToString("yyyyMMdd"); //法人身份证有效期 格式：20150101-20200101，若结束日期为长期填20150101-长期
            }
            List<ImageItem> imageList = new List<ImageItem>();
            ImageItem imageItem = new ImageItem();
            //法人手持身份证正面
            if (!string.IsNullOrEmpty(AddInfo.PersonWithIdCard))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.PersonWithIdCard.Substring(AddInfo.PersonWithIdCard.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.PersonWithIdCard, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "personWithIdCard";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            //收款人银行卡照片
            if (!string.IsNullOrEmpty(AddInfo.BankCard))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.BankCard.Substring(AddInfo.BankCard.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.BankCard, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "bankCard";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            //身份证正面
            if (!string.IsNullOrEmpty(AddInfo.IdCardCopy))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.IdCardCopy.Substring(AddInfo.IdCardCopy.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.IdCardCopy, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "idCardFront";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            //身份证反面
            if (!string.IsNullOrEmpty(AddInfo.IdCardNational))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.IdCardNational.Substring(AddInfo.IdCardNational.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.IdCardNational, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "idCardBack";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }

            //开户许可证(对公凭证)
            if (!string.IsNullOrEmpty(AddInfo.OpenAccountLicenses))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.OpenAccountLicenses.Substring(AddInfo.OpenAccountLicenses.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.OpenAccountLicenses, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "openAccountLicenses";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            // //租赁协议
            // if (!string.IsNullOrEmpty(AddInfo.RentAgreement))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "rentAgreement";
            //     imageItem.value = AddInfo.RentAgreement;
            //     imageList.Add(imageItem);
            // }
            // //盒伙人商户合影
            // if (!string.IsNullOrEmpty(AddInfo.OperatorMcht))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "operatorMcht";
            //     imageItem.value = AddInfo.OperatorMcht;
            //     imageList.Add(imageItem);
            // }
            //营业执照
            if (!string.IsNullOrEmpty(AddInfo.LicenseCopy))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.LicenseCopy.Substring(AddInfo.LicenseCopy.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.LicenseCopy, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "license";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            // if (true)
            // {
            //     //委托结算协议
            //     if (!string.IsNullOrEmpty(AddInfo.AgentProtocol))
            //     {
            //         imageItem = new ImageItem();
            //         imageItem.name = "agentProtocol";
            //         imageItem.value = AddInfo.AgentProtocol;
            //         imageList.Add(imageItem);
            //     }
            //     //法人手持委托结算协议
            //     if (!string.IsNullOrEmpty(AddInfo.HoldAgentProtocol))
            //     {
            //         imageItem = new ImageItem();
            //         imageItem.name = "holdAgentProtocol";
            //         imageItem.value = AddInfo.HoldAgentProtocol;
            //         imageList.Add(imageItem);
            //     }
            //     //被委托人手持身份证
            //     if (!string.IsNullOrEmpty(AddInfo.AgentCardIdPicture))
            //     {
            //         imageItem = new ImageItem();
            //         imageItem.name = "agentCardIdPicture";
            //         imageItem.value = AddInfo.AgentCardIdPicture;
            //         imageList.Add(imageItem);
            //     }
            //     //被委托人身份证正面
            //     if (!string.IsNullOrEmpty(AddInfo.AgentIdCardFront))
            //     {
            //         imageItem = new ImageItem();
            //         imageItem.name = "agentIdCardFront";
            //         imageItem.value = AddInfo.AgentIdCardFront;
            //         imageList.Add(imageItem);
            //     }
            //     //被委托人身份证反面
            //     if (!string.IsNullOrEmpty(AddInfo.AgentIdCardBack))
            //     {
            //         imageItem = new ImageItem();
            //         imageItem.name = "agentIdCardBack";
            //         imageItem.value = AddInfo.AgentIdCardBack;
            //         imageList.Add(imageItem);
            //     }
            // }
            // //商户签名图片
            // if (!string.IsNullOrEmpty(AddInfo.SignaturePicture))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "signaturePicture";
            //     imageItem.value = AddInfo.SignaturePicture;
            //     imageList.Add(imageItem);
            // }
            // //辅助照片
            // if (!string.IsNullOrEmpty(AddInfo.Support))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "support";
            //     imageItem.value = AddInfo.Support;
            //     imageList.Add(imageItem);
            // }
            //门头照
            if (!string.IsNullOrEmpty(AddInfo.StoreEntrancePic))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.StoreEntrancePic.Substring(AddInfo.StoreEntrancePic.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.StoreEntrancePic, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "shopFrontImg";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            //收银台
            if (!string.IsNullOrEmpty(AddInfo.CheckstandImg))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.CheckstandImg.Substring(AddInfo.CheckstandImg.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.CheckstandImg, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "checkstandImg";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            //店铺全景
            if (!string.IsNullOrEmpty(AddInfo.IndoorPic))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.IndoorPic.Substring(AddInfo.IndoorPic.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.IndoorPic, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "shopInnerImg";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            // //商品照片
            // if (!string.IsNullOrEmpty(AddInfo.ProductImg))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "productImg";
            //     imageItem.value = AddInfo.ProductImg;
            //     imageList.Add(imageItem);
            // }
            // //合同签名
            // if (!string.IsNullOrEmpty(AddInfo.Agreement))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "agreement";
            //     imageItem.value = AddInfo.Agreement;
            //     imageList.Add(imageItem);
            // }
            // //税务登记证
            // if (!string.IsNullOrEmpty(AddInfo.TaxImage))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "taxImage";
            //     imageItem.value = AddInfo.TaxImage;
            //     imageList.Add(imageItem);
            // }
            // //组织机构证照片
            // if (!string.IsNullOrEmpty(AddInfo.OrganizationCopy))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "orgImage";
            //     imageItem.value = AddInfo.OrganizationCopy;
            //     imageList.Add(imageItem);
            // }
            // //行业许可证
            // if (!string.IsNullOrEmpty(AddInfo.IndustryLicense1))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "industryLicense1";
            //     imageItem.value = AddInfo.IndustryLicense1;
            //     imageList.Add(imageItem);
            // }
            // //正面人像图片
            // if (!string.IsNullOrEmpty(AddInfo.FaceRecognition))
            // {
            //     imageItem = new ImageItem();
            //     imageItem.name = "faceRecognition";
            //     imageItem.value = AddInfo.FaceRecognition;
            //     imageList.Add(imageItem);
            // }
            //补充资料
            if (!string.IsNullOrEmpty(AddInfo.Extra1))
            {
                var tokenInfo = JsonMapper.ToObject(HaoDaHelper.Instance.GetToken(AddInfo.Extra1.Substring(AddInfo.Extra1.LastIndexOf("/")).Trim('/'), "0", AddInfo.BrandId));
                if (tokenInfo["code"].ToString() == "1")
                {
                    var returnInfo = HaoDaHelper.Instance.ReturnFileName(tokenInfo["token"].ToString(), AddInfo.Extra1, AddInfo.BrandId);
                    if (returnInfo != "false")
                    {
                        imageItem = new ImageItem();
                        imageItem.name = "extra1";
                        imageItem.value = returnInfo;
                        imageList.Add(imageItem);
                    }
                }
            }
            addNewMerchant.images = imageList; //图片
            return addNewMerchant;
        }
    }
}