import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { LevelInitializer, levels } from "../../game/levels";

interface LevelState {
  idx: number;
  name: string;
  voxels: Array<any>;
  playerPos: any;
  codeviews: Array<any>;
  allowedCodeBlocks: Array<string>;
  activeCodeview: string;
  introMessages: Array<any> | null;
}

const initialState: LevelState = {
  ...levels[0],
  idx: 0,
};

const levelSlice = createSlice({
  name: 'level',
  initialState,
  reducers: {
    setLevel: (state, action: PayloadAction<{ level: LevelInitializer; idx: number }>) => {
      const { level, idx } = action.payload;
      return { ...level, idx };
    },
    nextLevel: (state) => {
      if (levels.length <= state.idx + 1) return state;
      const nextLevel = levels[state.idx + 1];
      return { ...nextLevel, idx: state.idx + 1 };
    },
  },
});

export const { setLevel, nextLevel } = levelSlice.actions;
export default levelSlice.reducer;