/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */
import {createSelector, createSlice, PayloadAction} from '@reduxjs/toolkit';
import {RootState} from '../index';

// Screen types
export enum StoreScreensType {
  ChannelList = 'channelList',
  ChannelDetail = 'channelDetail',
  Settings = 'settings',
  Login = 'login',
  Channels = "Channels"
}

// Screen state interface
export interface ScreenProps {
  [key: string]: unknown;
}

export interface ScreenState {
  currentScreen: StoreScreensType;
  screenProps: ScreenProps;
  previousScreen: StoreScreensType | null;
  navigationHistory: StoreScreensType[];
}

const initialState: ScreenState = {
  currentScreen: StoreScreensType.Login,
  screenProps: {},
  previousScreen: null,
  navigationHistory: []
};

// Selectors
export const screensSelector = (state: RootState): ScreenState => state.screens;

export const selectCurrentScreen = createSelector(
  [screensSelector],
  (screens: ScreenState) => screens.currentScreen
);

export const selectScreenProps = createSelector(
  [screensSelector],
  (screens: ScreenState) => screens.screenProps
);

export const selectPreviousScreen = createSelector(
  [screensSelector],
  (screens: ScreenState) => screens.previousScreen
);

export const selectNavigationHistory = createSelector(
  [screensSelector],
  (screens: ScreenState) => screens.navigationHistory
);

// Slice
const screensSlice = createSlice({
  name: 'screens',
  initialState,
  reducers: {
    setCurrentScreen: (state, action: PayloadAction<StoreScreensType>) => {
      state.previousScreen = state.currentScreen;
      state.currentScreen = action.payload;
      
      // Add to navigation history (keep last 10)
      state.navigationHistory.push(action.payload);
      if (state.navigationHistory.length > 10) {
        state.navigationHistory.shift();
      }
    },
    setScreenProps: (state, action: PayloadAction<ScreenProps>) => {
      state.screenProps = action.payload;
    },
    updateScreenProps: (state, action: PayloadAction<Partial<ScreenProps>>) => {
      state.screenProps = {
        ...state.screenProps,
        ...action.payload
      };
    },
    navigateToScreen: (state, action: PayloadAction<{screen: StoreScreensType; props?: ScreenProps}>) => {
      state.previousScreen = state.currentScreen;
      state.currentScreen = action.payload.screen;
      
      if (action.payload.props) {
        state.screenProps = action.payload.props;
      }
      
      // Add to navigation history
      state.navigationHistory.push(action.payload.screen);
      if (state.navigationHistory.length > 10) {
        state.navigationHistory.shift();
      }
    },
    navigateBack: (state) => {
      if (state.previousScreen) {
        const temp = state.currentScreen;
        state.currentScreen = state.previousScreen;
        state.previousScreen = temp;
      }
    },
    clearScreenProps: (state) => {
      state.screenProps = {};
    },
    resetNavigation: (state) => {
      state.currentScreen = StoreScreensType.Login;
      state.previousScreen = null;
      state.screenProps = {};
      state.navigationHistory = [];
    }
  }
});

export const {
  setCurrentScreen,
  setScreenProps,
  updateScreenProps,
  navigateToScreen,
  navigateBack,
  clearScreenProps,
  resetNavigation
} = screensSlice.actions;

export default screensSlice.reducer;