import {
  type MembershipLevel,
  MembershipLevelEnum,
} from "@/constants/membershipLevels"

import { getSteppedUpLevel } from "@/utils/user"

import {
  type LevelWithRewards,
  OverviewTableActionsEnum,
  type OverviewTableClientProps,
  type OverviewTableReducerAction,
} from "@/types/components/overviewTable"

export function getLevel(
  membershipLevel: MembershipLevel,
  levels: LevelWithRewards[]
) {
  return levels.find((level) => level.level_id === membershipLevel)!
}

export function getInitialState({
  activeMembership,
  levels,
}: OverviewTableClientProps) {
  if (!activeMembership) {
    return {
      selectedLevelAMobile: getLevel(MembershipLevelEnum.L1, levels),
      selectedLevelBMobile: getLevel(MembershipLevelEnum.L2, levels),
      selectedLevelADesktop: getLevel(MembershipLevelEnum.L1, levels),
      selectedLevelBDesktop: getLevel(MembershipLevelEnum.L2, levels),
      selectedLevelCDesktop: getLevel(MembershipLevelEnum.L3, levels),
    }
  }
  const level = MembershipLevelEnum[activeMembership]

  switch (level) {
    case MembershipLevelEnum.L6:
      return {
        selectedLevelAMobile: getLevel(MembershipLevelEnum.L6, levels),
        selectedLevelBMobile: getLevel(MembershipLevelEnum.L7, levels),
        selectedLevelADesktop: getLevel(MembershipLevelEnum.L5, levels),
        selectedLevelBDesktop: getLevel(MembershipLevelEnum.L6, levels),
        selectedLevelCDesktop: getLevel(MembershipLevelEnum.L7, levels),
      }
    case MembershipLevelEnum.L7:
      return {
        selectedLevelAMobile: getLevel(MembershipLevelEnum.L6, levels),
        selectedLevelBMobile: getLevel(MembershipLevelEnum.L7, levels),
        selectedLevelADesktop: getLevel(MembershipLevelEnum.L6, levels),
        selectedLevelBDesktop: getLevel(MembershipLevelEnum.L7, levels),
        selectedLevelCDesktop: getLevel(MembershipLevelEnum.L1, levels),
      }
    default:
      return {
        selectedLevelAMobile: getLevel(level, levels),
        selectedLevelBMobile: getLevel(getSteppedUpLevel(level, 1), levels),
        selectedLevelADesktop: getLevel(level, levels),
        selectedLevelBDesktop: getLevel(getSteppedUpLevel(level, 1), levels),
        selectedLevelCDesktop: getLevel(getSteppedUpLevel(level, 2), levels),
      }
  }
}

export function reducer(state: any, action: OverviewTableReducerAction) {
  switch (action.type) {
    case OverviewTableActionsEnum.SET_SELECTED_LEVEL_A_MOBILE:
      return {
        ...state,
        selectedLevelAMobile: action.payload,
      }
    case OverviewTableActionsEnum.SET_SELECTED_LEVEL_B_MOBILE:
      return {
        ...state,
        selectedLevelBMobile: action.payload,
      }
    case OverviewTableActionsEnum.SET_SELECTED_LEVEL_A_DESKTOP:
      return {
        ...state,
        selectedLevelADesktop: action.payload,
      }
    case OverviewTableActionsEnum.SET_SELECTED_LEVEL_B_DESKTOP:
      return {
        ...state,
        selectedLevelBDesktop: action.payload,
      }
    case OverviewTableActionsEnum.SET_SELECTED_LEVEL_C_DESKTOP:
      return {
        ...state,
        selectedLevelCDesktop: action.payload,
      }
    default:
      return state
  }
}
