package cn.pluss.platform.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import mob.push.api.client.push.PushV3Client;
import mob.push.api.config.MobPushConfig;
import mob.push.api.http.Result;
import mob.push.api.model.*;
import mob.push.api.res.PushV3Res;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class MobPushUtil implements IPush {

    /**
     * 生产环境还是开发环境
     */
    public final static int PROD = 1;

    private static IPush instance;

    private MobPushUtil() {
    }

    public static IPush getInstance() {
        if (instance == null) {
            instance = new MobPushUtil();
        }

        return instance;
    }

    private static final String APP_KEY = "30db41bafd252";

    private static final String APP_SECRET = "5fc6143d7c4462c8d67650e94698554f";

    static {
        MobPushConfig.appkey = APP_KEY;
        MobPushConfig.appSecret = APP_SECRET;
    }

    @Override
    public void sendAndroidByAlias(List<String> alias, String notificationTitle, String msgContent, String extrasParam) {
        PushNotify notify = generalPushNotify(notificationTitle, msgContent, extrasParam);
        notify.setPlats(Sets.newHashSet(1));

        Push push = new Push();
        push.setPushNotify(notify);
        PushTarget pushTarget = new PushTarget();
        pushTarget.setAlias(Sets.newHashSet(alias));
        // 目标类型为别名
        pushTarget.setTarget(2);
        notify.setAndroidNotify(new AndroidNotify() {{
            // 普通通知
            setStyle(0);
            setSound("sound.caf");
        }});

        push.setPushTarget(pushTarget);

        sendAndResultHandle(push);
    }

    @Override
    public void sendIOSByAlias(List<String> alias, String notificationTitle, String msgContent, String extrasParam) {
        PushNotify notify = generalPushNotify(notificationTitle, msgContent, extrasParam);
        notify.setIosProduction(PROD);
        notify.setPlats(Sets.newHashSet(2));

        List<String> arrExtra = new ArrayList<>();
        for (String s : alias) {
            arrExtra.add(s);
            arrExtra.add("syb_" + s);
        }

        Push push = new Push();
        push.setPushNotify(notify);
        PushTarget pushTarget = new PushTarget();
        pushTarget.setAlias(Sets.newHashSet(arrExtra));
        // 目标类型为别名
        pushTarget.setTarget(2);
        notify.setIosNotify(new IosNotify() {{
            setMutableContent(1);
            setSound("sound.caf");
        }});

        push.setPushTarget(pushTarget);

        sendAndResultHandle(push);
    }

    @Override
    public void sendAllPlatByAlias(List<String> alias, String notificationTitle, String msgContent, String extrasParam) {
        PushNotify notify = generalPushNotify(notificationTitle, msgContent, extrasParam);
        notify.setIosProduction(PROD);
        notify.setPlats(Sets.newHashSet(1, 2));

        Push push = new Push();
        push.setPushNotify(notify);
        PushTarget pushTarget = new PushTarget();
        pushTarget.setAlias(Sets.newHashSet(alias));
        // 目标类型为别名
        pushTarget.setTarget(2);
        notify.setAndroidNotify(new AndroidNotify() {{
            // 普通通知
            setStyle(0);
            setSound("sound.caf");
        }});
        notify.setIosNotify(new IosNotify() {{
            setMutableContent(1);
            setSound("sound.caf");
        }});

        push.setPushTarget(pushTarget);

        sendAndResultHandle(push);
    }

    @Override
    public void sendIOSAll(String notificationTitle, String msgContent, String extrasParam) {
        PushNotify notify = generalPushNotify(notificationTitle, msgContent, extrasParam);
        notify.setIosProduction(PROD);
        notify.setPlats(Sets.newHashSet(2));


        Push push = new Push();
        push.setPushNotify(notify);
        PushTarget pushTarget = new PushTarget();
        // 广播，推送给全部用户
        pushTarget.setTarget(1);
        notify.setIosNotify(new IosNotify() {{
            setMutableContent(1);
            setSound("sound.caf");
        }});

        push.setPushTarget(pushTarget);

        sendAndResultHandle(push);
    }

    @Override
    public void sendAndroidAll(String notificationTitle, String msgContent, String extrasParam) {
        PushNotify notify = generalPushNotify(notificationTitle, msgContent, extrasParam);
        notify.setPlats(Sets.newHashSet(1));

        Push push = new Push();
        push.setPushNotify(notify);
        PushTarget pushTarget = new PushTarget();
        // 广播
        pushTarget.setTarget(1);
        notify.setAndroidNotify(new AndroidNotify() {{
            // 普通通知
            setStyle(0);
            setSound("sound.caf");
        }});

        push.setPushTarget(pushTarget);

        sendAndResultHandle(push);
    }

    @Override
    public void sendAllPlatAll(String notificationTitle, String msgContent, String extrasParam) {
        PushNotify notify = generalPushNotify(notificationTitle, msgContent, extrasParam);
        notify.setIosProduction(PROD);
        notify.setPlats(Sets.newHashSet(1, 2));

        Push push = new Push();
        push.setPushNotify(notify);
        PushTarget pushTarget = new PushTarget();
        // 目标类型为广播
        pushTarget.setTarget(1);
        notify.setAndroidNotify(new AndroidNotify() {{
            // 普通通知
            setStyle(0);
            setSound("sound.caf");
        }});
        notify.setIosNotify(new IosNotify() {{
            setMutableContent(1);
            setSound("sound.caf");
        }});

        push.setPushTarget(pushTarget);

        sendAndResultHandle(push);
    }

    private PushNotify generalPushNotify(String notificationTitle, String msgContent, String extrasParam) {
        PushNotify pushNotify = new PushNotify();
        PushMap pushMap = new PushMap();
        pushMap.setKey("url");
        pushMap.setValue(extrasParam);
        pushNotify.setExtrasMapList(Lists.newArrayList(pushMap));
        pushNotify.setOfflineSeconds(86400);
        pushNotify.setTitle(notificationTitle);
        pushNotify.setContent(msgContent);
        // type为1时，为通知
        pushNotify.setType(1);

        return pushNotify;
    }

    private void sendAndResultHandle(Push push) {
        try {
            Result<PushV3Res> pushV3ResResult = PushV3Client.pushTaskV3(push);
        } catch (Exception e) {
            System.out.println();
            log.error("[mob推送]  ", e.toString());
        }
    }
}
