package com.fs.wxcid.service.impl;

import com.fs.wxcid.dto.common.ApiResponse;
import com.fs.wxcid.dto.login.LoginRequest;
import com.fs.wxcid.dto.login.QrCodeRequest;
import com.fs.wxcid.dto.login.SlideVerifyRequest;
import com.fs.wxcid.dto.login.VerifyCodeRequest;
import com.fs.wxcid.service.LoginService;
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 LoginServiceImpl implements LoginService {

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

    // ------------------ 账号密码登录 ------------------

    public ApiResponse a16Login(String authKey, LoginRequest request) {
        return post("/login/A16Login", authKey, request);
    }

    public ApiResponse deviceLogin(String authKey, LoginRequest request) {
        return post("/login/DeviceLogin", authKey, request);
    }

    // ------------------ 二维码登录 ------------------

    public ApiResponse getLoginQrCodeNew(String authKey, QrCodeRequest request) {
        return post("/login/GetLoginQrCodeNew", authKey, request);
    }

    public ApiResponse getLoginQrCodeNewDirect(String authKey, QrCodeRequest request) {
        return post("/login/GetLoginQrCodeNewDirect", authKey, request);
    }

    public ApiResponse macLogin(String authKey, QrCodeRequest request) {
        return post("/login/MacLogin", authKey, request);
    }

    public ApiResponse carLogin(String authKey, QrCodeRequest request) {
        return post("/login/CarLogin", authKey, request);
    }

    public ApiResponse wakeUpLogin(String authKey, QrCodeRequest request) {
        return post("/login/WakeUpLogin", authKey, request);
    }

    // ------------------ 状态检测 ------------------

    public ApiResponse checkCanSetAlias(String authKey) {
        return get("/login/CheckCanSetAlias", authKey);
    }

    public ApiResponse checkLoginStatus(String authKey) {
        return get("/login/CheckLoginStatus", authKey);
    }

    public ApiResponse getLoginStatus(String authKey) {
        return get("/login/GetLoginStatus", authKey);
    }

    public ApiResponse get62Data(String authKey) {
        return get("/login/Get62Data", authKey);
    }

    public ApiResponse logOut(String authKey) {
        return get("/login/LogOut", authKey);
    }

    // ------------------ 验证 ------------------

    public ApiResponse verifyCode(String authKey, VerifyCodeRequest request) {
        return post("/login/VerifyCode", authKey, request);
    }

    public ApiResponse verifyCodeSlide(String authKey, SlideVerifyRequest request) {
        return post("/login/VerifyCodeSlide", 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);
    }
}