package com.xunhong.erp.turbo.facade.app.executor.cmd;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.collection.CollUtil;
import com.google.gson.annotations.SerializedName;
import com.xunhong.erp.turbo.api.facade.dto.vo.WxMaOcrBankCardVO;
import com.xunhong.erp.turbo.api.infra.api.SettingServiceI;
import com.xunhong.erp.turbo.facade.app.executor.common.WxCmdExe;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.bean.ocr.WxOcrBankCardResult;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * @author shenyifei
 */
@Component
@RequiredArgsConstructor
public class WxMaOcrBankCardCmdExe extends WxCmdExe {

	@DubboReference(version = "1.0.0")
	private final SettingServiceI settingService;

	@SneakyThrows
	public WxMaOcrBankCardVO execute(String ocrUrl) {
		WxMaService wxMaService = getWxMaService(settingService);
		WxOcrBankCardResult wxOcrBankCardResult = wxMaService.getOcrService().bankCard(ocrUrl);
		String number = wxOcrBankCardResult.getNumber();
		WxMaOcrBankCardVO wxMaOcrBankCardVO = new WxMaOcrBankCardVO();
		wxMaOcrBankCardVO.setNumber(number);
		wxMaOcrBankCardVO.setOcrUrl(ocrUrl);

		GetBankByNumRequest getBankByNumRequest = new GetBankByNumRequest();
		getBankByNumRequest.setSetAccountNumber(number);

		String responseContent = wxMaService.post("https://api.weixin.qq.com/shop/funds/getbankbynum", getBankByNumRequest);
		WxGetBankByNumResult wxGetBankByNumResult = WxGetBankByNumResult.fromJson(responseContent);
		List<BankInfo> data = wxGetBankByNumResult.getData();
		if (CollUtil.isNotEmpty(data)) {
			wxMaOcrBankCardVO.setBankName(data.getFirst().getBankName());
		}
		return wxMaOcrBankCardVO;
	}

	@Getter
	@Setter
	public static class GetBankByNumRequest implements Serializable {
		@Serial
		private static final long serialVersionUID = 1L;

		@SerializedName("account_number")
		private String setAccountNumber;
	}

	@Getter
	@Setter
	public static class WxGetBankByNumResult implements Serializable {
		@Serial
		private static final long serialVersionUID = 1L;

		@SerializedName("total_count")
		private Integer totalCount;
		private List<BankInfo> data;

		public static WxGetBankByNumResult fromJson(String json) {
			return WxGsonBuilder.create().fromJson(json, WxGetBankByNumResult.class);
		}

		public String toString() {
			return WxGsonBuilder.create().toJson(this);
		}
	}

	@Getter
	@Setter
	public static class BankInfo implements Serializable {
		@Serial
		private static final long serialVersionUID = 1L;
		@SerializedName("bank_code")
		private String bankCode;

		@SerializedName("bank_id")
		private String bankId;

		@SerializedName("bank_name")
		private String bankName;

		@SerializedName("need_branch")
		private Boolean needBranch;

		@SerializedName("account_bank")
		private String accountBank;
	}
}
