import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import type { Message, UserInfo } from '@/types/global'

export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++
  }

  return { count, doubleCount, increment }
})

export const useUserInfoStore = defineStore('userInfo', () => {
  const userInfo = ref({} as UserInfo)

  const setUserInfo = (newUserInfo: UserInfo) => {
    userInfo.value = newUserInfo
  }

  const getUserInfo = () => {
    return userInfo.value || {}
  }

  return { userInfo, setUserInfo, getUserInfo }
})

export const useMessageStore = defineStore('message', () => {
  const messageRecords = ref({} as Record<number,Message[]>)
  const messageIds = ref({} as Record<number, Set<number>>)
  const setMessageRecord = (userId: number, record: Message) => {
    if (!messageRecords.value[userId]) {
      messageRecords.value[userId] = []
      messageIds.value[userId] = new Set()
    }
    if (messageIds.value[userId].has(record.id as number)) {
      // Replace the existing message
      const index = messageRecords.value[userId].findIndex(msg => msg.id === record.id)
      messageRecords.value[userId][index] = record
    } else {
      // Add the new message
      messageRecords.value[userId].push(record)
      messageIds.value[userId].add(record.id as number)
    }
  }
  const unshiftMessageRecord = (userId: number, record: Message) => {
    if (!messageRecords.value[userId]) {
      messageRecords.value[userId] = []
      messageIds.value[userId] = new Set()
    }
    if (messageIds.value[userId].has(record.id as number)) {
      // Replace the existing message
      const index = messageRecords.value[userId].findIndex(msg => msg.id === record.id)
      messageRecords.value[userId][index] = record
    } else {
      // Add the new message
      messageRecords.value[userId].unshift(record)
      messageIds.value[userId].add(record.id as number)
    }
  }

  const getMessageRecord = (userId: any, pageNo:number = 1, pageSize:number = 30) => {
    // 分页查询list
    // 获取所有消息
    const allMessages = messageRecords.value[userId] || [];
    // 计算开始和结束的索引
    const startIndex = (pageNo - 1) * pageSize;
    const endIndex = pageNo * pageSize;
    console.log('userId,',userId,'全部消息数量为',allMessages.length,'startIndex：', startIndex,'endIndex',endIndex,'pageNo', pageNo)
    // 返回分页后的消息
    return allMessages.slice(startIndex, endIndex);
  }
  const getMessageRecordTotal = (userId: number) => {
    const allMessages = messageRecords.value[userId] || [];
    return allMessages.length
  }
  const removeMessageRecord = (userId: number,record: Message) => {
    messageRecords.value[userId] = messageRecords.value[userId].filter(item => item.id !== record.id)
  }
  const clearMessageRecord = (userId: number) => {
    messageRecords.value[userId] = []
  }
  return { messageRecords, setMessageRecord,unshiftMessageRecord, getMessageRecord,getMessageRecordTotal, removeMessageRecord,clearMessageRecord }
})

export const useUserInfoListStore = defineStore('userInfoList', () => {
  const userInfoList = ref({} as Record<number, UserInfo>)
  const setUserInfoList = (userId: number, userInfo: UserInfo) => {
    userInfoList.value[userId] = userInfo
  }
  const getUserInfo = (userId: number) => {
    return userInfoList.value[userId] || {}
  }
  const resetUserInfoList = (newUserInfoList: Array<UserInfo>) => {
    userInfoList.value = []
    newUserInfoList.forEach((item) => {
      userInfoList.value[item.id as number] = item
    })
  }
  return { userInfoList, setUserInfoList, getUserInfo,resetUserInfoList }
})


// 保存语言
export const useMessageLoadStatus = defineStore('messageLoadStatus', () => {
  const status = ref({} as Record<number, boolean>)
  const setLoadStatus = (userId:number, newStatus: boolean) => {
    console.log('userid', userId, '消息已经全部从服务器加载完成')
    status.value[userId] = newStatus
  }
  const getStatus = (userId:number) => {
    if (!status.value[userId]) {
      status.value[userId] = false
    }
    return status.value[userId]
  }
  return { status, setLoadStatus, getStatus }
})

