package com.hfkj.unipush;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.req.Audience;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sum1dream
 */
public class UniPushService {
    private static Logger log = LoggerFactory.getLogger(UniPushService.class);

    /**
     * 创建请求
     * @return
     */
    private static PushApi createClient() {
        // 设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId("9LTgsDR5Y26MTWpN1uBhb1");
        apiConfiguration.setAppKey("GV7qmZoFh1AaqD4uDfH857");
        apiConfiguration.setMasterSecret("JX9m0F92tZ5ciBtrfiJu93");
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        return apiHelper.creatApi(PushApi.class);
    }


    /**
     * @Author Sum1Dream
     * @Name androidTemplate
     * @Description // 安卓模版
     * @Date 16:08 2023/6/9
     * @Param [uniPushModel]
     * @Return void
     */
    public static Boolean pushToSingleByCid(JSONObject object) {

        log.info("========= START 个推手机端推送 START ===========");


        PushApi pushApi = createClient();

        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        // 设置个推通道参数
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        pushMessage.setTransmission(object.getString("body"));
        /*设置接收人信息*/
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(object.getString("cid"));
        log.info("个推-请求参数: " + JSON.toJSONString(pushDTO));
        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        log.info("个推-返回参数: " + JSON.toJSONString(apiResult));
        log.info("========= END 个推手机端推送 END ===========");

        if (apiResult.isSuccess()) {
            return apiResult.getData().toString().contains("successed_online");
        } else {
            return false;
        }
    }

/*    *//**
     * @Author Sum1Dream
     * @Name androidTemplate
     * @Description // 安卓模版
     * @Date 16:08 2023/6/9
     * @Param [uniPushModel]
     * @Return void
     *//*
    public static Boolean pushToSingleByCid(JSONObject object) {
        log.info("========= START 个推手机端推送 START ===========");

        PushApi pushApi = createClient();
        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        // 设置个推通道参数
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        pushMessage.setTransmission(object.getString("body"));


        Alert alert = new Alert();
        alert.setTitle("离线标题");
        alert.setBody(object.getString("body"));

        Aps aps = new Aps();
        aps.setAlert(alert);

        PushChannel pushChannel = new PushChannel();
        IosDTO iosDTO = new IosDTO();
        iosDTO.setType("notify");
        iosDTO.setAps(aps);
        pushChannel.setIos(iosDTO);



        AndroidDTO androidDTO = new AndroidDTO();
        ThirdNotification thirdNotification = new ThirdNotification();
        thirdNotification.setTitle("离线标题");
        thirdNotification.setBody("离线内容");
        thirdNotification.setClickType("startapp");

        Ups ups = new Ups();
        ups.setNotification(thirdNotification);

        Map<String, Map<String, Object>> paramMap = new HashMap<>();
        Map<String,Object> huaweiMap = new HashMap<>();
        huaweiMap.put("/message/android/category", "FINANCE");
        huaweiMap.put("/message/android/ttl", "60");
        paramMap.put("HW", huaweiMap);
        ups.setOptions(paramMap);
        androidDTO.setUps(ups);

        pushChannel.setAndroid(androidDTO);

        pushDTO.setPushChannel(pushChannel);

        *//*设置接收人信息*//*
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(object.getString("cid"));
        log.info("个推-请求参数: " + JSON.toJSONString(pushDTO));
        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        log.info("个推-返回参数: " + JSON.toJSONString(apiResult));
        log.info("========= END 个推手机端推送 END ===========");

        if (apiResult.isSuccess()) {
            return apiResult.getData().toString().contains("successed_online");
        } else {
            return false;
        }
    }*/

}
