package com.njcn.msgpush.module.push.client.sender.impl;

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.Settings;
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.GTNotification;
import com.njcn.msgpush.module.push.client.sender.AppPushSender;
import com.njcn.msgpush.module.push.client.sender.Sender;
import com.njcn.msgpush.module.push.client.setting.impl.UniPushAppPushSetting;
import com.njcn.msgpush.module.push.dal.dataobject.message.MessageRecordDO;

import java.util.Map;

/**
 * @author caozehui
 * @data 2026-02-26
 */
public class UniPushAppPushSender implements AppPushSender {
    //    private UniPushAppPushSetting uniPushAppPushSetting;
    private Sender sender;

    private ApiHelper apiHelper;

    public UniPushAppPushSender(UniPushAppPushSetting uniPushAppPushSetting, Sender sender) {
//        this.uniPushAppPushSetting = uniPushAppPushSetting;
        this.sender = sender;

        GtApiConfiguration gtApiConfiguration = new GtApiConfiguration();
        gtApiConfiguration.setAppId(uniPushAppPushSetting.getAppId());
        gtApiConfiguration.setAppKey(uniPushAppPushSetting.getAppKey());
        gtApiConfiguration.setMasterSecret(uniPushAppPushSetting.getMasterSecret());
        gtApiConfiguration.setDomain("https://restapi.getui.com/v2/");
        this.apiHelper = ApiHelper.build(gtApiConfiguration);
    }

    @Override
    public boolean appPush(MessageRecordDO message) {
        PushDTO<Audience> pushDTO = this.buildPushDTO(message.getTitle(), message.getContent());
        // 进行cid单推
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        // 设置接收人信息
        Audience audience = new Audience();
        audience.addCid(message.getReceiver());
        pushDTO.setAudience(audience);

        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        return apiResult.isSuccess();
    }

    /**
     * 构建推送消息体
     *
     * @param title
     * @param content
     * @return
     */
    private PushDTO<Audience> buildPushDTO(String title, String content) {
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
//        pushDTO.setGroupName("hyqx-group");

        //配置推送条件
        Settings settings = new Settings();
        //消息有效期，走厂商消息需要设置该值
        settings.setTtl(3600000);
        pushDTO.setSettings(settings);

        //安卓在线通道走个推推送时的消息体（在线通道不支持ios）
        PushMessage pushMessage = new PushMessage();
        //通知消息
        GTNotification notification = new GTNotification();
        notification.setTitle(title);
        notification.setBody(content);
        /**
         * intent：打开应用内特定页面，
         * url：打开网页地址，
         * payload：自定义消息内容启动应用，
         * payload_custom：自定义消息内容不启动应用，
         * startapp：打开应用首页，
         * none：纯通知，无后续动作
         */
        //进入特定的页面
//        notification.setClickType("intent");
//        notification.setIntent("intent://com.getui.push/detail?#Intent;scheme=gtpushscheme;launchFlags=0x4000000;package=com.getui.demo;component=com.getui.demo/com.getui.demo.DemoActivity;S.payload=payloadStr;end");
        notification.setClickType("startapp");
        pushMessage.setNotification(notification);
        pushDTO.setPushMessage(pushMessage);
        //透传消息 此格式的透传消息由 unipush 做了特殊处理，会自动展示通知栏。开发者也可自定义其它格式，在客户端自己处理。
//        pushMessage.setTransmission(" {title:\"个推通道透传消息标题\",content:\"个推通道透传消息内容\",payload:\"自定义数据\"}");

        //设置离线推送时的消息体
//        PushChannel pushChannel = new PushChannel();

        //安卓离线厂商通道推送的消息体
//        AndroidDTO androidDTO = new AndroidDTO();
//        Ups ups = new Ups();
//        //通知消息
//        ThirdNotification thirdNotification = new ThirdNotification();
//        ups.setNotification(thirdNotification);
//        thirdNotification.setTitle(title + "安卓离线厂商通道通知消息标题");
//        thirdNotification.setBody(content + "安卓离线厂商通道通知消息内容");
//        thirdNotification.setClickType("intent");
//        thirdNotification.setIntent("intent://com.getui.push/detail?#Intent;scheme=gtpushscheme;launchFlags=0x4000000;package=com.getui.demo;component=com.getui.demo/com.getui.demo.DemoActivity;S.payload=payloadStr;end");
//        //透传消息
////        ups.setTransmission(" {title:\"安卓离线厂商通道透传消息标题\",content:\"安卓离线厂商通道透传消息内容\",payload:\"自定义数据\"}");
//
//        androidDTO.setUps(ups);
//        pushChannel.setAndroid(androidDTO);

//        //ios离线apn通道推送的消息体
//        Alert alert = new Alert();
//        alert.setTitle(title+"苹果离线通知栏标题");
//        alert.setBody(content+"苹果离线通知栏内容");
//        Aps aps = new Aps();
//        aps.setContentAvailable(0);//0表示普通通知消息(默认为0);1表示静默推送(无通知栏消息)，静默推送时不需要填写其他参数。苹果建议1小时最多推送3条静默消息
//        aps.setSound("default");//自定义铃声:系统铃声设置为：default; 无声设置为：com.gexin.ios.silence，或不填
//        aps.setAlert(alert);
//        IosDTO iosDTO = new IosDTO();
//        iosDTO.setAps(aps);
//        iosDTO.setType("notify");
//        pushChannel.setIos(iosDTO);

//        pushDTO.setPushChannel(pushChannel);
        return pushDTO;
    }
}
