import { defineStore } from 'pinia'

export interface IState {
  peerId: string | undefined
  isBusy: boolean
  isClose: boolean
  callId: string | undefined
}

export const usePeerStore = defineStore({
  id: 'peer_store',
  state(): IState {
    return {
      /**
       * 自己的peerid
       */
      peerId: undefined,
      /**
       * 是否已经在通话
       */
      isBusy: false,

      /**
       * 是否收到关闭通话的消息
       */
      isClose: false,

      /**
       * 被呼叫的id
       */
      callId: undefined
    }
  },
  getters: {
    getIsClose(): boolean {
      return this.isClose
    }
  },
  actions: {
    setPeerId(peerId: string) {
      this.peerId = peerId
    },
    setCallId(callId: string|undefined) {
      this.callId = callId
    },
    updateBusyStatus(isBusy: boolean) {
      this.isBusy = isBusy
    },
    updateCloseStatus(isClose: boolean) {
      if (isClose) {
        this.isBusy = false
      }
      this.isClose = isClose
    },
    closeAll() {
      this.updateBusyStatus(false)
    }
  }
})
