import { tassign } from 'tassign';
import { IItemTypes} from '../models/item.types';
import { IListItem } from '../models/list.item';
import { IUser } from '../models/user';
import { IPackage,IPackageMap,IPackages} from '../models/package';
import * as appCommonActions from '../actions/app-common.actions';
import { createSelector, createFeatureSelector, ActionReducerMap } from '@ngrx/store';

import { MODULE_NAME } from '../module-name';
import { IItem } from '../models/item';
import {getValidPackages, isValidPackage} from '../services/package.service';

export interface State {
  openedModalName: string,
  initialized: boolean,
  rootItems: IListItem[],
  itemTypes: IItemTypes,
  user:IUser,
  fullScreen: boolean,
  routeLoading:boolean,
  menuVisible: boolean,
  userPackages: IPackages,
  packages: IPackageMap,
  userSettingsRoot: IItem,
  accountMenuVisible: boolean,
  appMenuVisible: boolean,
  notificationMenuVisible: boolean,
  helpMenuVisible: boolean,
  settingMenuVisible: boolean,
  unreadNotifications: number,
  isOnline: boolean,
  isPageMode:boolean,
  settingMenuBackgroundColor:string
}

export const initialState: State = {
  openedModalName: null,
  initialized: false,
  rootItems: [],
  itemTypes: {},
  user:null,
  fullScreen: true,
  routeLoading: false,
  menuVisible: false,
  userPackages: {},
  packages: {},
  userSettingsRoot: null,
  accountMenuVisible: false,
  appMenuVisible: false,
  notificationMenuVisible: false,
  helpMenuVisible: false,
  settingMenuVisible: false,
  unreadNotifications: 0,
  isOnline: true,
  isPageMode: true,
  settingMenuBackgroundColor:'rgb(128, 128, 128)'
}

export function reducer(state = initialState, action: appCommonActions.Actions ): State {
  switch (action.type) {
    case appCommonActions.INITUSER: {
      return tassign(state,{initialized: true});
    }
    case appCommonActions.INITUSERSUCCESS: {
      const a = action as appCommonActions.InitUserSuccess;
      const claims = { ...a.userinfo.info };
      const user:IUser = {
        code:a.user.code,
        email:claims["email"]!== undefined ? claims["email"] : a.user.name,
        name:claims["name"]!== undefined?claims["name"] : a.user.email,
        lastName:a.user.lastName,
        firstName:a.user.firstName,
        claims:claims,
        searchable: false
      };
      return tassign(state, { user: user });
    }
    case appCommonActions.INITROOTSUCCESS: {
      const a = action as appCommonActions.InitRootSuccess;
      return tassign(state, { rootItems:a.items});
    }
    case appCommonActions.OPENMODAL: {
      return tassign(state, { openedModalName: action.modalName });
    }
    case appCommonActions.CLOSEMODAL: {
      return tassign(state, { openedModalName: null });
    }
    case appCommonActions.LOADITEMTYPESSUCCESS: {
      const a = action as appCommonActions.LoadItemTypesSuccess;
      return tassign(state, { itemTypes: a.itemTypes });
    }
    case appCommonActions.FULLSCREEN: {
      return tassign(state, {
        fullScreen:true
      });
    }
    case appCommonActions.SHOWNAVBAR: {
      return tassign(state, {
        fullScreen: false
      });
    }
    case appCommonActions.STARTROUTELOADING: {
      return tassign(state, {
        routeLoading: true
      });
    }
    case appCommonActions.ENDROUTELOADING: {
      return tassign(state, {
        routeLoading: false
      });
    }
    case appCommonActions.TOGGLEMENU: {
      return tassign(state, { menuVisible: !state.menuVisible,accountMenuVisible:!state.menuVisible?false:state.accountMenuVisible,appMenuVisible:!state.menuVisible?false:state.appMenuVisible,notificationMenuVisible:!state.menuVisible?false:state.notificationMenuVisible,helpMenuVisible:!state.menuVisible?false:state.helpMenuVisible,settingMenuVisible:!state.menuVisible?false:state.settingMenuVisible });
    }
    case appCommonActions.TOGGLEACCOUNTMENU: {
      return tassign(state, { accountMenuVisible: !state.accountMenuVisible,appMenuVisible:false,notificationMenuVisible:false,helpMenuVisible:false,settingMenuVisible:false });
    }
    case appCommonActions.TOGGLEAPPMENU: {
      return tassign(state, { appMenuVisible: !state.appMenuVisible,accountMenuVisible:false,notificationMenuVisible:false,helpMenuVisible:false,settingMenuVisible:false });
    }
    case appCommonActions.TOGGLENOTIFICATIONMENU: {
      return tassign(state, { notificationMenuVisible : !state.notificationMenuVisible,accountMenuVisible:false,appMenuVisible:false,helpMenuVisible:false,settingMenuVisible:false });
    }
    case appCommonActions.TOGGLEHELPMENU: {
      return tassign(state, { helpMenuVisible : !state.helpMenuVisible,accountMenuVisible:false,appMenuVisible:false,notificationMenuVisible:false,settingMenuVisible:false });
    }
    case appCommonActions.TOGGLESETTINGMENU: {
      return tassign(state, { settingMenuVisible : !state.settingMenuVisible,accountMenuVisible:false,appMenuVisible:false,notificationMenuVisible:false,helpMenuVisible:false });
    }
    case appCommonActions.ESCAPE: {
      return tassign(state, { menuVisible: false,accountMenuVisible:false,appMenuVisible: false,notificationMenuVisible:false,helpMenuVisible:false,settingMenuVisible:false });
    }
    case appCommonActions.SETMENUVISIBLE: {
      const a = action as appCommonActions.SetMenuVisible;
      return tassign(state, { menuVisible: a.visible,accountMenuVisible:a.visible?false:state.accountMenuVisible,appMenuVisible:a.visible?false:state.appMenuVisible,notificationMenuVisible:a.visible?false:state.notificationMenuVisible,helpMenuVisible:a.visible?false:state.helpMenuVisible,settingMenuVisible:a.visible?false:state.settingMenuVisible });
    }
    case appCommonActions.INITUSERPACKAGESSUCCESS:{
      const a = action as appCommonActions.InitUserPackagesSuccess;
      const packages = {}
      a.items.forEach((item) => {
        item.data.dataDate = item.dataDate;
        item.data.dataEndDate = item.dataEndDate;
        if (!packages[item.data.id]) {
          packages[item.data.id] = [];
        }
        packages[item.data.id].push(item.data);
      });

      return tassign(state,{userPackages:packages});
    }
    case appCommonActions.INITPACKAGESSUCCESS:{
      const a = action as appCommonActions.InitPackagesSuccess;
      const packages = {}
      a.items.forEach((item) => {
        packages[item.data.id] = item.data;
      });

      return tassign(state,{packages:packages});
    }
    case appCommonActions.INITUSERSETTINGSROOTSUCCESS:{
      const a = action as appCommonActions.InitUserSettingsRootSuccess;
      return tassign(state, { userSettingsRoot : a.item });
    }
    case appCommonActions.LOGOUT:{
      return tassign(state,{user:null,initialized:false});
    }
    case appCommonActions.CLOSEALL: {
      return tassign(state,{accountMenuVisible:false,appMenuVisible:false, menuVisible:false });
    }
    case appCommonActions.ONLINE:{
      return tassign(state,{isOnline:true});
    }
    case appCommonActions.OFFLINE:{
      return tassign(state,{isOnline:false});
    }
    case appCommonActions.SETPAGEMODE: {
      const a = action as appCommonActions.SetPageMode;
      return tassign(state,{isPageMode:a.pageMode});
    }
    case appCommonActions.NOTIFICATIONEVENT: {
      const a = action as appCommonActions.NotificationEvent;
      let unread = 0;
      if(a.attributes["unread"]) {
        unread = parseInt(a.attributes["unread"]);
      }
      return tassign(state,{unreadNotifications:unread});
    }
    case appCommonActions.SETUNREADNOTIFICATIONS: {
      const a = action as appCommonActions.SetUnreadNotifications;
      return tassign(state,{unreadNotifications:a.unread});
    }
    case appCommonActions.SETSETTINGMENUBACKGROUNDCOLOR: {
      const a = action as appCommonActions.SetSettingMenuBackgroundColor;
      return tassign(state,{settingMenuBackgroundColor:a.color});
    }    
    default: {
      return state;
    }
  }
}

export const getOpenedModalName = (state: State) => state.openedModalName;
export const getInitialized = (state: State) => state.initialized;
export const getItemTypes = (state: State) => state.itemTypes;
export const getRootItems = (state: State) => state.rootItems;
export const getFullScreen = (state: State) => state.fullScreen;
export const getRouteLoading = (state: State) => state.routeLoading;
export const getMenuVisible = (state: State) => state.menuVisible;
export const getUser = (state: State) => state.user;
export const getUserPackages = (state: State) => state.userPackages;
export const getPackages = (state: State) => state.packages;
export const getUserSettingsRoot = (state: State) => state.userSettingsRoot;
export const getAccountMenuVisible = (state: State) => state.accountMenuVisible;
export const getAppMenuVisible = (state: State) => state.appMenuVisible;
export const getNotificationMenuVisible = (state: State) => state.notificationMenuVisible;
export const getHelpMenuVisible = (state: State) => state.helpMenuVisible;
export const getSettingMenuVisible = (state: State) => state.settingMenuVisible;
export const getUnreadNotifications = (state: State) => state.unreadNotifications;
export const getIsOnline = (state: State) => state.isOnline;
export const getIsPageMode = (state: State) => state.isPageMode;
export const getSettingMenuBackgroundColor = (state: State) => state.settingMenuBackgroundColor;

export const selectAppCommonState = createFeatureSelector<State>(MODULE_NAME);

export const selectOpenedModalName = createSelector(selectAppCommonState, getOpenedModalName);
export const selectGetInitialized = createSelector(selectAppCommonState, getInitialized);
export const selectGetItemTypes = createSelector(selectAppCommonState, getItemTypes);
export const selectGetRootItems = createSelector(selectAppCommonState, getRootItems);
export const selectGetFullScreen = createSelector(selectAppCommonState, getFullScreen);
export const selectGetRouteLoading = createSelector(selectAppCommonState, getRouteLoading);
export const SelectGetMenuVisible = createSelector(selectAppCommonState,getMenuVisible);
export const SelectGetUser = createSelector(selectAppCommonState,getUser);
export const SelectGetUserPackages = createSelector(selectAppCommonState,getUserPackages);
export const SelectGetPackages = createSelector(selectAppCommonState,getPackages);
export const SelectGetValidUserPackages = createSelector(SelectGetUserPackages, (packageMap) => {
  return getValidPackages(packageMap);
});
export const SelectGetUserSettingsRoot = createSelector(selectAppCommonState,getUserSettingsRoot);
export const SelectGetAccountMenuVisible = createSelector(selectAppCommonState,getAccountMenuVisible);
export const SelectGetAppMenuVisible = createSelector(selectAppCommonState,getAppMenuVisible);
export const SelectGetNotificationMenuVisible = createSelector(selectAppCommonState,getNotificationMenuVisible);
export const SelectGetHelpMenuVisible = createSelector(selectAppCommonState,getHelpMenuVisible);
export const SelectGetSettingMenuVisible = createSelector(selectAppCommonState,getSettingMenuVisible);
export const SelectgetUnreadNotifications = createSelector(selectAppCommonState,getUnreadNotifications);

export const SelectGetIsOnline = createSelector(selectAppCommonState,getIsOnline);
export const SelectGetIsPageMode = createSelector(selectAppCommonState,getIsPageMode);
export const SelectGetSettingMenuBackgroundColor= createSelector(selectAppCommonState,getSettingMenuBackgroundColor);
