import { Action, combineReducers, createReducer, on } from '@ngrx/store';
import { State, initialState } from './state';
import {
  loginSuccess,
  increaseLoginFailCount,
  initialLoginFailCount,
  logout,
  logoutInitialize,
  updateLoginRes,
  infoUserSuccess
} from './actions';
import { UserInfoUpdateType } from '@ucap-webmessenger/protocol-info';

export const reducer = createReducer(
  initialState,
  on(loginSuccess, (state, action) => {
    return {
      ...state,
      loginRes: action.loginRes
    };
  }),

  on(updateLoginRes, (state, action) => {
    return {
      ...state,
      loginRes: action.loginRes
    };
  }),

  on(increaseLoginFailCount, (state, action) => {
    return {
      ...state,
      loginFailCount: state.loginFailCount + 1
    };
  }),

  on(initialLoginFailCount, (state, action) => {
    return {
      ...state,
      loginFailCount: 0
    };
  }),

  on(infoUserSuccess, (state, action) => {
    let loginRes = {
      ...state.loginRes
    };

    switch (action.res.type) {
      case UserInfoUpdateType.Image:
        loginRes = {
          ...loginRes
        };
        break;
      case UserInfoUpdateType.Intro:
        loginRes = {
          ...loginRes
        };
        break;
      case UserInfoUpdateType.TelephoneVisible:
        loginRes = {
          ...loginRes
        };
        break;
      case UserInfoUpdateType.StatusMessage1:
        loginRes = {
          ...loginRes,
          statusMessage1: action.res.info
        };
        break;
      case UserInfoUpdateType.StatusMessage2:
        loginRes = {
          ...loginRes,
          statusMessage2: action.res.info
        };
        break;
      case UserInfoUpdateType.StatusMessage3:
        loginRes = {
          ...loginRes,
          statusMessage3: action.res.info
        };
        break;
      default:
        break;
    }

    return {
      ...state,
      loginRes: {
        ...loginRes
      }
    };
  }),

  on(logoutInitialize, (state, action) => {
    return {
      ...initialState
    };
  })
);
