import type { SoundAction, SoundState } from "../../types/types";

export const initialState: SoundState = {
  sightingSound: "switch",
  NPEDsound: "popup",
  hotlistSound: "warning",
  hotlists: [{ name: "hotlistName", sound: "notification" }],
  soundOptions: [
    { name: "Switch (Default)", soundFileName: "switch" },
    { name: "Popup", soundFileName: "popup" },
    { name: "Notification", soundFileName: "notification" },
    { name: "Beep", soundFileName: "beep" },
    { name: "Ding", soundFileName: "ding" },
    { name: "Shutter", soundFileName: "shutter" },
    { name: "Warning (voice)", soundFileName: "warning" },
    { name: "Attention (voice)", soundFileName: "attention" },
  ],
  sightingVolume: 1,
  NPEDsoundVolume: 1,
  hotlistSoundVolume: 1,
  uploadedSound: null,
};

export function reducer(state: SoundState, action: SoundAction): SoundState {
  switch (action.type) {
    case "UPDATE": {
      return {
        ...state,
        sightingSound: action.payload.sightingSound,
        NPEDsound: action.payload.NPEDsound,
        hotlistSound: action.payload.hotlistSound,
        hotlists: action.payload.hotlists?.map((hotlist) => ({
          name: hotlist.name,
          sound: hotlist.sound,
        })),
        NPEDsoundVolume: action.payload.NPEDsoundVolume,
        sightingVolume: action.payload.sightingVolume,
        hotlistSoundVolume: action.payload.hotlistSoundVolume,
        soundOptions: action.payload.soundOptions,
      };
    }

    case "ADD": {
      return {
        ...state,
        soundOptions: [...(state.soundOptions ?? []), action.payload],
      };
    }
    // todo: refactor to use single state coupled with sound name. e.g : {name: <soundname>, volume: <volume>}
    case "SIGHTINGVOLUME":
      return {
        ...state,
        sightingVolume: action.payload,
      };

    case "NPEDVOLUME":
      return {
        ...state,
        NPEDsoundVolume: action.payload,
      };

    case "HOTLISTVOLUME":
      return {
        ...state,
        hotlistSoundVolume: action.payload,
      };
    case "UPLOADEDSOUND":
      return {
        ...state,
        uploadedSound: action.payload,
      };
    default:
      return state;
  }
}
