import { showerFetch, ResponseDataEntity } from './index';

export enum OperationMode {
  beanFirst = 1,
  aimiFirst = 2,
  onlyAimi = 3,
}

type ShowerParams = {
  deviceCode: string;
  customerId: string;
  campusId: number;
};
export type DeviceInfoResponse = {
  code: string;
  isOnlyBluetooth: number;
  operationMode: OperationMode;
  position: string;
};
/**
 * 通过设备码获取设备信息
 */
export const fetchShowerInfo = (
  params: ShowerParams,
): Promise<ResponseDataEntity<DeviceInfoResponse>> =>
  showerFetch({
    url: `/dcxy/api/shower/devices/${params.deviceCode}`,
    data: params,
  });

type UsingParams = {
  customerId: number;
};
/**
 * 通过用户查询在使用的设备信息
 */
export const fetchUsingShowerInfo = (params: UsingParams) =>
  showerFetch({
    url: '/dcxy/api/shower/devices/inuseByCurrentUser',
    data: params,
  });

type StartParams = {
  deviceCode: string;
  customerId: string;
  customerName: string;
  customerPhone: string;
};
/**
 * 开启设备使用
 */
export const startShowerEquipment = (params: StartParams) =>
  showerFetch({
    url: `/dcxy/api/shower/devices/${params.deviceCode}/beginning`,
    method: 'POST',
    data: params,
  });

export enum ShowerUseType {
  bluetooth = 1,
  appointment = 2,
  mix = 3,
}
type ControllerParams = {
  customerId: number;
  campusId: number;
};
type Balance = {
  amount: number;
  // allowEmptyValue: false
  // 服务数量

  serviceId: string;
  // allowEmptyValue: false
  // 服务id -1：授信额度；0：艾米；1：豆

  serviceName: string;
  // allowEmptyValue: false
  // 服务名称
};
export type ControllerResponse = {
  appointmentThresholdPrompt: string;
  balances: Balance[];
  beanAmount: number;
  defaultType: ShowerUseType;
  money: number;
  thresholdPrompt: string;
  thresholdValue: number;
  useType: ShowerUseType;
};

export const getShowerController = (
  params: ControllerParams,
): Promise<ResponseDataEntity<ControllerResponse>> =>
  showerFetch({
    url: '/dcxy/api/shower/controllerConfigs',
    data: params,
  });

type AppointDeviceParam = {
  userId: number;
  campusId: number;
  location?: string;
  pageNum?: number;
  pageSize?: number;
};
export type AppointDeviceResponse = {
  appointable: boolean;
  // example: false
  // allowEmptyValue: false
  // 是否可预约

  code: string;
  // allowEmptyValue: false
  // 设备编号

  inPunishment: boolean;
  // example: false
  // allowEmptyValue: false
  // 用户是在惩罚中

  location: string;
  // allowEmptyValue: false
  // 设备位置

  operationMode: OperationMode;
  // allowEmptyValue: false
  // 设备计费模式
};
export const getAppointmentDevice = (
  params: AppointDeviceParam,
): Promise<ResponseDataEntity<{ list: AppointDeviceResponse[] }>> =>
  showerFetch({
    url: '/dcxy/api/shower/appointment/devices',
    data: params,
  });
type AppointingParams = {
  account: string;
  // allowEmptyValue: false
  // 设备登录账号

  deviceCode: string;
  // allowEmptyValue: false
  // 设备编号

  phone: string;
  // allowEmptyValue: false
  // 手机号

  userId: number;
  // allowEmptyValue: false
  // 用户ID

  userName: string;
  // allowEmptyValue: false
  // 用户名称
};
export const appointingEquipment = (
  params: AppointingParams,
): Promise<ResponseDataEntity<number>> =>
  showerFetch({
    url: '/dcxy/api/shower/appointment/appointing',
    method: 'POST',
    data: params,
  });
type AppointmentRecordsParams = {
  pageNum?: number;
  // 当前页码

  pageSize?: number;
  // 页面大小

  orderSort?: string;
  // 排序

  userId: number;
  // 用户ID

  appointTimeStart?: string;
  // 预约时间区间上限

  appointTimeEnd?: string;
  // 预约时间区间下限
};
export type AppointmentRecordsResponse = {
  appointTime: string;
  // allowEmptyValue: false
  // 预约时间

  campusName: string;
  // allowEmptyValue: false
  // 所属区域

  deviceCode: string;
  // allowEmptyValue: false
  // 设备编号

  deviceLocation: string;
  // allowEmptyValue: false
  // 设备位置

  expireTime: string;
  // allowEmptyValue: false
  // 到期时间

  status: number;
  // allowEmptyValue: false
  // 状态: 0-预约中，1-预约成功，2-使用中，3-完成，4-预约失败，5-失效(逾期未使用)

  used: boolean;
  // example: false
  // allowEmptyValue: false
  // 是否使用
};
export const getAppointmentRecords = (
  params: AppointmentRecordsParams,
): Promise<ResponseDataEntity<{ list: AppointmentRecordsResponse[] }>> =>
  showerFetch({
    url: '/dcxy/api/shower/appointment/appointRecords',
    data: params,
  });
type UserAppointableParams = {
  userId: number;
  campusId: number;
};
export type UserAppointableData = {
  appointMode: number;
  // allowEmptyValue: false
  // 校验结果：0-校区未启用预约功能，1-校区未设置惩罚时长，2-用户未在惩罚中，3-用户在惩罚中

  defaultType: number;
  // allowEmptyValue: false
  // 混合模式时的默认使用模式

  duration: number;
  // allowEmptyValue: false
  // 预约有效时长

  message: string;
  // allowEmptyValue: false
  // 被禁用时提示消息

  punishment: number;
  // allowEmptyValue: false
  // 系统惩罚时长

  useType: number;
  // allowEmptyValue: false
  // 使用模式: 1-蓝牙，2-预约，3-混合

  userPunishment: number;
  // allowEmptyValue: false
  // 用户剩余惩罚时长
};
export const getUserAppointableData = (
  params: UserAppointableParams,
): Promise<ResponseDataEntity<UserAppointableData>> =>
  showerFetch({
    url: `/dcxy/api/shower/appointment/user/${params.userId}/appointable/${
      params.campusId
    }`,
  });

type AppointResultParams = {
  recordId: number;
  userId: number;
};
export enum AppointResultRes {
  appointting = 0,
  appointSuccess = 1,
  appointFail = 4,
}
export const fetchAppointResult = (
  params: AppointResultParams,
): Promise<ResponseDataEntity<AppointResultRes>> =>
  showerFetch({
    url: `/dcxy/api/shower/appointment/appointResult/${params.recordId}`,
    data: params,
  });
