import { Reducer } from 'redux';

import * as Actions from './actions';
import { IPokemonExplorerState, PokemonExplorerActionTypes } from './types';

export const initialState : IPokemonExplorerState = {
    isLoading: false,
    leaguePokemon: null,
    maxPossibleStats: {
        baseStamina: 0,
        baseAttack: 0,
        baseDefense: 0,
        level: 0,
    },
    individualValues: {
        level: null,
        hp: null,
        atk: null,
        def: null,
    },
    league: 'great',
};

const reduceSetIsLoading = (
    state : IPokemonExplorerState,
    action : ReturnType<typeof Actions.setIsLoading>
) : IPokemonExplorerState => ({
    ...state,
    isLoading: action.payload.isLoading,
});

const reduceSetMaxPossibleStats = (
    state : IPokemonExplorerState,
    action : ReturnType<typeof Actions.setMaxPossibleStats>
) : IPokemonExplorerState => ({
    ...state,
    maxPossibleStats: {
        ...action.payload.maxStats,
    },
});

const reduceSetLeaguePokemon = (
    state : IPokemonExplorerState,
    action : ReturnType<typeof Actions.setLeaguePokemon>
) : IPokemonExplorerState => ({
    ...state,
    leaguePokemon: action.payload.leaguePokemon,
});

const reduceSetIvLevel = (
    state : IPokemonExplorerState,
    action : ReturnType<typeof Actions.setIvLevel>
) : IPokemonExplorerState => ({
    ...state,
    individualValues: {
        ...state.individualValues,
        level: action.payload.level,
    }
});

const reduceSetIvHp = (
    state : IPokemonExplorerState,
    action : ReturnType<typeof Actions.setIvHp>
) : IPokemonExplorerState => ({
    ...state,
    individualValues: {
        ...state.individualValues,
        hp: action.payload.hp,
    }
});

const reduceSetIvAtk = (
    state : IPokemonExplorerState,
    action : ReturnType<typeof Actions.setIvAtk>
) : IPokemonExplorerState => ({
    ...state,
    individualValues: {
        ...state.individualValues,
        atk: action.payload.atk,
    }
});

const reduceSetIvDef = (
    state : IPokemonExplorerState,
    action : ReturnType<typeof Actions.setIvDef>
) : IPokemonExplorerState => ({
    ...state,
    individualValues: {
        ...state.individualValues,
        def: action.payload.def,
    }
});

const reduceSetActiveLeague = (
    state : IPokemonExplorerState,
    action : ReturnType<typeof Actions.setActiveLeague>
) : IPokemonExplorerState => ({
    ...state,
    league: action.payload.league
});

export const PokemonExplorerReducers : Reducer<IPokemonExplorerState> = (
    state : IPokemonExplorerState = initialState,
    action,
) : IPokemonExplorerState => {
    switch (action.type) {
        case PokemonExplorerActionTypes.SET_IS_LOADING:
            return reduceSetIsLoading(state, action as ReturnType<typeof Actions.setIsLoading>);
        case PokemonExplorerActionTypes.SET_MAX_STATS:
            return reduceSetMaxPossibleStats(state, action as ReturnType<typeof Actions.setMaxPossibleStats>);
        case PokemonExplorerActionTypes.SET_LEAGUE_POKEMON:
            return reduceSetLeaguePokemon(state, action as ReturnType<typeof Actions.setLeaguePokemon>);
        case PokemonExplorerActionTypes.SET_IV_LEVEL:
            return reduceSetIvLevel(state, action as ReturnType<typeof Actions.setIvLevel>);
        case PokemonExplorerActionTypes.SET_IV_HP:
            return reduceSetIvHp(state, action as ReturnType<typeof Actions.setIvHp>);
        case PokemonExplorerActionTypes.SET_IV_ATK:
            return reduceSetIvAtk(state, action as ReturnType<typeof Actions.setIvAtk>);
        case PokemonExplorerActionTypes.SET_IV_DEF:
            return reduceSetIvDef(state, action as ReturnType<typeof Actions.setIvDef>);
        case PokemonExplorerActionTypes.SET_ACTIVE_LEAGUE:
            return reduceSetActiveLeague(state, action as ReturnType<typeof Actions.setActiveLeague>);
        default:
            return state;
    }
};
