package com.jeequan.jeepay.thirdparty.service.push;

import cn.hutool.core.util.IdUtil;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.CommonEnum;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.message.PushBatchDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.push.IAppPushService;
import com.jeequan.jeepay.core.model.DBAppPushConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class AppPushService implements IAppPushService {

    /**
     *    1. 第一个参数为消息内容
     *    2. 第二个参数为设备号
     */
    @Override
    public Map<String, Map<String, String>> pushMessageToList(String content, Set<String> cIds, DBAppPushConfig appPushConfig) {

        GtApiConfiguration apiConfig = new GtApiConfiguration();
        apiConfig.setAppId(appPushConfig.getUniPushAppId());
        apiConfig.setAppKey(appPushConfig.getUniPushAppKey());
        apiConfig.setMasterSecret(appPushConfig.getUniPushMasterSecret());
        apiConfig.setDomain(DBAppPushConfig.GT_APP_PUSH);
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfig);
        // 创建推送对象
        PushApi pushApi = apiHelper.creatApi(PushApi.class);

        // 根据cid进行批量单推
        PushBatchDTO pushBatchDTO = new PushBatchDTO();
        pushBatchDTO.setAsync(true);

        for (String cid : cIds) {
            PushDTO<Audience> pushDTO = new PushDTO<>();
            pushBatchDTO.addPushDTO(pushDTO);

            // 设置推送ID
            pushDTO.setRequestId(IdUtil.fastSimpleUUID());

            // 设置个推通道参数
            PushMessage pushMessage = new PushMessage();
            pushMessage.setNetworkType(CommonEnum.NetworkTypeEnum.TYPE_ALL.type);
            pushMessage.setTransmission(content);
            pushDTO.setPushMessage(pushMessage);

            // 设置接收人信息
            Audience audience = new Audience();
            pushDTO.setAudience(audience);
            audience.addCid(cid);

            // 推送设置
            Settings settings = new Settings();
            settings.setTtl(-1); // 离线不发
            pushDTO.setSettings(settings);
        }

        // 进行cid批量推送
        log.info("个推消息推送cids：{}", StringUtils.join(cIds, ","));
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushBatchByCid(pushBatchDTO);
        if (apiResult.isSuccess()) {
            log.info("个推消息推送成功，response：{}", apiResult.getData());
            return apiResult.getData();
        } else {
            log.info("个推消息推送失败，code：{}, msg：{}", apiResult.getCode(), apiResult.getMsg());
            throw new BizException(apiResult.getMsg());
        }
    }


}