/*
 Rookeries client - redux application store

 @copyright (c) Copyright 2013-2017 Dorian Pula
 @license AGPL v3
 @author Dorian Pula [dorian.pula@amber-penguin-software.ca]
 */

import {combineReducers} from 'redux';

import * as actions from './actions';
import {adminStateReducer} from './admin/reducer';


function pageStateReducer(state = 'Loading...', action) {
  switch (action.type) {
  case actions.LOAD_PAGE_ACTION:
    return action.content;
  default:
    return state;
  }
}

function menuStateReducer(state = [], action) {
  switch (action.type) {
  case actions.LOAD_MENU_ACTION:
    return action.menu;
  default:
    return state;
  }
}


const DEFAULT_SITE_INFO  = {
  name: 'Rookeries',
  logo: '/static/images/mr-penguin-amber.svg',
  tagline: 'Simple Site Scaffolding',
  footer: 'Rookeries - © 2013-2016 - Amber Penguin Software'
};

function siteInfoStateReducer(state = {...DEFAULT_SITE_INFO}, action) {
  switch (action.type) {
  case actions.UPDATE_SITE_INFO_ACTION:
    return action.site;
  default:
    return state;
  }
}

export const DEFAULT_USER_LOGIN = {
  fullName: 'Stranger',
  token: ''
};

function userLoginStateReducer(state = {...DEFAULT_USER_LOGIN}, action) {
  switch (action.type) {
  case actions.LOAD_INITIAL_USER_ACTION:
    return action.user;
  case actions.UPDATE_USER_ACTION:
    return action.user;
  case actions.LOGOUT_USER_ACTION:
    return action.user;
  default:
    return state;
  }
}

function themeStateReducer(state = {}, action) {
  switch (action.type) {
  case actions.SWITCH_THEME_ACTION:
    return {theme: action.theme, alternativeTheme: action.alternativeTheme};
  case actions.LOAD_THEME_ACTION:
    return {theme: action.theme, alternativeTheme: action.alternativeTheme};
  default:
    return state;
  }
}

const DEFAULT_EDITOR_STATE = {
  state: 0,
  theme: 'default'
};

function editorStateReducer(state = {...DEFAULT_EDITOR_STATE}, action) {
  switch (action.type) {
  case actions.SWITCH_THEME_ACTION:
    return {...state, theme: action.editorTheme};
  case actions.LOAD_THEME_ACTION:
    return {...state, theme: action.editorTheme};
  default:
    return state;
  }
}

const reducer = combineReducers({
  admin: adminStateReducer,
  page: pageStateReducer,
  editor: editorStateReducer,
  menu: menuStateReducer,
  site: siteInfoStateReducer,
  user: userLoginStateReducer,
  theme: themeStateReducer,
});

export {reducer};
