import {
  bluebookBaseConst,
  getUserCenterParams,
  usercenterbase,
} from '@/xiaolanbenlib/api/index'
import request from '@/xiaolanbenlib/module/axios.js'
import { AxiosResponse } from 'taro-axios'

export interface loginWithoutAuthorizeProps {
  autoPwd: number // "密码是否由系统自动生成(0不是，1是)",
  avatarUrl: string // "头像地址",
  birthday: string // "用户生日",
  cookieValue: string // "cookieValue",
  from: string // "登录方式",
  loginTime: string // "登录时间",
  mobile: string // "手机号",
  nickName: string // "用户昵称",
  outId: string // "第三方账号登录的openId",
  redirectTo: string // "重定向地址",
  regTime: string // "注册时间",
  register: boolean // "标示当前登陆操作是否自动注册了新账户",
  sex: number // "性别(0保密，1男，2女)",
  token: string // "用户token",
  tokenExpiredTime: number // "Token有效时长(秒)",
  userId: number // "兼容老的用户id",
  userName: string // "用户名"
}
// 小程序小蓝本注册不授权用户信息
export const loginWithoutAuthorize = async (
  payload,
): Promise<AxiosResponse<loginWithoutAuthorizeProps>> => {

  return request.post(
    `${usercenterbase}usercenter-gateway/api/v2/web/mini/sns/weixin/login/withoutAuthorize`,
    payload,
    {
      params: {
        ...getUserCenterParams(),
      },
    },
  )
}

interface WechatUserInfo {
  autoPwd: number //"密码是否由系统自动生成(0不是，1是)",
  avatarUrl: string //"头像地址",
  birthday: string //"用户生日",
  cookieValue: string //"cookieValue",
  from: string //"登录方式",
  loginTime: string //"登录时间",
  mobile: string //"手机号",
  nickName: string //"用户昵称",
  openId: string //"微信账号登录的openId",
  outId: string //"第三方账号登录的openId",
  redirectTo: string //"重定向地址",
  regTime: string //"注册时间",
  register: boolean //"标示当前登陆操作是否自动注册了新账户",
  sex: string //"性别(0保密，1男，2女)",
  token: string //"用户token",
  tokenExpiredTime: number //"Token有效时长(秒)",
  unionid: string //"微信账号登录的unionId",
  userId: number //"兼容老的用户id",
  userName: string //"用户名"
}

/**
 * 登陆(不自动注册，创新项目用,不获取微信信息)
 * @param code  临时登录凭证
 * @returns
 *
 * 新用户返回结果
 *  {
      "userId": 0,                               // 0
      "sex": 0,                                  // 0
      "regTime": "1970-01-01 08:00:00",          // "1970-01-01 08:00:00"
      "loginTime": "1970-01-01 08:00:00",        // "1970-01-01 08:00:00"
      "tokenExpiredTime": 0,                     // 0
      "birthday": "",                            // ""
      "openId": "ooBvs5WHdcui5pRX514heLimtOE0",  // 真的openId
      "unionid": "oXs7tsnhAv795dGyNOheVwPcerBg"  // 真的unionid
    }
 */
export const getWechatUserInfoByCode = async (
  code: string,
): Promise<AxiosResponse<WechatUserInfo>> => {
  return request.post(
    `${usercenterbase}usercenter-gateway/api/v2/web/mini/sns/weixin/login/checkRegister`,
    {
      code,
    },
    {
      params: getUserCenterParams(),
    },
  )
}

export interface UserInfoResponse {
  userId: number
  encodeUserId: string
  name: string
  nickName: string
  company: string
  logo: string
  eid: string
  capStrengthStr: string
  capStrengthStrV2: string
  userType: number
  pid: string
  wechatId: string
  mobile: string
  position: string
  betaUser: BetaUser
  alpha: number
  alphaDeadlineStr: string
  alphaStatus: number
  alphaExpireDate: string
  alphaType: number
  trialDone: number
  autoRenewStatus: number
  buttonText: string
  smartCard: boolean
  newMsgCount: string
  alphaExpireStatus: number
  contactImported: boolean
}

export interface BetaUser {
  title: string
  detailColor: string
  destUrl: string
  destUrlV2: string
}

export const getMyInfo = async (): Promise< AxiosResponse<UserInfoResponse>> => {
  return request.get(`${bluebookBaseConst}api/v2/user/myInfo`, {
    omitError: true,
  })
}
