package org.springblade.flow.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import org.springblade.common.cache.PlatformCache;
import org.springblade.common.config.entity.PlatformConfig;
import org.springblade.common.config.yeepay.OpenProduct;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.ldt.shop.entity.Audit;
import org.springblade.yeePay.common.YeePayConst;
import org.springblade.yeePay.common.YeepayApiConstant;
import org.springblade.yeePay.entity.saas.InvitationDto;
import org.springblade.yeePay.service.YeepayCommonService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author: lianghanqiang
 * @description: 构建InvitationDto
 * @since: 9/23/21 -- 2:35 PM
 */
public class InvitationBuilder {

	private static YeepayCommonService yeepayCommonService;
	private static YeePayConst yeePayConst;

	static {
		yeepayCommonService = SpringUtil.getBean(YeepayCommonService.class);
		yeePayConst = SpringUtil.getBean(YeePayConst.class);
	}


	/**
	 * 封装进件信息
	 */
	public static InvitationDto build(Audit audit) {

		InvitationDto invitationDto = InvitationDto.builder().build();

		invitationDto.setRequestNo(IdUtil.simpleUUID());

		//商户信息
		InvitationDto.MerchantSubjectInfo merchantSubjectInfo = invitationDto.new MerchantSubjectInfo();
		String handLicenceUrl = yeepayCommonService.addFileToYeepay(audit.getHandLicenceUrl());
		String licenceUrl = yeepayCommonService.addFileToYeepay(audit.getBusinessLicense());
		String openAccountLicenceUrl = yeepayCommonService.addFileToYeepay(audit.getOpenAccountLicenceUrl());
		merchantSubjectInfo.setHandLicenceUrl(handLicenceUrl);
		merchantSubjectInfo.setLicenceNo(audit.getLicenceNo());
		merchantSubjectInfo.setLicenceUrl(licenceUrl);
		merchantSubjectInfo.setOpenAccountLicenceNo(audit.getOpenAccountLicenceNo());
		merchantSubjectInfo.setOpenAccountLicenceUrl(openAccountLicenceUrl);
		merchantSubjectInfo.setShortName(audit.getShortName());
		merchantSubjectInfo.setSignType(audit.getSignType());
		merchantSubjectInfo.setSignName(audit.getSignName());
		invitationDto.setMerchantSubjectInfo(merchantSubjectInfo);

		//法人信息
		InvitationDto.MerchantCorporationInfo merchantCorporationInfo = invitationDto.new MerchantCorporationInfo();
		String legalLicenceBackUrl = yeepayCommonService.addFileToYeepay(audit.getIdCardContrary());
		String legalLicenceFontUrl = yeepayCommonService.addFileToYeepay(audit.getIdCardFront());
		merchantCorporationInfo.setLegalLicenceBackUrl(legalLicenceBackUrl);
		merchantCorporationInfo.setLegalLicenceFrontUrl(legalLicenceFontUrl);
		merchantCorporationInfo.setLegalLicenceNo(audit.getLegalLicenceNo());
		merchantCorporationInfo.setLegalLicenceType(audit.getLegalLicenceType());
		merchantCorporationInfo.setLegalName(audit.getLegalPerson());
		invitationDto.setMerchantCorporationInfo(merchantCorporationInfo);

		//联系人信息
		InvitationDto.MerchantContactInfo merchantContactInfo = invitationDto.new MerchantContactInfo();
		merchantContactInfo.setContactEmail(audit.getContactEmail());
		merchantContactInfo.setContactLicenceNo(audit.getContactLicenceNo());
		merchantContactInfo.setContactMobile(audit.getContactMobile());
		merchantContactInfo.setContactName(audit.getContactName());
		merchantContactInfo.setServicePhone(audit.getServicePhone());
		invitationDto.setMerchantContactInfo(merchantContactInfo);

		//经营地址
		InvitationDto.BusinessAddressInfo businessAddressInfo = invitationDto.new BusinessAddressInfo();
		businessAddressInfo.setAddress(audit.getAddress());
		businessAddressInfo.setCity(audit.getCity());
		businessAddressInfo.setDistrict(audit.getDistrict());
		businessAddressInfo.setProvince(audit.getProvince());
		invitationDto.setBusinessAddressInfo(businessAddressInfo);

		//结算账户信息
		InvitationDto.SettlementAccountInfo settlementAccountInfo = invitationDto.new SettlementAccountInfo();
		settlementAccountInfo.setBankAccountType(audit.getBankAccountType());
		settlementAccountInfo.setBankCardNo(audit.getBankCardNo());
		settlementAccountInfo.setBankCode(audit.getBankCode());
		settlementAccountInfo.setSettlementDirection(YeepayApiConstant.settlementDirection.ACCOUNT.name());
		invitationDto.setSettlementAccountInfo(settlementAccountInfo);

		//添加开通产品信息
		List<InvitationDto.ProductInfo> productInfos = new ArrayList<>(32);
		Arrays.stream(OpenProduct.MERCHANT_PRODUCTS).forEach(item -> productInfos.add(PlatformCache.productInfo(item)));
		invitationDto.setProductInfo(productInfos);

		//产品资质信息
		InvitationDto.ProductQualificationInfo productQualificationInfo = invitationDto.new ProductQualificationInfo();
		productQualificationInfo.setPaymentScene("GYSDK");
		invitationDto.setProductQualificationInfo(productQualificationInfo);

		invitationDto.setParentMerchantNo(yeePayConst.getPlatformServiceNo());

		return invitationDto;
	}
}
