import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import * as styles from '../styles'
import { localStorageDarkModeKey, localStorageStyleKey } from '../config'
import { StyleKey } from '../interfaces'

interface StyleState {
  asideStyle: string
  asideScrollbarsStyle: string
  asideBrandStyle: string
  asideMenuItemStyle: string
  asideMenuItemActiveStyle: string
  asideMenuDropdownStyle: string
  navBarItemLabelStyle: string
  navBarItemLabelHoverStyle: string
  navBarItemLabelActiveColorStyle: string
  overlayStyle: string
  darkMode: boolean
  bgLayoutColor: string;
  iconsColor: string;
  activeLinkColor: string;
  cardsColor: string;
  focusRingColor: string;
  corners: string;
  cardsStyle: string;
  linkColor: string;
  websiteHeder: string;
  borders: string;
  shadow: string;
  websiteSectionStyle: string;
  textSecondary: string;
}


const initialState: StyleState = {
  asideStyle: styles.onyx.aside,
  asideScrollbarsStyle: styles.onyx.asideScrollbars,
  asideBrandStyle: styles.onyx.asideBrand,
  asideMenuItemStyle: styles.onyx.asideMenuItem,
  asideMenuItemActiveStyle: styles.onyx.asideMenuItemActive,
  activeLinkColor: styles.onyx.activeLinkColor,
  asideMenuDropdownStyle: styles.onyx.asideMenuDropdown,
  navBarItemLabelStyle: styles.onyx.navBarItemLabel,
  navBarItemLabelHoverStyle: styles.onyx.navBarItemLabelHover,
  navBarItemLabelActiveColorStyle: styles.onyx.navBarItemLabelActiveColor,
  overlayStyle: styles.onyx.overlay,
  darkMode: true, // Force dark mode for premium feel
  bgLayoutColor: styles.onyx.bgLayoutColor,
  iconsColor: styles.onyx.iconsColor,
  cardsColor: styles.onyx.cardsColor,
  focusRingColor: styles.onyx.focusRingColor,
  corners: styles.onyx.corners,
  cardsStyle: styles.onyx.cardsStyle,
  linkColor: styles.onyx.linkColor,
  websiteHeder: styles.onyx.websiteHeder,
  borders: styles.onyx.borders,
  shadow: styles.onyx.shadow,
  websiteSectionStyle: styles.onyx.websiteSectionStyle,
  textSecondary: styles.onyx.textSecondary,
};


export const styleSlice = createSlice({
  name: 'style',
  initialState,
  reducers: {
    setDarkMode: (state, action: PayloadAction<boolean | null>) => {
      state.darkMode = action.payload !== null ? action.payload : !state.darkMode

      if (typeof localStorage !== 'undefined') {
        localStorage.setItem(localStorageDarkModeKey, state.darkMode ? '1' : '0')
      }

      if (typeof document !== 'undefined') {
        document.body.classList[state.darkMode ? 'add' : 'remove']('dark-scrollbars')

        document.documentElement.classList[state.darkMode ? 'add' : 'remove'](
          'dark-scrollbars-compat'
        )
      }
    },

    setStyle: (state, action: PayloadAction<StyleKey>) => {
      if (!styles[action.payload]) {
        return
      }

      if (typeof localStorage !== 'undefined') {
        localStorage.setItem(localStorageStyleKey, action.payload)
      }

      const style = styles[action.payload]

      for (const key in style) {
        state[`${key}Style`] = style[key]
      }
    },
  },
})

// Action creators are generated for each case reducer function
export const { setDarkMode, setStyle } = styleSlice.actions

export default styleSlice.reducer