package com.yn.bftl.thirdparty.modules.umeng.abstracts;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.HashSet;

/**
 * 友盟通知抽象类
 *
 * @author huabiao
 * @create 2023/12/5  15:48
 */
public abstract class AbstractUmengNotification {

    /**
     * 此JSONObject用于构造整个请求字符串
     */
    protected final JSONObject rootJson = new JSONObject();

    /**
     * APP的应用密钥
     */
    protected String appMasterSecret;

    /**
     * 允许设置进rootJson的根级别Key
     */
    protected static final HashSet<String> ROOT_KEYS = new HashSet<String>(Arrays.asList(new String[]{
            "appkey", "timestamp", "type", "device_tokens", "alias", "alias_type", "file_id",
            "filter", "production_mode", "feedback", "description", "thirdparty_id", "mipush", "mi_activity", "channel_properties"}));

    /**
     * 允许设置进rootJson的策略级别Key
     */
    protected static final HashSet<String> POLICY_KEYS = new HashSet<String>(Arrays.asList(new String[]{
            "start_time", "expire_time", "max_send_num"
    }));

    /**
     * 友盟通知-遵循规则将key-value设置进rootJson
     *
     * @param key   key值
     * @param value value值
     * @throws Exception
     * @description 由IOS和Android自行实现
     * @author huabiao
     * @create 2023/12/6  15:05
     */
    public abstract boolean setPredefinedKeyValue(String key, Object value) throws Exception;

    /**
     * 友盟通知-获取请求体的json字符串
     *
     * @return json字符串
     * @author huabiao
     * @create 2023/12/5  15:50
     */
    public String getPostBody() {
        return rootJson.toString();
    }

    /**
     * 友盟通知-设置应用密钥
     *
     * @param secret 应用密钥
     * @author huabiao
     * @create 2023/12/5  15:49
     */
    public void setAppMasterSecret(String secret) {
        appMasterSecret = secret;
    }

    /**
     * 友盟通知-获取应用密钥
     *
     * @return 应用密钥
     * @author huabiao
     * @create 2023/12/5  15:49
     */
    public final String getAppMasterSecret() {
        return appMasterSecret;
    }

    /**
     * 友盟通知-设置模式
     * <p>1、广播、组播下的测试模式只会将消息发给测试设备。测试设备需要到web上添加</p>
     * <p>2、单播、文件播不受测试设备限制</p>
     *
     * @param prod true:正式模式;false:测试模式
     * @author huabiao
     * @create 2023/12/5  15:45
     */
    protected void setProductionMode(Boolean prod) throws Exception {
        setPredefinedKeyValue("production_mode", prod.toString());
    }

    /**
     * 友盟通知-设置正式模式
     *
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setProductionMode() throws Exception {
        setProductionMode(true);
    }

    /**
     * 友盟通知-设置测试模式
     *
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setTestMode() throws Exception {
        setProductionMode(false);
    }

    /**
     * 友盟通知-设置发送消息描述
     *
     * @param description 发送消息描述，建议填写
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setDescription(String description) throws Exception {
        setPredefinedKeyValue("description", description);
    }

    /**
     * 友盟通知-设置定时发送时间
     *
     * @param startTime 发送时间，若不填写表示立即发送。格式: "YYYY-MM-DD hh:mm:ss"。
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setStartTime(String startTime) throws Exception {
        setPredefinedKeyValue("start_time", startTime);
    }

    /**
     * 友盟通知-设置消息过期时间
     *
     * @param expireTime 消息过期时间，格式: "YYYY-MM-DD hh:mm:ss"。
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setExpireTime(String expireTime) throws Exception {
        setPredefinedKeyValue("expire_time", expireTime);
    }

    /**
     * 友盟通知-设置发送限速
     *
     * @param num 每秒发送的最大条数
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setMaxSendNum(Integer num) throws Exception {
        setPredefinedKeyValue("max_send_num", num);
    }

    /**
     * 友盟通知-设置厂商弹窗activity
     *
     * @param activity 厂商弹窗activity
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setChannelActivity(String activity) throws Exception {
        setPredefinedKeyValue("mipush", "true");
        setPredefinedKeyValue("mi_activity", activity);
    }

    /**
     * 友盟通知-设置华为厂商属性配置
     *
     * @param channelActivity 系统弹窗完整包路径
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setHuaweiChannelProperties(String channelActivity) throws Exception {
        JSONObject channelProperties = new JSONObject();
        channelProperties.put("main_activity", channelActivity);
        channelProperties.put("channel_activity", channelActivity);
        channelProperties.put("huawei_channel_importance", "NORMAL");
        channelProperties.put("huawei_channel_category", "IM");
        setPredefinedKeyValue("channel_properties", channelProperties);
        // 设置角标
        setPredefinedKeyValue("add_badge", 1);
    }

    /**
     * 友盟通知-设置小米厂商属性配置
     *
     * @param xiaoMiChannelId 小米厂商属性配置
     * @param channelActivity 系统弹窗完整包路径
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setXiaoMiChannelProperties(String xiaoMiChannelId, String channelActivity) throws Exception {
        JSONObject channelProperties = new JSONObject();
        channelProperties.put("channel_activity", channelActivity);
        channelProperties.put("xiaomi_channel_id", xiaoMiChannelId);
        setPredefinedKeyValue("channel_properties", channelProperties);
    }

    /**
     * 友盟通知-设置OPPO厂商属性配置
     *
     * @param oppoChannelId   OPPO厂商属性配置
     * @param channelActivity 系统弹窗完整包路径
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setOppoChannelProperties(String oppoChannelId, String channelActivity) throws Exception {
        JSONObject channelProperties = new JSONObject();
        channelProperties.put("channel_activity", channelActivity);
        channelProperties.put("oppo_channel_id", oppoChannelId);
        setPredefinedKeyValue("channel_properties", channelProperties);
    }

    /**
     * 友盟通知-设置VIVO厂商属性配置
     *
     * @param channelActivity 系统弹窗完整包路径
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setVivoChannelProperties(String channelActivity) throws Exception {
        JSONObject channelProperties = new JSONObject();
        channelProperties.put("channel_activity", channelActivity);
        channelProperties.put("vivo_category", "IM");
        setPredefinedKeyValue("channel_properties", channelProperties);
    }

    /**
     * 友盟通知-设置魅族厂商属性配置
     *
     * @param channelActivity 系统弹窗完整包路径
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setMeizuChannelProperties(String channelActivity) throws Exception {
        JSONObject channelProperties = new JSONObject();
        channelProperties.put("channel_activity", channelActivity);
        setPredefinedKeyValue("channel_properties", channelProperties);
    }

    /**
     * 友盟通知-设置荣耀厂商属性配置
     *
     * @param channelActivity 系统弹窗完整包路径
     * @author huabiao
     * @create 2023/12/5  15:41
     */
    public void setHonorChannelProperties(String channelActivity) throws Exception {
        JSONObject channelProperties = new JSONObject();
        channelProperties.put("channel_activity", channelActivity);
        channelProperties.put("huawei_channel_importance", "NORMAL");
        channelProperties.put("huawei_channel_category", "IM");
        setPredefinedKeyValue("channel_properties", channelProperties);
    }
}
