package com.fs.wxcid.service.impl;

import com.fs.wxcid.dto.common.ApiResponse;
import com.fs.wxcid.dto.message.GetMsgBigImgRequest;
import com.fs.wxcid.dto.message.*;
import com.fs.wxcid.service.MessageService;
import com.fs.wxwork.utils.WxWorkHttpUtil;
import com.alibaba.fastjson.TypeReference;
import org.springframework.stereotype.Service;

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

@Service
public class MessageServiceImpl implements MessageService {

    private static final String BASE_URL = "http://114.117.215.244:7006";


    @Override
    public ApiResponse addMessageMgr(String authKey, AddMessageMgrRequest request) {
        return post("/message/AddMessageMgr", authKey, request);
    }

    @Override
    public ApiResponse cdnUploadVideo(String authKey, CdnUploadVideoRequest request) {
        return post("/message/CdnUploadVideo", authKey, request);
    }

    @Override
    public ApiResponse downloadEmojiGif(String authKey, DownloadEmojiGifRequest request) {
        return post("/message/DownloadEmojiGif", authKey, request);
    }

    @Override
    public ApiResponse forwardEmoji(String authKey, ForwardEmojiRequest request) {
        return post("/message/ForwardEmoji", authKey, request);
    }

    @Override
    public ApiResponse forwardImageMessage(String authKey, ForwardImageMessageRequest request) {
        return post("/message/ForwardImageMessage", authKey, request);
    }

    @Override
    public ApiResponse forwardVideoMessage(String authKey, ForwardVideoMessageRequest request) {
        return post("/message/ForwardVideoMessage", authKey, request);
    }

    @Override
    public ApiResponse getMsgBigImg(String authKey, GetMsgBigImgRequest request) {
        return post("/message/GetMsgBigImg", authKey, request);
    }

    @Override
    public ApiResponse getMsgVideo(String authKey, GetMsgVideoRequest request) {
        return post("/message/GetMsgVideo", authKey, request);
    }

    @Override
    public ApiResponse getMsgVoice(String authKey, GetMsgVoiceRequest request) {
        return post("/message/GetMsgVoice", authKey, request);
    }

    @Override
    public ApiResponse groupMassMsgImage(String authKey, GroupMassMsgImageRequest request) {
        return post("/message/GroupMassMsgImage", authKey, request);
    }

    @Override
    public ApiResponse groupMassMsgText(String authKey, GroupMassMsgTextRequest request) {
        return post("/message/GroupMassMsgText", authKey, request);
    }

    @Override
    public ApiResponse httpSyncMsg(String authKey, HttpSyncMsgRequest request) {
        return post("/message/HttpSyncMsg", authKey, request);
    }

    @Override
    public ApiResponse newSyncHistoryMessage(String authKey) {
        return post("/message/NewSyncHistoryMessage", authKey, new Object()); // 无 body，传空对象或自定义
    }

    @Override
    public ApiResponse revokeMsg(String authKey, RevokeMsgRequest request) {
        return post("/message/RevokeMsg", authKey, request);
    }

    @Override
    public ApiResponse revokeMsgNew(String authKey, RevokeMsgNewRequest request) {
        return post("/message/RevokeMsgNew", authKey, request);
    }

    @Override
    public ApiResponse sendAppMessage(String authKey, SendAppMessageRequest request) {
        return post("/message/SendAppMessage", authKey, request);
    }

    @Override
    public ApiResponse sendEmojiMessage(String authKey, SendEmojiMessageRequest request) {
        return post("/message/SendEmojiMessage", authKey, request);
    }

    @Override
    public ApiResponse sendImageMessage(String authKey, SendImageMessageRequest request) {
        return post("/message/SendImageMessage", authKey, request);
    }

    @Override
    public ApiResponse sendImageNewMessage(String authKey, SendImageNewMessageRequest request) {
        return post("/message/SendImageNewMessage", authKey, request);
    }

    @Override
    public ApiResponse sendTextMessage(String authKey, SendTextMessageRequest request) {
        return post("/message/SendTextMessage", authKey, request);
    }

    @Override
    public ApiResponse sendVoice(String authKey, SendVoiceRequest request) {
        return post("/message/SendVoice", authKey, request);
    }

    @Override
    public ApiResponse shareCardMessage(String authKey, ShareCardMessageRequest request) {
        return post("/message/ShareCardMessage", authKey, request);
    }

    // ------------------ 工具方法 ------------------
    /**
     * 通用 POST 请求方法
     *
     * @param path   接口路径，如 "/friend/AgreeAdd"
     * @param authKey    账号唯一标识（query 参数）
     * @param request 请求体对象
     * @return 统一响应结果
     */
    private ApiResponse post(String path, String authKey, Object request) {
        String url = BASE_URL + path + "?key=" + authKey;
        return WxWorkHttpUtil.postWithType(url, request, new TypeReference<ApiResponse>() {});
    }

    /**
     * 通用 GET 请求方法（无请求体）
     *
     * @param path 接口路径
     * @param authKey  账号唯一标识
     * @return 统一响应结果
     */
    private ApiResponse get(String path, String authKey) {
        String url = BASE_URL + path;
        Map<String, Object> params = new HashMap<>();
        params.put("authKey", authKey);
        String resp = WxWorkHttpUtil.get(url, params);
        return com.alibaba.fastjson.JSON.parseObject(resp, ApiResponse.class);
    }
}