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

import com.alibaba.fastjson.JSONObject;
import com.yn.bftl.thirdparty.modules.umeng.abstracts.AbstractUmengNotification;
import org.apache.commons.lang3.StringUtils;

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

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

    /**
     * 允许设置进rootJson的aps级别Key
     */
    protected static final HashSet<String> APS_KEYS = new HashSet<>(Arrays.asList(new String[]{
            "alert", "badge", "sound", "content-available"
    }));

    /**
     * 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 (APS_KEYS.contains(key)) {
            // 设置aps级别参数
            JSONObject apsJson;
            JSONObject payloadJson;
            if (rootJson.containsKey("payload")) {
                payloadJson = rootJson.getJSONObject("payload");
            } else {
                payloadJson = new JSONObject();
                rootJson.put("payload", payloadJson);
            }
            if (payloadJson.containsKey("aps")) {
                apsJson = payloadJson.getJSONObject("aps");
            } else {
                apsJson = new JSONObject();
                payloadJson.put("aps", apsJson);
            }
            apsJson.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 == "aps" || key == "policy") {
                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("Unknownd key: " + key);
            }
        }

        return true;
    }

    /**
     * IOS通知-设置自定义key-value参数
     *
     * @param key   key值
     * @param value value值
     * @author huabiao
     * @create 2023/12/6  11:27
     */
    public boolean setCustomizedField(String key, String value) {
        JSONObject payloadJson;
        if (rootJson.containsKey("payload")) {
            payloadJson = rootJson.getJSONObject("payload");
        } else {
            payloadJson = new JSONObject();
            rootJson.put("payload", payloadJson);
        }
        payloadJson.put(key, value);
        return true;
    }

    /**
     * IOS通知-设置提醒消息（字符串）
     *
     * @param token
     * @return
     * @description
     * @author huabiao
     * @create 2023/12/6  11:28
     */
    public void setAlert(String token) throws Exception {
        setPredefinedKeyValue("alert", token);
    }

    /**
     * IOS通知-设置提醒消息(字典)
     *
     * @param title    标题
     * @param subtitle 副标题
     * @param body     消息内容
     * @author huabiao
     * @create 2023/12/6  11:17
     */
    public void setAlert(String title, String subtitle, String body) throws Exception {
        JSONObject object = new JSONObject();
        object.put("title", title);
        if (StringUtils.isNotBlank(subtitle)) {
            object.put("subtitle", subtitle);
        }
        object.put("body", body);
        setPredefinedKeyValue("alert", object);
    }

    /**
     * IOS通知-设置角标
     * <p>1、N（代表设置角标为N）</p>
     * <p>2、+N（代表角标原有基础上+N）</p>
     * <p>3、-N（代表角标原有基础上-N）</p>
     * <p>4、空字符串（代表清空角标，同N=0）</p>
     *
     * @param badge 角标值
     * @author huabiao
     * @create 2023/12/6  11:19
     */
    public void setBadge(String badge) throws Exception {
        setPredefinedKeyValue("badge", badge);
    }

    /**
     * IOS通知-设置声音
     * <p>1、如果该字段为空，采用SDK默认的声音，即res/raw/下的umeng_push_notification_default_sound声音文件</p>
     * <p>2、如果SDK默认声音文件不存在，则使用系统默认Notification提示音</p>
     *
     * @param sound 通知声音
     * @author huabiao
     * @create 2023/12/6  11:23
     */
    public void setSound(String sound) throws Exception {
        setPredefinedKeyValue("sound", sound);
    }

    /**
     * IOS通知-设置静默推送
     *
     * @param contentAvailable 1:静默推送
     * @author huabiao
     * @create 2023/12/6  11:29
     */
    public void setContentAvailable(Integer contentAvailable) throws Exception {
        setPredefinedKeyValue("content-available", contentAvailable);
    }
}