package com.izouma.nineth.service;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.Message;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.izouma.nineth.config.PushProperties;
import com.izouma.nineth.dto.PushMsgDTO;
import com.izouma.nineth.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 推送服务
 */
@Service
@AllArgsConstructor
@Slf4j
public class PushService {


    /**
     * 默认失效时间
     */
    public static final long DEFAULT_EXPIRE_TIME =  1000 * 3600;

    private PushProperties pushProperties;

    private IGtPush iGtPush;



    /**
     * 推送通知
     * @param pushMsgDTO
     * @return
     */
    public IPushResult pushNotification(PushMsgDTO pushMsgDTO){
        Style0 style = new Style0();
        // STEP2：设置推送标题、推送内容
        if(StringUtils.isEmpty(pushMsgDTO.getTitle()) || StringUtils.isEmpty(pushMsgDTO.getContent())){
            throw new BusinessException("推送的标题和消息不能为空");
        }

        style.setTitle(pushMsgDTO.getTitle());
        style.setText(pushMsgDTO.getContent());
        style.setLogo("push.png");  // 设置推送图标
        // STEP3：设置响铃、震动等推送效果
        style.setRing(true);  // 设置响铃
        style.setVibrate(true);  // 设置震动


        // STEP4：选择通知模板
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(pushProperties.getAppId());
        template.setAppkey(pushProperties.getAppKey());
        template.setStyle(style);


        // STEP5：定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
        IPushResult ret = null;
        if(StringUtils.isNotEmpty(pushMsgDTO.getClientId()) ){
            SingleMessage singleMessage = new SingleMessage();
            singleMessage.setData(template);
            singleMessage.setOffline(true);
            singleMessage.setOfflineExpireTime(DEFAULT_EXPIRE_TIME);  // 时间单位为毫秒
            Target target = new Target();
            target.setAppId(pushProperties.getAppId());
            target.setClientId(pushMsgDTO.getClientId());
            // STEP6：执行推送
            ret = iGtPush.pushMessageToSingle(singleMessage, target);
        }else{
            List<String> appIds = new ArrayList<>();
            appIds.add(pushProperties.getAppId());
            AppMessage appMessage = new AppMessage();
            appMessage.setData(template);
            appMessage.setAppIdList(appIds);
            appMessage.setOffline(true);
            appMessage.setOfflineExpireTime(DEFAULT_EXPIRE_TIME);  // 时间单位为毫秒
            // STEP6：执行推送
            ret = iGtPush.pushMessageToApp(appMessage);
        }

        return ret;
    }





}
