package org.tio.sitexxx.service.service.jpush.huawei;

import com.alibaba.fastjson.JSONObject;
import org.tio.sitexxx.service.service.jpush.JPushService;
import org.tio.sitexxx.service.service.jpush.huawei.android.*;
import org.tio.sitexxx.service.service.jpush.huawei.exception.HuaweiMesssagingException;
import org.tio.sitexxx.service.service.jpush.huawei.message.AndroidConfig;
import org.tio.sitexxx.service.service.jpush.huawei.message.Message;
import org.tio.sitexxx.service.service.jpush.huawei.message.Notification;
import org.tio.sitexxx.service.service.jpush.huawei.messaging.HuaweiApp;
import org.tio.sitexxx.service.service.jpush.huawei.messaging.HuaweiMessaging;
import org.tio.sitexxx.service.service.jpush.huawei.model.Importance;
import org.tio.sitexxx.service.service.jpush.huawei.model.Urgency;
import org.tio.sitexxx.service.service.jpush.huawei.model.Visibility;
import org.tio.sitexxx.service.service.jpush.huawei.response.SendResponse;
import org.tio.sitexxx.service.service.jpush.huawei.util.InitAppUtils;
import org.tio.sitexxx.service.service.jpush.huawei.android.*;

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

/**
 * <pre>
 *    @author : vagrancy
 *    e-mail : 18050829067@163.com
 *    time   : 2025-02-21 09:58
 *    desc   :
 * </pre>
 */
public class Huawei {
    public static void sendNotification() throws HuaweiMesssagingException {
        HuaweiApp app = InitAppUtils.initializeApp();
        HuaweiMessaging huaweiMessaging = HuaweiMessaging.getInstance(app);

        Notification notification = Notification.builder().setTitle("sample title")
                .setBody("sample message body")
                .build();

        JSONObject multiLangKey = new JSONObject();
        JSONObject titleKey = new JSONObject();
        titleKey.put("en","好友请求");
        JSONObject bodyKey = new JSONObject();
        bodyKey.put("en","My name is %s, I am from %s.");
        multiLangKey.put("key1", titleKey);
        multiLangKey.put("key2", bodyKey);

        LightSettings lightSettings = LightSettings.builder().setColor(Color.builder().setAlpha(0f).setRed(0f).setBlue(1f).setGreen(1f).build())
                .setLightOnDuration("3.5")
                .setLightOffDuration("5S")
                .build();

        AndroidNotification androidNotification = AndroidNotification.builder().setIcon("/raw/ic_launcher2")
                .setColor("#AACCDD")
                .setSound("/raw/shake")
                .setDefaultSound(true)
                .setTag("tagBoom")
                .setClickAction(ClickAction.builder().setType(2).setUrl("https://www.huawei.com").build())
                .setBodyLocKey("key2")
                .addBodyLocArgs("boy").addBodyLocArgs("dog")
                .setTitleLocKey("key1")
                .addTitleLocArgs("Girl").addTitleLocArgs("Cat")
                .setChannelId("Your Channel ID")
                .setNotifySummary("some summary")
                .setMultiLangkey(multiLangKey)
                .setStyle(1)
                .setBigTitle("Big Boom Title")
                .setBigBody("Big Boom Body")
                .setAutoClear(86400000)
                .setNotifyId(486)
                .setGroup("Group1")
                .setImportance(Importance.LOW.getValue())
                .setLightSettings(lightSettings)
                .setBadge(BadgeNotification.builder().setAddNum(1).setBadgeClass("Classic").build())
                .setVisibility(Visibility.PUBLIC.getValue())
                .setForegroundShow(true)
                .addInboxContent("content1").addInboxContent("content2").addInboxContent("content3").addInboxContent("content4").addInboxContent("content5")
                .addButton(Button.builder().setName("button1").setActionType(0).build())
                .addButton(Button.builder().setName("button2").setActionType(1).setIntentType(0).setIntent("https://com.huawei.hms.hmsdemo/deeplink").build())
                .addButton(Button.builder().setName("button3").setActionType(4).setData("your share link").build())
                .build();

        AndroidConfig androidConfig = AndroidConfig.builder().setCollapseKey(-1)
                .setUrgency(Urgency.HIGH.getValue())
                .setTtl("10000s")
                .setBiTag("the_sample_bi_tag_for_receipt_service")
                .setNotification(androidNotification)
                .build();

        Message message = Message.builder().setNotification(notification)
                .setAndroidConfig(androidConfig)
                .addToken("IQAAAACy1ImUAABH18hDkIn_DOeA-9eAqQRAx8zp-ZRFMgfwmpphuF2pv3rUyXFjh29XDumDsrwNkQPJvmVxgyVI7pKcDDxpN8rGnCSkplqJqOp3vw")
                .build();

        SendResponse response = huaweiMessaging.sendMessage(message);
    }

    public static void send(String token, String title, String msg) {
        List<String> tokens = new ArrayList<>();
        tokens.add(token);
        send(tokens, title, msg);
    }

    public static void send(List<String> token, String title, String msg) {
        HuaweiApp app = InitAppUtils.initializeApp();
        HuaweiMessaging huaweiMessaging = HuaweiMessaging.getInstance(app);

        Notification notification = Notification.builder()
                .setTitle(title)
                .setBody(msg)
                .build();

        AndroidNotification androidNotification = AndroidNotification.builder()
                .setDefaultSound(true)
                .setClickAction(ClickAction.builder().setType(3).build())
                .setNotifyId(486)
                .setTitle(msg)
                .setBody(msg)
                .setImportance(Importance.LOW.getValue())
                .setBadge(BadgeNotification.builder().setAddNum(1)
                        .setBadgeClass("com.tengabai.show.feature.splash.SplashActivity").build())
                .setVisibility(Visibility.PUBLIC.getValue())
                .setForegroundShow(true)
                .build();

        AndroidConfig androidConfig = AndroidConfig.builder()
                .setCategory("IM")
                .setCollapseKey(-1)
                .setUrgency(Urgency.HIGH.getValue())
                .setNotification(androidNotification)
                .build();

        Message message = Message.builder().setNotification(notification)
                .setAndroidConfig(androidConfig)
                .addAllToken(token)
                .build();
        try {
            SendResponse response = huaweiMessaging.sendMessage(message);
            if (JPushService.IS_DEBUG) {
                System.out.println("code:"+response.getCode()+",msg:"+response.getMsg()+",requestId:"+response.getRequestId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String args[]) throws Exception {
         // 获取鉴权令牌
        send("IQAAAACy1ImUAABH18hDkIn_DOeA-9eAqQRAx8zp-ZRFMgfwmpphuF2pv3rUyXFjh29XDumDsrwNkQPJvmVxgyVI7pKcDDxpN8rGnCSkplqJqOp3vw", "你好文本", "你不好");
    }
}
