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

import com.alibaba.fastjson.JSONObject;
import com.yn.bftl.thirdparty.modules.umeng.abstracts.AbstractUmengNotification;
import com.yn.bftl.thirdparty.modules.umeng.enums.AfterOpenAction;
import com.yn.bftl.thirdparty.modules.umeng.enums.DisplayType;

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

/**
 * 安卓通知抽象类
 *
 * @author huabiao
 * @create 2023/12/5  15:48
 */
public abstract class AbstractAndroidNotificationAbstract extends AbstractUmengNotification {

    /**
     * 允许设置进rootJson的有效负载级别Key
     */
    protected static final HashSet<String> PAYLOAD_KEYS = new HashSet<String>(Arrays.asList(new String[]{
            "display_type"}));

    /**
     * 允许设置进rootJson的请求体级别Key
     */
    protected static final HashSet<String> BODY_KEYS = new HashSet<String>(Arrays.asList(new String[]{
            "ticker", "title", "text", "builder_id", "icon", "largeIcon", "img", "play_vibrate", "play_lights", "play_sound",
            "sound", "after_open", "url", "activity", "custom", "add_badge"}));

    /**
     * IOS通知-设置设置rootJson的参数
     *
     * @param key   key值
     * @param value value值
     * @return true:设置成功;false:设置失败
     * @author huabiao
     * @create 2023/12/6  11:32
     */
    @Override
    public boolean setPredefinedKeyValue(String key, Object value) throws Exception {
        if (ROOT_KEYS.contains(key)) {
            // 设置根级别参数
            rootJson.put(key, value);
        } else if (PAYLOAD_KEYS.contains(key)) {
            // 设置有效负载级别参数
            JSONObject payloadJson;
            if (rootJson.containsKey("payload")) {
                payloadJson = rootJson.getJSONObject("payload");
            } else {
                payloadJson = new JSONObject();
                rootJson.put("payload", payloadJson);
            }
            payloadJson.put(key, value);
        } else if (BODY_KEYS.contains(key)) {
            // 设置请求体级别参数
            JSONObject bodyJson;
            JSONObject payloadJson;
            // 若payload不存在，则构建一个payload
            if (rootJson.containsKey("payload")) {
                payloadJson = rootJson.getJSONObject("payload");
            } else {
                payloadJson = new JSONObject();
                rootJson.put("payload", payloadJson);
            }
            // 获取body的数据，若不存在则构建
            if (payloadJson.containsKey("body")) {
                bodyJson = payloadJson.getJSONObject("body");
            } else {
                bodyJson = new JSONObject();
                payloadJson.put("body", bodyJson);
            }
            bodyJson.put(key, value);
        } else if (POLICY_KEYS.contains(key)) {
            // 设置策略级别参数
            JSONObject policyJson;
            if (rootJson.containsKey("policy")) {
                policyJson = rootJson.getJSONObject("policy");
            } else {
                policyJson = new JSONObject();
                rootJson.put("policy", policyJson);
            }
            policyJson.put(key, value);
        } else {
            if (key == "payload" || key == "body" || key == "policy" || key == "extra") {
                throw new Exception("You don't need to set value for " + key + " , just set values for the sub keys in it.");
            } else {
                throw new Exception("Unknown key: " + key);
            }
        }
        return true;
    }

    /**
     * 安卓通知-设置自定义key-value
     *
     * @param key   key值
     * @param value value值
     * @description 可选，JSON格式，用户自定义key-value。
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public boolean setExtraField(String key, String value) {
        JSONObject payloadJson;
        JSONObject extraJson;
        if (rootJson.containsKey("payload")) {
            payloadJson = rootJson.getJSONObject("payload");
        } else {
            payloadJson = new JSONObject();
            rootJson.put("payload", payloadJson);
        }
        if (payloadJson.containsKey("extra")) {
            extraJson = payloadJson.getJSONObject("extra");
        } else {
            extraJson = new JSONObject();
            payloadJson.put("extra", extraJson);
        }
        extraJson.put(key, value);
        return true;
    }

    /**
     * 安卓通知-设置消息类型
     *
     * @param displayType 消息类型
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setDisplayType(DisplayType displayType) throws Exception {
        setPredefinedKeyValue("display_type", displayType.getValue());
    }

    /**
     * 安卓通知-设置通知栏提示文字
     *
     * @param ticker 通知栏提示文字
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setTicker(String ticker) throws Exception {
        setPredefinedKeyValue("ticker", ticker);
    }

    /**
     * 安卓通知-设置通知标题
     *
     * @param title 通知标题
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setTitle(String title) throws Exception {
        setPredefinedKeyValue("title", title);
    }

    /**
     * 安卓通知-设置通知文字描述
     *
     * @param text 通知文字描述
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setText(String text) throws Exception {
        setPredefinedKeyValue("text", text);
    }

    /**
     * 安卓通知-设置自定义通知样式
     *
     * @param builderId 自定义通知样式
     * @author huabiao
     * @description 可选，默认为0，用于标识该通知采用的样式。使用该参数时开发者必须在SDK里面实现自定义通知栏样式
     * @create 2023/12/6  10:55
     */
    public void setBuilderId(Integer builderId) throws Exception {
        setPredefinedKeyValue("builder_id", builderId);
    }

    /**
     * 安卓通知-设置自定义通知图标
     *
     * @param icon 自定义通知图标
     * @description 可选，状态栏图标ID, R.drawable.[smallIcon],如果没有, 默认使用应用图标。
     * @author huabiao
     * @create 2023/12/6  14:55
     */
    public void setIcon(String icon) throws Exception {
        setPredefinedKeyValue("icon", icon);
    }

    /**
     * 卓通知-设置通知栏拉开后左侧图标
     *
     * @param largeIcon 通知栏拉开后左侧图标ID
     * @author huabiao
     * @create 2023/12/6  14:56
     */
    public void setLargeIcon(String largeIcon) throws Exception {
        setPredefinedKeyValue("largeIcon", largeIcon);
    }

    /**
     * 卓通知-设置通知栏大图标的URL链接
     *
     * @param img 通知栏大图标的URL链接
     * @description 通知栏大图标的URL链接。该字段的优先级大于largeIcon。该字段要求以http或者https开头。
     * @author huabiao
     * @create 2023/12/6  14:56
     */
    public void setImg(String img) throws Exception {
        setPredefinedKeyValue("img", img);
    }

    /**
     * 安卓通知-设置通知震动
     *
     * @param playVibrate
     * @description 收到通知是否震动, 默认为"true"
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setPlayVibrate(Boolean playVibrate) throws Exception {
        setPredefinedKeyValue("play_vibrate", playVibrate.toString());
    }

    /**
     * 安卓通知-设置通知闪灯
     *
     * @param playLights
     * @description 收到通知是否闪灯, 默认为"true"
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setPlayLights(Boolean playLights) throws Exception {
        setPredefinedKeyValue("play_lights", playLights.toString());
    }

    /**
     * 安卓通知-设置通知是否发出声音
     *
     * @param playSound 通知是否发出声音，默认为"true"
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setPlaySound(Boolean playSound) throws Exception {
        setPredefinedKeyValue("play_sound", playSound.toString());
    }

    /**
     * 安卓通知-设置通知声音
     *
     * @param sound 通知声音
     * @description 通知声音，R.raw.[sound]. 如果该字段为空，采用SDK默认的声音
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setSound(String sound) throws Exception {
        setPredefinedKeyValue("sound", sound);
    }

    /**
     * 安卓通知-设置通知后播放指定声音
     *
     * @param sound 通知声音
     * @description 通知声音，R.raw.[sound]. 如果该字段为空，采用SDK默认的声音
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void setPlaySound(String sound) throws Exception {
        setPlaySound(true);
        setSound(sound);
    }

    /**
     * 安卓通知-设置点击"通知"的后续行为
     *
     * @description 点击"通知"的后续行为，默认为打开app。
     * @author huabiao
     * @create 2023/12/6  10:55
     */
    public void goAppAfterOpen() throws Exception {
        setAfterOpenAction(AfterOpenAction.GO_APP);
    }

    /**
     * 安卓通知-跳转到指定URL
     *
     * @param url 跳转URL
     * @description
     * @author huabiao
     * @create 2023/12/6  10:27
     */
    public void goUrlAfterOpen(String url) throws Exception {
        setAfterOpenAction(AfterOpenAction.GO_URL);
        setUrl(url);
    }

    /**
     * 安卓通知-打开特定的activity
     *
     * @param activity 特定的activity
     * @description
     * @author huabiao
     * @create 2023/12/6  10:27
     */
    public void goActivityAfterOpen(String activity) throws Exception {
        setAfterOpenAction(AfterOpenAction.GO_ACTIVITY);
        setActivity(activity);
    }

    /**
     * 安卓通知-打开特定的activity
     *
     * @param custom 特定的activity
     * @description
     * @author huabiao
     * @create 2023/12/6  10:27
     */
    public void goCustomAfterOpen(String custom) throws Exception {
        setAfterOpenAction(AfterOpenAction.GO_CUSTOM);
        setCustomField(custom);
    }

    /**
     * 安卓通知-打开特定的activity
     *
     * @param custom 特定的activity
     * @description
     * @author huabiao
     * @create 2023/12/6  10:27
     */
    public void goCustomAfterOpen(JSONObject custom) throws Exception {
        setAfterOpenAction(AfterOpenAction.GO_CUSTOM);
        setCustomField(custom);
    }

    /**
     * 安卓通知-设置后续操作
     *
     * @param afterOpenAction 后续操作
     * @description 默认为打开APP
     * @author huabiao
     * @create 2023/12/6  10:28
     */
    public void setAfterOpenAction(AfterOpenAction afterOpenAction) throws Exception {
        setPredefinedKeyValue("after_open", afterOpenAction.getValue());
    }

    /**
     * 安卓通知-设置跳转URL
     *
     * @param url 跳转URL
     * @author huabiao
     * @create 2023/12/6  10:28
     */
    public void setUrl(String url) throws Exception {
        setPredefinedKeyValue("url", url);
    }

    /**
     * 安卓通知-设置特定activity
     *
     * @param activity activity
     * @author huabiao
     * @create 2023/12/6  10:28
     */
    public void setActivity(String activity) throws Exception {
        setPredefinedKeyValue("activity", activity);
    }

    /**
     * 安卓通知-设置用户自定义内容
     *
     * @param custom 自定义内容
     * @author huabiao
     * @create 2023/12/6  10:28
     */
    public void setCustomField(String custom) throws Exception {
        setPredefinedKeyValue("custom", custom);
    }

    /**
     * 安卓通知-设置用户自定义内容
     *
     * @param custom 自定义内容
     * @author huabiao
     * @create 2023/12/6  10:28
     */
    public void setCustomField(JSONObject custom) throws Exception {
        setPredefinedKeyValue("custom", custom);
    }
}
