package com.ruoyi.app.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.utils.event.PushEventService;
import com.ruoyi.common.utils.LocaleUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.List;
import java.util.Locale;

public class PayPush {
    private static final Logger log = LoggerFactory.getLogger(PayPush.class);

    //用户端推送
    public void apppush(String cid, String title, String content, String body) {
        System.out.println("用户端推送cid:" + cid);
        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msduser?cid=" + cid +
                "&title=" + title + "&content=" + content + "&payload=" + body;
        String res = "";
        res = HttpUtil.get(url);
        JSONObject jsonObject = JSON.parseObject(res);
        System.out.println("=========================1111111111111111" + res);
    }

    //用户端批量推送
    public void userPushBatch(List<String> cid, String title, String content, String body) {
        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msduser";

        JSONObject json = new JSONObject();
        // cid 是数组/列表，不能直接字符串拼接，否则会得到 [Ljava.lang.String;@xxxx
        json.put("cid", cid);
        json.put("title", title);
        json.put("content", content);
        // payload 建议作为 JSON 对象传递，避免双引号转义问题
        json.put("payload", body == null ? null : JSON.parse(body));

        String jsonBody = json.toJSONString();
        String res = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .body(jsonBody)
                .execute()
                .body();
        System.out.println("-------用户批量推送");
    }

    //骑手端推送
    public void qspush(String cid, String title, String content, String body) {
        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msdrider?cid=" + cid +
                "&title=" + title + "&content=" + content + "&payload=" + body + "&sound=pushsound.caf";
        String res = "";
        res = HttpUtil.get(url);
        JSONObject jsonObject = JSON.parseObject(res);
        System.out.println("=========================用户推送" + res);
    }

    //用户端批量推送
    public void qsPushBatch(List<String> cid, String title, String content, String body) {
        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msdrider";

        JSONObject json = new JSONObject();
        // cid 是数组/列表，不能直接字符串拼接，否则会得到 [Ljava.lang.String;@xxxx
        json.put("cid", cid);
        json.put("title", title);
        json.put("content", content);
        // payload 建议作为 JSON 对象传递，避免双引号转义问题
        json.put("payload", body == null ? null : JSON.parse(body));

        String jsonBody = json.toJSONString();
        String res = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .body(jsonBody)
                .execute()
                .body();
        System.out.println("-------用户批量推送");
    }

    //商家端推送
    public void shpush(String cid, String title, String content, String body) {
        System.out.println("商家cid:" + cid);
        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msdstore?cid=" + cid +
                "&title=" + title + "&content=" + content + "&payload=" + body;
        String res = "";
        res = HttpUtil.get(url);
        JSONObject jsonObject = JSON.parseObject(res);
        System.out.println("=========================商家推送" + res);
    }

    /**
     * 商家推送
     *
     * @param push
     * @param pushEventService
     * @param userId
     * @param cid
     * @param title
     * @param content
     * @param body
     */
    public static void shPush(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType) {
        if (pushEventService != null) {
            pushEventService.PublisherEvent(userId, title, content, body);
        }
        if (cid == null || cid.trim().isEmpty()) {
            log.error("商家推送失败：Device token is empty, userId:{}", userId);
            return;
        }

        push.shpush(cid, title, content, body);


    }

    /**
     * 发送前设置local信息
     *
     * @param push
     * @param pushEventService
     * @param userId
     * @param cid
     * @param title
     * @param content
     * @param body
     * @param cidType
     * @param ddId
     */
    public static void shPushHandleLocal(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType, String ddId) {
        Locale local = LocaleUtils.getUserLocale(userId);
        LocaleContextHolder.setLocale(local);
        title = MessageUtils.message(title);
        content = MessageUtils.message(content) + ",NO:" + ddId;
        shPush(push, pushEventService, userId, cid, title, content, body, cidType);
    }

    /**
     * 骑手推送
     *
     * @param pushEventService
     * @param userId
     * @param cid
     * @param title
     * @param content
     * @param body
     */
    public static void qsPush(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType) {
        if (pushEventService != null) {
            pushEventService.PublisherEvent(userId, title, content, body);
        }
        if (cid == null || cid.trim().isEmpty()) {
            log.error("骑手推送失败：Device token is empty, userId:{}", userId);
            return;
        }
        log.info("useId{}:cid{}:cidType{}:content:{}", userId, cid, cidType, content);
        log.info("给骑手发送进入uni推送:{},{},{},{}", userId, cid, cidType, content);
        push.qspush(cid, title, content, body);
    }

    /**
     * 发送前设置local信息
     *
     * @param push
     * @param pushEventService
     * @param userId
     * @param cid
     * @param title
     * @param content
     * @param body
     * @param cidType
     * @param ddId
     */
    public static void qsPushHandleLocal(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType, String ddId) {
        Locale local = LocaleUtils.getUserLocale(userId);
        LocaleContextHolder.setLocale(local);
        title = MessageUtils.message(title);
        content = MessageUtils.message(content) + ",NO:" + ddId;
        qsPush(push, pushEventService, userId, cid, title, content, body, cidType);
    }

    /**
     * 用户推送
     *
     * @param push
     * @param pushEventService
     * @param userId
     * @param cid
     * @param title
     * @param content
     * @param body
     */
    public static void userPush(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType) {
        if (pushEventService != null) {
            pushEventService.PublisherEvent(userId, title, content, body);
        }
        if (cid == null || cid.trim().isEmpty()) {
            log.error("Device token is empty, cannot send push message");
            return;
        }
        push.apppush(cid, title, content, body);
    }

    /**
     * 发送前设置local信息
     *
     * @param push
     * @param pushEventService
     * @param userId
     * @param cid
     * @param title
     * @param content
     * @param body
     * @param cidType
     * @param ddId
     */
    public static void userPushHandleLocal(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType, String ddId) {
        Locale local = LocaleUtils.getUserLocale(userId);
        LocaleContextHolder.setLocale(local);
        title = MessageUtils.message(title);
        content = MessageUtils.message(content);
        if (!StringUtils.isEmpty(ddId)) {
            content = content + ",NO:" + ddId;
        }
        userPush(push, pushEventService, userId, cid, title, content, body, cidType);
    }

}
