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

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedissonConfigImpl;
import cn.hutool.extra.spring.SpringUtil;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.xunhong.erp.turbo.api.infra.api.SettingServiceI;
import com.xunhong.erp.turbo.api.infra.dto.common.*;
import com.xunhong.erp.turbo.api.infra.dto.enums.SettingKeyEnum;
import com.xunhong.erp.turbo.api.infra.dto.qry.SettingShowQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.SettingVO;
import me.chanjar.weixin.cp.api.WxCpGroupRobotService;
import me.chanjar.weixin.cp.api.impl.WxCpGroupRobotServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpRedissonConfigImpl;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedissonConfigImpl;
import org.redisson.api.RedissonClient;

import java.nio.charset.StandardCharsets;

/**
 * @author shenyifei
 */
public class WxCmdExe {

	public WxMaService getWxMaService(SettingServiceI settingService) {
		SettingShowQry settingShowQry = new SettingShowQry();
		settingShowQry.setSettingKey(SettingKeyEnum.WX_MA_CONFIG);
		SettingVO settingVO = settingService.show(settingShowQry);
		WxMaConfigValue wxMaConfigValue = (WxMaConfigValue) settingVO.getSettingValue();

		RedissonClient redissonClient = SpringUtil.getBean(RedissonClient.class);
		WxMaRedissonConfigImpl wxMaRedissonConfig = new WxMaRedissonConfigImpl(redissonClient);
		wxMaRedissonConfig.setAppid(wxMaConfigValue.getAppId());
		wxMaRedissonConfig.setSecret(wxMaConfigValue.getAppSecret());

		SettingShowQry settingShowQry1 = new SettingShowQry();
		settingShowQry1.setSettingKey(SettingKeyEnum.WX_MA_MESSAGE_PUSH_CONFIG);
		SettingVO settingVO1 = settingService.show(settingShowQry1);
		WxMaMessagePushConfigValue wxMaMessagePushConfigValue = (WxMaMessagePushConfigValue) settingVO1.getSettingValue();
		wxMaRedissonConfig.setToken(wxMaMessagePushConfigValue.getToken());
		wxMaRedissonConfig.setAesKey(wxMaMessagePushConfigValue.getEncodingAESKey());

		WxMaServiceImpl wxMaService = new WxMaServiceImpl();
		wxMaService.setWxMaConfig(wxMaRedissonConfig);

		return wxMaService;
	}

	public WxMpService getWxMpService(SettingServiceI settingService) {
		SettingShowQry settingShowQry = new SettingShowQry();
		settingShowQry.setSettingKey(SettingKeyEnum.WX_MP_CONFIG);
		SettingVO settingVO = settingService.show(settingShowQry);
		WxMpConfigValue wxMpConfigValue = (WxMpConfigValue) settingVO.getSettingValue();

		RedissonClient redissonClient = SpringUtil.getBean(RedissonClient.class);
		WxMpRedissonConfigImpl wxMpRedissonConfig = new WxMpRedissonConfigImpl(redissonClient);
		wxMpRedissonConfig.setAppId(wxMpConfigValue.getAppId());
		wxMpRedissonConfig.setSecret(wxMpConfigValue.getAppSecret());

		SettingShowQry settingShowQry1 = new SettingShowQry();
		settingShowQry1.setSettingKey(SettingKeyEnum.WX_MP_MESSAGE_PUSH_CONFIG);
		SettingVO settingVO1 = settingService.show(settingShowQry1);
		WxMpMessagePushConfigValue wxMpMessagePushConfigValue = (WxMpMessagePushConfigValue) settingVO1.getSettingValue();
		wxMpRedissonConfig.setToken(wxMpMessagePushConfigValue.getToken());
		wxMpRedissonConfig.setAesKey(wxMpMessagePushConfigValue.getEncodingAESKey());

		WxMpServiceImpl wxMpService = new WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(wxMpRedissonConfig);

		return wxMpService;
	}


	public WxPayService getWxPayService(SettingServiceI settingService) {
		SettingShowQry settingShowQry = new SettingShowQry();
		settingShowQry.setSettingKey(SettingKeyEnum.WX_PAY_CONFIG);
		SettingVO settingVO = settingService.show(settingShowQry);

		WxPayConfigValue wxPayConfigValue = (WxPayConfigValue) settingVO.getSettingValue();

		WxPayConfig wxPayConfig = new WxPayConfig();
		wxPayConfig.setAppId(wxPayConfigValue.getAppId());
		wxPayConfig.setMchId(wxPayConfigValue.getMchId());
		wxPayConfig.setApiV3Key(wxPayConfigValue.getApiV3Key());
		wxPayConfig.setCertSerialNo(wxPayConfigValue.getCertSerialNo());
		wxPayConfig.setPrivateCertContent(wxPayConfigValue.getPrivateCertString().getBytes(StandardCharsets.UTF_8));
		wxPayConfig.setPrivateKeyContent(wxPayConfigValue.getPrivateKeyString().getBytes(StandardCharsets.UTF_8));
		wxPayConfig.setPublicKeyId(wxPayConfigValue.getPublicKeyId());
		wxPayConfig.setPublicKeyContent(wxPayConfigValue.getPublicKeyString().getBytes(StandardCharsets.UTF_8));
		wxPayConfig.setNotifyUrl(wxPayConfigValue.getNotifyUrl());

		WxPayServiceImpl wxPayService = new WxPayServiceImpl();
		wxPayService.setConfig(wxPayConfig);

		return wxPayService;
	}

	public WxCpGroupRobotService getWxCpGroupRobotService(String webhookKey) {
		RedissonClient redissonClient = SpringUtil.getBean(RedissonClient.class);
		WxCpRedissonConfigImpl wxCpRedissonConfig = new WxCpRedissonConfigImpl(redissonClient);
		wxCpRedissonConfig.setWebhookKey(webhookKey);

		WxCpServiceImpl wxCpService = new WxCpServiceImpl();
		wxCpService.setWxCpConfigStorage(wxCpRedissonConfig);
		return new WxCpGroupRobotServiceImpl(wxCpService);
	}
}
