import { PhonePasswordInfo } from "@/enums/verification.ts";
import {realApi, threeRealApi} from "@/utils/request.ts";
import axios from "axios";

export class Address {
  province = "";
  city = "";
  country = "";

  constructor(obj?: Partial<Address>) {
    console.log("obj=", obj);
    if (obj) {
      this.province = obj.province || "";
      this.city = obj.city || "";
      this.country = obj.country || "";
    }
  }

  toString() {
    console.log(this);
    return `{"province":"${this.province}","city":"${this.city}", "country":"${this.country}"}`;
  }
}

export class UserInfo {
  readonly id: number = 0;
  readonly name: string = "";
  readonly avatar: string = "";
  readonly gender: number = 0;
  readonly phone: string = "";
  readonly address!: Address;
}

/**
 * 用户账户登录
 * @param data 登录请求数据
 * @returns 登录请求的 Promise
 */
export const login = (data: any): Promise<string> => {
  data.clientId = 'e5cd7e4891bf95d1d19206ce24a7b32e';
  data.grantType = 'password';
  try {
    return axios.post('http://localhost:8080/auth/login',data).then(res=>{
      return res.data
    });
    // return instanceReal.post('/auth/login',data);
    // return realApi.post({
    //   url: "/auth/login",
    //   data,
    // });
  } catch (error) {
    throw error;
  }
};

/**
 * 手机验证码登录
 * @param data 手机验证码登录请求数据
 * @returns 手机验证码登录请求的 Promise
 */
export const phoneLogin = (data: object): Promise<string> => {
  try {
    return realApi.post<string>({
      url: "/user/phoneLogin",
      data,
    });
  } catch (error) {
    throw error;
  }
};

/**
 * 发送验证码
 * @param data 发送验证码请求数据
 * @returns 发送验证码请求的 Promise
 */
export const sendCode = (data: object) => {
  try {
    return realApi.post({
      url: "/user/authCode",
      data,
    });
  } catch (error) {
    throw error;
  }
};

/**
 * 获取用户信息
 * @returns 获取用户信息请求的 Promise
 */
export const getUserInfo = (): Promise<UserInfo> => {
  try {
    return realApi.get<UserInfo>({
      url: "/user/info",
    });
  } catch (error) {
    throw error;
  }
};

/**
 * 注册用户
 * @param data 发送验证码请求数据
 * @returns 获取用户信息请求的 Promise
 */
export const register = (data: PhonePasswordInfo) => {
  try {
    return realApi.post({
      url: "/user/register",
      data,
    });
  } catch (error) {
    throw error;
  }
};

/**
 * 重置密码
 * @param data 发送验证码请求数据
 * @returns 获取用户信息请求的 Promise
 */
export const resetPassword = (data: PhonePasswordInfo) => {
  try {
    return realApi.put({
      url: "/user/forgetPassword",
      data,
    });
  } catch (error) {
    throw error;
  }
};

/**
 * 提交用户信息
 * @param data 用户信息的数据
 * @returns 提交用户信息请求的 Promise
 */

export const submitUserInfo = (data: object) => {
  try {
    return realApi.put({
      url: "/user/info",
      data,
    });
  } catch (error) {
    throw error;
  }
};

/**
 * 更改用户手机号码
 * @param data 用户手机号码的数据
 * @returns 更改用户手机号码请求的 Promise
 */

export const submitPhoneInfo = (data: object) => {
  try {
    return realApi.put({
      url: "/user/phone",
      data,
    });
  } catch (error) {
    throw error;
  }
};

/**
 * 修改用户密码
 * @param data 用户密码的数据
 * @returns 更改用户密码请求的 Promise
 */

export const sumbitChangePassword = (data: object) => {
  try {
    return realApi.put({
      url: "/user/resetPassword",
      data,
    });
  } catch (error) {
    throw error;
  }
};

/**
 * @Description: 获取sdk Token
 * @author：翁庚荣
 * @date 2024/4/29 15:27
 * @param {string} appId
 * @param {string} appSecret
 */
interface AccessTokenInfo {
  accessToken: string;
  expiration: string;
}
export const getAccessToken = (
  appId: string,
  appSecret: string
): Promise<AccessTokenInfo> => {
  try {
    return threeRealApi.get<AccessTokenInfo>({
      url: "/api/getAccessToken",
      params: {
        appId,
        appSecret,
      },
    });
  } catch (error) {
    throw error;
  }
};

/**
 * @Description: 微信绑定验证
 * @author：汪明
 * @date 2024/5/6 19:11
 * @param {string} code
 */
export class WechatTokenWrapper {
  token?: string;
  openId = "";
  type = "";
  message = "";
}

export const wechatLogin = async (
  code?: string
): Promise<WechatTokenWrapper> => {
  try {
    const scope = "snsapi_login";
    return realApi.get<WechatTokenWrapper>({
      url: "/oauth/wechat",
      params: {
        code,
        scope,
      },
    });
  } catch (error) {
    throw error;
  }
};

/**
 * @Description: 手机号绑定微信
 * @author：汪明
 * @date 2024/5/6 20:19
 * @param {object} data
 */

export const bindPhone = (data: object) => {
  return realApi.post({
    url: "/oauth/bindPhone",
    data,
  });
};
