package com.zhgd.xmgl.push.service.impl;

import com.alibaba.fastjson.JSON;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import com.getui.push.v2.sdk.dto.res.CidStatusDTO;
import com.zhgd.xmgl.push.config.PushApiHelper;
import com.zhgd.xmgl.push.service.UniPushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: wisdomSite
 * @description:
 * @author: Mr.Peng
 * @create: 2022-04-20 09:46
 **/
@Service
@Slf4j
public class UniPushServiceImpl implements UniPushService {
    @Autowired
    private PushApiHelper pushApiHelper;

    @Override
    public ApiResult pushToSingleByAlias(String alias, String title, String content, String payload) {
        PushDTO<Audience> pushDTO = this.buildPushDTO(title, content, payload);
//        log.info("payload-----" + pushDTO.getPushMessage().getNotification().getPayload());
        log.info("alias----{},title-----{},content-----{}", alias, title, content);
        log.info("pushDTO-----" + JSON.toJSONString(pushDTO));
        // 设置接收人信息
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addAlias(alias);// cid
        audience.addCid(alias);// cid
        if (pushApiHelper != null) {
            pushApiHelper.getPushApi().pushToSingleByCid(pushDTO);
            // 进行cid单推
            ApiResult<Map<String, Map<String, String>>> apiResult = pushApiHelper.getPushApi().pushToSingleByCid(pushDTO);
            if (apiResult.isSuccess()) {
                // success
                log.info(apiResult.getData().toString());
            } else {
                // failed
                log.error("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
            }
            return apiResult;
        } else {
            return ApiResult.fail("配置初始化失败", 5000);
        }

    }

    private PushDTO<Audience> buildPushDTO(String title, String content, String payload) {
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        //requestid需要每次变化唯一
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        pushDTO.setGroupName("wxb-group");

        //配置推送条件
        // 1: 表示该消息在用户在线时推送个推通道，用户离线时推送厂商通道;
        // 2: 表示该消息只通过厂商通道策略下发，不考虑用户是否在线;
        // 3: 表示该消息只通过个推通道下发，不考虑用户是否在线；
        // 4: 表示该消息优先从厂商通道下发，若消息内容在厂商通道代发失败后会从个推通道下发。
        Strategy strategy = new Strategy();
        strategy.setDef(1);
        strategy.setSt(1);
        Settings settings = new Settings();
        settings.setStrategy(strategy);
        pushDTO.setSettings(settings);
        //消息有效期，走厂商消息需要设置该值
        settings.setTtl(3600000);


        //推送苹果离线通知标题内容
        Alert alert = new Alert();
        //苹果离线通知栏标题
        alert.setTitle(title);
        //苹果离线通知栏内容
        alert.setBody(content);
        Aps aps = new Aps();
        //1表示静默推送(无通知栏消息)，静默推送时不需要填写其他参数。
        // 苹果建议1小时最多推送3条静默消息
        aps.setContentAvailable(0);
        aps.setSound("default");
        aps.setAlert(alert);
        IosDTO iosDTO = new IosDTO();
        iosDTO.setPayload(payload);
        iosDTO.setAps(aps);
        iosDTO.setType("notify");

        PushChannel pushChannel = new PushChannel();
        pushChannel.setIos(iosDTO);
        //安卓离线厂商通道推送消息体
        AndroidDTO androidDTO = new AndroidDTO();
        Ups ups = new Ups();
        ThirdNotification notification1 = new ThirdNotification();
        ups.setNotification(notification1);
        //安卓离线展示的标题
        notification1.setTitle(title);
        //安卓离线展示的内容
        notification1.setBody(content);
        notification1.setClickType("intent");
        notification1.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.weixiaobao/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=" + title + ";S.content=" + content + ";S.payload=" + payload + ";end");
        notification1.setPayload(payload);
        notification1.setUrl(payload);
        //各厂商自有功能单项设置
        //ups.addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry ");
        //ups.addOption("HW", "/message/android/notification/badge/add_num", 1);
        //ups.addOption("HW", "/message/android/notification/importance", "HIGH");
        //ups.addOption("VV","classification",1);
        androidDTO.setUps(ups);
        pushChannel.setAndroid(androidDTO);
        pushDTO.setPushChannel(pushChannel);

        // PushMessage在线走个推通道才会起作用的消息体
        PushMessage pushMessage = new PushMessage();


//        GTNotification notification = new GTNotification();
//        notification.setTitle(title);
//        notification.setBody(content);
//        notification.setClickType("payload");
//        notification.setChannelLevel("4");
//        notification.setPayload(payload);
//        pushMessage.setNotification(notification);
        pushMessage.setTransmission("{title:\"" + title + "\",content:\"" + content + "\",payload:\"" + payload + "\"}");
        pushDTO.setPushMessage(pushMessage);
        return pushDTO;
    }

    //别名绑定
    @Override
    public ApiResult bindAlias(String alias, String clientId) {
        CidAliasListDTO cidAliasListDTO = new CidAliasListDTO();
        List<CidAliasListDTO.CidAlias> list = new ArrayList<>();
        CidAliasListDTO.CidAlias cidAlias = new CidAliasListDTO.CidAlias();
        cidAlias.setAlias(alias);
        cidAlias.setCid(clientId);
        list.add(cidAlias);
        cidAliasListDTO.setDataList(list);
        if (pushApiHelper != null) {
            ApiResult result = pushApiHelper.getUserApi().bindAlias(cidAliasListDTO);
            return result;
        } else {
            return ApiResult.fail("配置初始化失败", 5000);
        }
    }

    @Override
    public ApiResult<Map<String, CidStatusDTO>> queryUserStatus(String clientId) {
        Set<String> list = new HashSet<>();
        list.add(clientId);
        if (pushApiHelper != null) {
            return pushApiHelper.getUserApi().queryUserStatus(list);
        } else {
            return ApiResult.fail("配置初始化失败", 5000);
        }
    }

}
