import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { RootState } from '../../app/store'
import { ParticipantTranscriptModel, SessionTranscriptModel } from './models'
import { RemoveParticipantTranscriptionAction, TranscriptionReceivedAction } from './actionModels'

const initialState: SessionTranscriptModel = {
  transcriptionServiceIsEnabled: false,
  transcriptionServiceIsLoading: false,
  transcriptionInProgress: false,
  transcripts: {},
}

export const transcriptSlice = createSlice({
  name: 'transcript',
  initialState,
  reducers: {
    transcriptionReceived: (state, action: PayloadAction<TranscriptionReceivedAction>) => {
      const { participantId, transcriptionResult } = action.payload
      if (!state.transcripts[participantId]) {
        state.transcripts[participantId] = { finalTranscripts: [], partialTranscript: null }
      }
      const participantTranscripts = state.transcripts[participantId]
      if (transcriptionResult.isFinal) {
        participantTranscripts.finalTranscripts.push(transcriptionResult)
        participantTranscripts.partialTranscript = null
      } else {
        participantTranscripts.partialTranscript = transcriptionResult
      }
    },
    setTranscriptionServiceEnabled: (state, action: PayloadAction<boolean>) => {
      state.transcriptionServiceIsEnabled = action.payload
    },
    setTranscriptionServiceIsLoading: (state, action: PayloadAction<boolean>) => {
      state.transcriptionServiceIsLoading = action.payload
    },
    setTranscriptionInProgress: (state, action: PayloadAction<boolean>) => {
      const inProgress = action.payload
      state.transcriptionInProgress = inProgress
      if (!inProgress) {
        for (const transcript of Object.values(state.transcripts)) {
          transcript.partialTranscript = null
        }
      }
    },
    clearTranscription: (state) => {
      state.transcripts = {}
      state.transcriptionServiceIsEnabled = false
    },
    clearRemoteParticipantTranscription: (state, action: PayloadAction<RemoveParticipantTranscriptionAction>) => {
      const { participantId } = action.payload
      delete state.transcripts[participantId]
    },
  },
})

export const {
  clearRemoteParticipantTranscription,
  clearTranscription,
  setTranscriptionServiceEnabled,
  setTranscriptionServiceIsLoading,
  setTranscriptionInProgress,
  transcriptionReceived,
} = transcriptSlice.actions

export const selectTranscriptionServiceIsEnabled = (state: RootState) => state.transcript.transcriptionServiceIsEnabled
export const selectTranscriptionServiceIsLoading = (state: RootState) => state.transcript.transcriptionServiceIsLoading
export const selectTranscriptionInProgress = (state: RootState) => state.transcript.transcriptionInProgress
export const selectTranscripts = (state: RootState) => state.transcript.transcripts

export const selectParticipantTranscripts = (state: RootState, participantId: string): ParticipantTranscriptModel => {
  const { transcripts } = state.transcript
  if (!transcripts[participantId]) {
    return { finalTranscripts: [], partialTranscript: null }
  }
  return transcripts[participantId]
}

export default transcriptSlice.reducer
