import { Api } from './Api';
import { AxiosRequest } from './AxiosRequest';
import { HttpResult } from './HttpResult';
import { deviceInfo } from '@kit.BasicServicesKit';
import { DeviceUtil, FileUtil } from '@pura/harmony-utils';
import { LoginRequest } from './request/LoginRequest';
import { FormData } from '@ohos/axios';
import { fileIo } from '@kit.CoreFileKit';
import { JSON } from '@kit.ArkTS';

class ApiService {
  /**
   * 用户配置
   * @param oaid
   * @returns
   */
  userConfig(oaid: string): Promise<HttpResult> {
    const params: Record<string, string> = {
      'oaid': oaid,
      'os_version': deviceInfo.buildVersion.toString(),
      'imei': DeviceUtil.getDeviceId(),
      'cid': ''
    }
    return AxiosRequest.get<HttpResult>(Api.USER_CONFIG, params)
  }

  /**
   * 用户信息
   * @returns
   */
  userinfo(): Promise<HttpResult> {
    return AxiosRequest.get<HttpResult>(Api.USERINFO)
  }

  /**
   * 更新用户信息
   * @returns
   */
  updateUserinfo(params: Record<string, string>): Promise<HttpResult> {
    return AxiosRequest.put<HttpResult>(Api.USERINFO, params)
  }

  /**
   * 事件上报
   * @param key
   * @param value
   * @param extra
   * @returns
   */
  eventReport(key: string, value: string, extra: string): Promise<HttpResult> {
    const params: Record<string, string> = {
      'source': 'android',
      'type': 'click',
      'key': key,
      'value': value,
      'extra': extra
    }
    return AxiosRequest.post<HttpResult>(Api.EVENT_REPORT, params);
  }

  /**
   * 图片上传
   * @returns
   */
  uploadImage(base64: string, scene: string): Promise<HttpResult> {
    const data: Record<string, string> = {
      'file': base64
    }
    const params: Record<string, string> = {
      'type': 'base64',
      'scene': scene
    }
    return AxiosRequest.request({
      url: Api.UPLOAD_IMAGE,
      method: 'post',
      data: data,
      params: params // query参数
    })
  }

  feedback(type: string, content: string, contact: string, images: Array<string>): Promise<HttpResult> {
    const params: Record<string, Object> = {
      'type': type,
      'content': content,
      'contact': contact,
      'images': images
    }
    return AxiosRequest.post(Api.FEEDBACK, params)
  }

  /**
   * 发送验证码
   * @returns
   */
  sendCode(phone: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'phone': phone }
    return AxiosRequest.post<HttpResult>(Api.SEND_CODE, params)
  }

  /**
   * 手机号登录
   * @returns
   */
  loginByPhone(phone: string, code: string, timestamp: string, isBind: boolean = false): Promise<HttpResult> {
    const params: LoginRequest = {
      login_type: 'phone',
      phone: {
        'phone': phone,
        'code': code,
        'timestamp': timestamp
      },
      bind: isBind
    }
    return AxiosRequest.post<HttpResult>(Api.LOGIN, params)
  }

  /**
   * 微信登录
   * @returns
   */
  loginByWX(code: string, isBind: boolean = false): Promise<HttpResult> {
    const params: LoginRequest = {
      login_type: 'weixin',
      weixin: {
        'code': code,
        'code_type': ''
      },
      bind: isBind
    }
    return AxiosRequest.post<HttpResult>(Api.LOGIN, params)
  }

  /**
   * 微信登录
   * @returns
   */
  loginByCode(code: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'key': code }
    return AxiosRequest.put<HttpResult>(Api.QRCODE_LOGIN, params)
  }

  /**
   * 注销账户
   * @returns
   */
  userDestroy(): Promise<HttpResult> {
    return AxiosRequest.post<HttpResult>(Api.USER_DESTROY)
  }

  /**
   * 退出登陆
   * @returns
   */
  logout(): Promise<HttpResult> {
    return AxiosRequest.post<HttpResult>(Api.USER_LOGOUT)
  }

  /**
   * 解绑账号
   * @returns
   */
  unbind(loginType: string): Promise<HttpResult> {
    const params: LoginRequest = {
      login_type: loginType,
      unbind: true
    }
    return AxiosRequest.post<HttpResult>(Api.LOGIN, params);
  }

  /**
   * 账号列表
   * @returns
   */
  accountList(scene: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'scene': scene }
    return AxiosRequest.get<HttpResult>(Api.ACCOUNT_LIST, params);
  }

  /**
   * 切换账户
   * @param userId
   * @returns
   */
  changeAccount(userId: string): Promise<HttpResult> {
    const params: LoginRequest = {
      login_type: 'device',
      device: {
        'user_id': userId
      }
    }
    return AxiosRequest.post<HttpResult>(Api.LOGIN, params)
  }

  /**
   * 套餐列表
   * @returns
   */
  goodsList(type: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'type': type }
    return AxiosRequest.get<HttpResult>(Api.GOODS_LIST, params)
  }

  /**
   * 创建订单
   * @returns
   */
  createOrder(goodsId: string, payType: string, source: string, coupon: string): Promise<HttpResult> {
    const params: Record<string, string> = {
      'goods_id': goodsId,
      'pay_type': payType,
      'source': source,
      'pay_source': 'app',
      'coupon': coupon
    }
    return AxiosRequest.post<HttpResult>(Api.CREATE_ORDER, params)
  }

  /**
   * 查询订单
   * @param orderId
   * @returns
   */
  getOrderInfo(orderId: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'order_id': orderId}
    return AxiosRequest.get<HttpResult>(Api.CREATE_ORDER, params)
  }

  /**
   * 微信客服
   * @returns
   */
  wxService(): Promise<HttpResult> {
    return AxiosRequest.get<HttpResult>(Api.WX_SERVICE)
  }

  /**
   * M币信息
   */
  getDiamondInfo(): Promise<HttpResult> {
    return AxiosRequest.get(Api.USER_DIAMOND_INFO)
  }

  /**
   * 检查权限
   * @param scene wechat download
   * @returns
   */
  checkPermission(scene: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'scene': scene }
    return AxiosRequest.get<HttpResult>(Api.USER_AUTH, params)
  }

  /**
   * 上报权限
   * @param scene
   * @param count
   * @returns
   */
  sendCheckPermission(scene: string, count: number): Promise<HttpResult> {
    const params: Record<string, Object> = { 'scene': scene, 'count': count }
    return AxiosRequest.post<HttpResult>(Api.USER_AUTH, params)
  }

  /**
   * 首页顶部通知
   * @returns
   */
  noticeList(): Promise<HttpResult> {
    return AxiosRequest.get<HttpResult>(Api.NOTICE_LIST)
  }

  /**
   * 发送链接
   * @returns
   */
  getMaterialInfo(content: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'content': content }
    return AxiosRequest.post<HttpResult>(Api.MATERIAL_INFO, params)
  }

  /**
   * 获取提取状态信息
   * @returns
   */
  analysisMaterial(logId: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'id': logId }
    return AxiosRequest.get<HttpResult>(Api.MATERIAL_INFO, params)
  }

  /**
   * 获取视频号和直播回放
   * @returns
   */
  wxVideoList(): Promise<HttpResult> {
    return AxiosRequest.get<HttpResult>(Api.WX_VIDEO)
  }

  /**
   * 删除视频号和直播回放
   * @returns
   */
  deleteWxVideo(logId: string): Promise<HttpResult> {
    const params: Record<string, string> = { 'id': logId }
    return AxiosRequest.delete<HttpResult>(Api.DOWNLOAD_HISTORY_LIST, params)
  }

  /**
   * 上报下载状态
   */
  reportDownLoadStatus(logId: string, status: string, size: string, message: string): Promise<HttpResult> {
    const params: Record<string, string> = {
      'logid': logId,
      'status': status,
      'size': size,
      'message': message
    }
    return AxiosRequest.put<HttpResult>(Api.DOWNLOAD_HISTORY_LIST, params)
  }

  /**
   * 跳转至微信发送视频号给客服
   */
  wxVideoService(): Promise<HttpResult> {
    return AxiosRequest.get<HttpResult>(Api.WX_VIDEO_SERVICE)
  }

  /**
   * 跳转至微信发送视频号给客服
   */
  bindWxUserInfo(code: string): Promise<HttpResult> {
    return AxiosRequest.request({
      url: Api.BIND_WX_USER_INFO,
      method: 'post',
      params: { code: code } // query参数
    })
  }

  /**
   * 获取下载记录
   * @returns
   */
  getDownloadHistoryList(page: string, startTime: string, endTime: string): Promise<HttpResult> {
    const params: Record<string, string> = {
      'status': '2',
      'is_deleted': '0',
      'page': page,
      'size': '20',
      'start_time': startTime,
      'end_time': endTime
    }
    return AxiosRequest.get<HttpResult>(Api.DOWNLOAD_HISTORY_LIST, params)
  }

  /**
   * 删除下载记录
   * @returns
   */
  deleteDownloadHistory(startTime: string, endTime: string): Promise<HttpResult> {
    const params: Record<string, string> = {
      'status': '2',
      'is_deleted': '0',
      'page': '1',
      'size': '9999',
      'start_time': startTime,
      'end_time': endTime
    }
    return AxiosRequest.delete<HttpResult>(Api.DOWNLOAD_HISTORY_LIST, params)
  }

  /**
   * 获取素材列表
   * @returns
   */
  getMaterialList(page: string, cateId: string = '', keywords: string = ''): Promise<HttpResult> {
    const params: Record<string, string> = {
      'page': page,
      'size': '20',
      'cate_id': cateId,
      'keywords': keywords
    }
    return AxiosRequest.get<HttpResult>(Api.MATERIAL_LIST, params)
  }

  /**
   * 获取素材分类列表
   * @returns
   */
  getMaterialCateList(): Promise<HttpResult> {
    const params: Record<string, string> = {
      'page': '1',
      'size': '50'
    }
    return AxiosRequest.get<HttpResult>(Api.MATERIAL_CATE_LIST, params)
  }
}

export const apiService = new ApiService();