package com.wzj.soopin.member.controller;

import com.wzj.soopin.member.domain.vo.IMCallbackVO;
import com.wzj.soopin.member.service.IMCallbackService;
import org.apache.commons.codec.digest.DigestUtils;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/callback/api")
public class ImCallbackController {

    private static final Logger log = LoggerFactory.getLogger(ImCallbackController.class);
    @Autowired
    private IMCallbackService imCallbackService;
    private static final String CALLBACK_TOKEN = "your_auth_token"; //

    @PostMapping
    public R handleCallback(
        @RequestParam(value = "Sign", required = false) String sign,
        @RequestParam(value = "RequestTime", required = false) String requestTime,
        @RequestParam(value = "CallbackCommand", required = false) String callbackCommand,
        @RequestBody Map<String, Object> requestBody) {

        // 1. 签名验证
        if (sign != null && !verifySignature(CALLBACK_TOKEN, sign, requestTime)) {
            return R.fail(1001, "签名校验失败");
        }

        // 2. 快速响应
        IMCallbackVO callbackVO = new IMCallbackVO();

        // 3. 分发业务逻辑
        if (StringUtils.isBlank(callbackCommand)) {
            return R.fail(1002, "缺少CallbackCommand");
        }
        switch (callbackCommand) {
            case "Sns.CallbackFriendAdd"://好友添加
                // 关注，直接调用 FansController 的 follow
                callbackVO = imCallbackService.handleImFollow(requestBody);
                break;
            case "Sns.CallbackFriendDelete"://好友删除
                // 取关，直接调用 FansController 的 cancel
                imCallbackService.handleImCancelFollow(requestBody);
                break;
            case "Profile.CallbackPortraitSet":
                // 资料修改
                imCallbackService.handleProfileCallbackPortraitSet(requestBody);
                break;
            case "Sns.CallbackBlackListAdd":
                imCallbackService.handleImAddBlock(requestBody);
                break;
            case "Sns.CallbackBlackListDelete":
                imCallbackService.handleImCancelBlock(requestBody);
                break;
            case "Follow.CallbackAfterFollowAdd"://关注
                imCallbackService.handleImFollow(requestBody);
                break;
            case "Follow.CallbackAfterFollowDelete"://取关
                imCallbackService.handleImCancelFollow(requestBody);
                break;
            default:
                log.info("收到未知事件类型: {}, 参数: {}", callbackCommand, requestBody);
                break;
        }
        return R.ok(callbackVO);
    }

    // 签名校验方法
    private boolean verifySignature(String token, String sign, String requestTime) {
        if (sign == null || requestTime == null) return false;
        long now = System.currentTimeMillis() / 1000;
        if (Math.abs(now - Long.parseLong(requestTime)) > 60) return false;
        String localSign = DigestUtils.sha256Hex(token + requestTime);
        return localSign.equals(sign);
    }


}
