import FetchRequest from '@/api/FetchRequest'
import type Chat from '@/mode/Chat'
import type AjaxResult from '@/mode/AjaxResult'

class ChatApi {
  static url = '/api/sys/chat'

  /**
   * 获取当前用户的非置顶聊天列表
   */
  static list(): Promise<AjaxResult<Chat[]>> {
    return FetchRequest.get(`${this.url}/list`, true)
  }

  /**
   * 获取当前用户的非置顶聊天列表
   */
  static topList(): Promise<AjaxResult<Chat[]>> {
    return FetchRequest.get(`${this.url}/topList`, true)
  }

  /**
   * 新增聊天
   * @param chat chat
   */
  static add(chat: Chat): Promise<AjaxResult<boolean>> {
    return FetchRequest.post(this.url, JSON.stringify(chat), true)
  }

  /**
   * 更新聊天
   * @param chat chat
   */
  static update(chat: Chat): Promise<AjaxResult<boolean>> {
    const chatTemp = JSON.parse(JSON.stringify(chat))
    chatTemp.unreadCount = 0
    return FetchRequest.put(this.url, JSON.stringify(chatTemp), true)
  }


  /**
   * 批量更新聊天
   * @param chatList chatList
   */
  static batch(chatList: Array<Chat>): Promise<AjaxResult<boolean>> {
    const chatListTemp = JSON.parse(JSON.stringify(chatList))
    chatListTemp.forEach((chat:Chat) => {
      chat.unreadCount = 0
    })
    return FetchRequest.put(`${this.url}/batch`, JSON.stringify(chatListTemp), true)
  }


  /**
   * 移动聊天室位置
   * @param chatId chatId
   */
  static move(chatId: string): void {
     FetchRequest.get(`${this.url}/move?chatId=${chatId}`, true)
  }

  /**
   *  置顶聊天
   *  @param chatId chatId
   */
  static top(chatId: string): Promise<AjaxResult<boolean>> {
    return FetchRequest.get(`${this.url}/top?chatId=${chatId}`, true)
  }

  /**
   *  取消置顶聊天
   *  @param chatId 收藏id
   */
  static cancelTop(chatId: string): Promise<AjaxResult<boolean>> {
    return FetchRequest.get(`${this.url}/cancelTop?chatId=${chatId}`, true)
  }

  /**
   * 删除聊天
   * @param chatId 聊天id
   */
  static delete(chatId: string): Promise<AjaxResult<boolean>> {
    return FetchRequest.del(`${this.url}/${chatId}`, '', true)
  }
}

export default ChatApi
