package cn.com.ty.lift.push.app;

import com.tencent.xinge.bean.AudienceType;
import com.tencent.xinge.bean.Message;
import com.tencent.xinge.bean.MessageType;
import com.tencent.xinge.bean.Platform;
import com.tencent.xinge.push.app.PushAppRequest;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;

/**
 * 消息推送参数封装，请求体
 *
 * @author wcz
 * @since 2020/2/10
 */
@Data
@AllArgsConstructor
public class MsgRequest {

    public static final Integer Android = 1;
    public static final Integer Ios = 2;
    /**
     * 消息标题
     */
    private String title;
    /**
     * 消息内容
     */
    private String content;

    /**
     * 消息推送的平台 1:andorid 2:ios
     */
    private Integer platform;

    /**
     * 消息是否全平台发布(平台内所有账号全部推动)
     */
    private Boolean allPlatform;

    /**
     * 发送的账号列表
     */
    private ArrayList<String> accountList;

    public static MsgRequest me(String title,String content,Integer platform,Boolean allPlatform,ArrayList<String> accountList){
        return new MsgRequest(title,content,platform,allPlatform,accountList);
    }

    public static MsgRequest android(String title,String content,ArrayList<String> accountList){
        return new MsgRequest(title,content,Android,false,accountList);
    }

    public static MsgRequest android(String title,String content){
        return new MsgRequest(title,content,Android,true,null);
    }

    public static MsgRequest ios(String title,String content,ArrayList<String> accountList){
        return new MsgRequest(title,content,Ios,false,accountList);
    }

    public static MsgRequest ios(String title,String content){
        return new MsgRequest(title,content,Ios,true,null);
    }

    public boolean isAndroid(){
        return this.getPlatform() == Android;
    }

    public boolean isIos(){
        return this.getPlatform() == Ios;
    }

    public PushAppRequest pushAppRequest(){
        /**
         * https://xg.qq.com/docs/server_api/v3/push_api_v3.html#message%EF%BC%9A%E6%B6%88%E6%81%AF%E4%BD%93
         */
        Message message = new Message();
        message.setTitle(this.getTitle());
        message.setContent(this.getContent());
        //推送请求信息
        PushAppRequest pushAppRequest = new PushAppRequest();
        //消息体
        pushAppRequest.setMessage(message);
        /**
         * 消息类型
         1）notify：通知
         2）message：透传消息/静默消息
         */
        pushAppRequest.setMessage_type(MessageType.notify);
        /**
         * 客户端平台类型
         1）android：安卓
         2）ios：苹果
         */
        if (this.isAndroid()) {
            pushAppRequest.setPlatform(Platform.android);
        } else if (this.isIos()) {
            pushAppRequest.setPlatform(Platform.ios);
        } else {
            return null;
        }
        /**
         * 推送目标
         1）all：全量推送
         2）tag：标签推送
         3）token：单设备推送
         4）token_list：设备列表推送
         5）account：单账号推送
         6）account_list：账号列表推送
         */

        if (this.getAllPlatform()) {
            pushAppRequest.setAudience_type(AudienceType.all);
        } else {
            ArrayList<String> accountList = this.getAccountList();
            if (null == accountList || accountList.isEmpty()) {
                return null;
            }
            if (accountList.size() == 1) {
                pushAppRequest.setAudience_type(AudienceType.account);
            } else {
                pushAppRequest.setAudience_type(AudienceType.account_list);
            }
            //推送账号
            pushAppRequest.setAccount_list(accountList);
        }

        //打印下推送数据
        /**
         * {
         * 	"audience_type": "account",
         * 	"platform": "android",
         * 	"message_type": "notify",
         * 	"message": {
         * 		"title": "天域电梯测试",
         * 		"content": "测试信鸽消息推送，当前时间: 2019-12-24 17:03:15",
         * 		"accept_time": null,
         * 		"android": null,
         * 		"ios": null
         *        },
         * 	"expire_time": 259200,
         * 	"send_time": null,
         * 	"multi_pkg": false,
         * 	"loop_times": 0,
         * 	"loop_interval": 0,
         * 	"environment": "product",
         * 	"stat_tag": null,
         * 	"seq": 0,
         * 	"tag_list": null,
         * 	"account_list": ["18571500256"],
         * 	"account_type": 0,
         * 	"token_list": [],
         * 	"push_id": null
         * }
         */
        return pushAppRequest;
    }
}
