package com.younike.common.utils.push;


import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.PushPayload;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 极光推送（商家）
 * 2021-7-22 Another
 */
public class ShopPushUtils extends PushUtils{

    protected static final Logger logger = LoggerFactory.getLogger(ShopPushUtils.class);

    private static final String appKey = "133a68595a1ada3423119106";
    private static final String masterSecret = "7c3e686d7583a09553bd1b51";


    /**
     * 推送：指定别名（推单个）
     * @param alias 别名（商家id）
     * @param title 通知标题
     * @param alert 通知内容
     * @param type 通知提醒类型（跳转app指定页面）
     * @param id 通知提醒跳转app所需id
     */
    public static void pushOneClient(String alias, String title, String alert, String type, String id) {
        Map<String, String> extras = new HashMap<>();
        extras.put("type", type);
        extras.put("id", id);
        PushPayload payload = buildPushObject_androidAndIos_alias_alertWithTitle_extras(alias, title, alert, extras);

        sendPush(payload);
    }

    /**
     * 推送：指定别名（推多个）
     * @param aliasList 别名List（商家idList）
     * @param title 通知标题
     * @param alert 通知内容
     * @param type 通知提醒类型（跳转app指定页面）
     * @param id 通知提醒跳转app所需id
     */
    public static void pushMultipleClient(List<String> aliasList, String title, String alert, String type, String id) {
        Map<String, String> extras = new HashMap<>();
        extras.put("type", type);
        extras.put("id", id);

        // 一次最多推送1000个
        if (aliasList.size() > 1000) {
            // 若推送多余1000个，将其拆分成1000一组分组推送
            List<List<String>> lists = Lists.partition(aliasList,1000);
            for (List<String> list : lists) {
                PushPayload payload = buildPushObject_androidAndIos_aliasList_alertWithTitle_extras(list, title, alert, extras);
                sendPush(payload);
            }
        } else {
            PushPayload payload = buildPushObject_androidAndIos_aliasList_alertWithTitle_extras(aliasList, title, alert, extras);
            sendPush(payload);
        }

    }

    /**
     * 推送：所有平台、所有对象
     * @param alert 通知内容
     */
    public static void pushAll(String alert) {
        PushPayload payload = buildPushObject_all_all_alert(alert);

        sendPush(payload);
    }

    /**
     * 公共调用推送
     * @param payload
     */
    private static void sendPush(PushPayload payload) {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());

        try {
            PushResult result = jpushClient.sendPush(payload);

            logger.info("Got result - " + result);
        } catch (APIConnectionException e) {
            logger.error("Connection error. Should retry later. " + e);
        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. " + e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
        }
    }

}
