package com.yn.bftl.thirdparty.modules.umeng.service.impl;

import com.yn.bftl.common.common.exception.YnceError;
import com.yn.bftl.common.modules.configuration.entity.ThirdPartyPaySet;
import com.yn.bftl.common.modules.customerService.dto.UmengUnicastDTO;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.common.repository.ThirdPartyPaySetRepository;
import com.yn.bftl.thirdparty.modules.umeng.abstracts.AbstractUmengNotification;
import com.yn.bftl.thirdparty.modules.umeng.abstracts.android.AbstractAndroidNotificationAbstract;
import com.yn.bftl.thirdparty.modules.umeng.abstracts.android.AbstractAndroidUnicast;
import com.yn.bftl.thirdparty.modules.umeng.abstracts.ios.AbstractIosUnicast;
import com.yn.bftl.thirdparty.modules.umeng.enums.DisplayType;
import com.yn.bftl.thirdparty.modules.umeng.service.UmengService;
import com.yn.bftl.thirdparty.modules.umeng.util.UmengPushUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 友盟服务实现
 *
 * @author huabiao
 * @create 2023/12/5  16:24
 **/
@Service
public class UmengServiceImpl implements UmengService {

    @Resource
    private ThirdPartyPaySetRepository thirdPartyPaySetRepository;

    @Override
    public Boolean sendUnicast(UmengUnicastDTO umengUnicastDTO) throws Exception {
        AbstractUmengNotification abstractUmengNotification;
        // 获取第三方配置
        ThirdPartyPaySet thirdPartyPaySet = getThirdPartyPaySetByCompanyId(umengUnicastDTO.getCompanyId());
        if (umengUnicastDTO.getIsAndroid()) {
            AbstractAndroidUnicast androidUnicast = new AbstractAndroidUnicast(thirdPartyPaySet.getMessagePushAndroidAppKey(), thirdPartyPaySet.getMessagePushAndroidAppMasterSecret());
            androidUnicast.setDeviceToken(umengUnicastDTO.getDeviceToken());
            androidUnicast.setTitle(umengUnicastDTO.getTitle());
            androidUnicast.setText(umengUnicastDTO.getText());
            androidUnicast.goAppAfterOpen();
            androidUnicast.setDisplayType(DisplayType.NOTIFICATION);
            // 设置正式模式
            androidUnicast.setProductionMode();
            androidUnicast.setChannelActivity(thirdPartyPaySet.getMessagePushChannelActivity());
            // 处理厂商渠道
            abstractUmengNotification = this.handleChannelProperties(thirdPartyPaySet, umengUnicastDTO.getDeviceBrand(), androidUnicast);
        } else {
            AbstractIosUnicast abstractIosUnicast = new AbstractIosUnicast(thirdPartyPaySet.getMessagePushIosAppKey(), thirdPartyPaySet.getMessagePushIosAppMasterSecret());
            abstractIosUnicast.setDeviceToken(umengUnicastDTO.getDeviceToken());
            abstractIosUnicast.setAlert(umengUnicastDTO.getTitle(), "", umengUnicastDTO.getText());
            abstractIosUnicast.setBadge("+1");
            abstractIosUnicast.setSound("default");
            // 设置正式模式
            abstractIosUnicast.setProductionMode();
            abstractUmengNotification = abstractIosUnicast;
        }
        return UmengPushUtil.send(abstractUmengNotification, thirdPartyPaySet.getMessagePushAddress(), thirdPartyPaySet.getMessagePushPath());
    }


    /**
     * 友盟-处理厂商渠道
     *
     * @param thirdPartyPaySet            第三方配置
     * @param deviceBrand                 设备品牌
     * @param abstractAndroidNotification 安卓通知
     * @author huabiao
     * @create 2023/12/5  17:18
     */
    private AbstractUmengNotification handleChannelProperties(ThirdPartyPaySet thirdPartyPaySet, String deviceBrand, AbstractAndroidNotificationAbstract abstractAndroidNotification) throws Exception {
        if(StringUtils.isBlank(deviceBrand)){
            return abstractAndroidNotification;
        }
        switch (deviceBrand.toLowerCase()) {
            case "huawei":
                checkMessagePushChannelActivity(thirdPartyPaySet.getMessagePushChannelActivity());
                abstractAndroidNotification.setHuaweiChannelProperties(thirdPartyPaySet.getMessagePushChannelActivity());
                break;
            case "redmi":
            case "xiaomi":
                checkMessagePushChannelActivity(thirdPartyPaySet.getMessagePushChannelActivity());
                if (StringUtils.isBlank(thirdPartyPaySet.getMessagePushXiaoMiChannelId())) {
                    throw new YnceErrorException(YnceError.YNCE_225001, "未配置消息推送-小米通道ID");
                }
                abstractAndroidNotification.setXiaoMiChannelProperties(thirdPartyPaySet.getMessagePushXiaoMiChannelId(), thirdPartyPaySet.getMessagePushChannelActivity());
                break;
            case "oppo":
                checkMessagePushChannelActivity(thirdPartyPaySet.getMessagePushChannelActivity());
                if (StringUtils.isBlank(thirdPartyPaySet.getMessagePushOppoChannelId())) {
                    throw new YnceErrorException(YnceError.YNCE_225001, "未配置消息推送-OPPO通道ID");
                }
                abstractAndroidNotification.setOppoChannelProperties(thirdPartyPaySet.getMessagePushOppoChannelId(), thirdPartyPaySet.getMessagePushChannelActivity());
                break;
            case "vivo":
                checkMessagePushChannelActivity(thirdPartyPaySet.getMessagePushChannelActivity());
                abstractAndroidNotification.setVivoChannelProperties(thirdPartyPaySet.getMessagePushChannelActivity());
                break;
            case "meizu":
                checkMessagePushChannelActivity(thirdPartyPaySet.getMessagePushChannelActivity());
                abstractAndroidNotification.setMeizuChannelProperties(thirdPartyPaySet.getMessagePushChannelActivity());
                break;
            case "honor":
                checkMessagePushChannelActivity(thirdPartyPaySet.getMessagePushChannelActivity());
                abstractAndroidNotification.setHonorChannelProperties(thirdPartyPaySet.getMessagePushChannelActivity());
                break;
            default:
                break;
        }
        return abstractAndroidNotification;
    }

    private void checkMessagePushChannelActivity(String messagePushChannelActivity) {
        if (StringUtils.isBlank(messagePushChannelActivity)) {
            throw new YnceErrorException(YnceError.YNCE_225001, "未配置消息推送-通道地址");
        }
    }

    /**
     * 获取第三方配置
     *
     * @param companyId 企业ID
     * @return 第三方配置
     * @author huabiao
     * @create 2023/12/5  17:32
     */
    private ThirdPartyPaySet getThirdPartyPaySetByCompanyId(Long companyId) {
        ThirdPartyPaySet thirdPartyPaySet = thirdPartyPaySetRepository.findFirstByCompanyId(companyId);
        if (thirdPartyPaySet == null) {
            throw new YnceErrorException(YnceError.YNCE_225001, "未配置参数");
        }
        if (StringUtils.isBlank(thirdPartyPaySet.getMessagePushAddress())
                || StringUtils.isBlank(thirdPartyPaySet.getMessagePushPath())
                || StringUtils.isBlank(thirdPartyPaySet.getMessagePushAndroidAppKey())
                || StringUtils.isBlank(thirdPartyPaySet.getMessagePushAndroidAppMasterSecret())
                || StringUtils.isBlank(thirdPartyPaySet.getMessagePushIosAppKey())
                || StringUtils.isBlank(thirdPartyPaySet.getMessagePushIosAppMasterSecret())) {
            throw new YnceErrorException(YnceError.YNCE_225001, "未配置消息推送参数");
        }
        return thirdPartyPaySet;
    }
}
