import { ActionType, AuthActions, OfferActions, UserActions } from '../actions';
import { NetworkState } from '../models';

const INITIAL_STATE: NetworkState = {
  user: {
    auth: {
      error: null,
      loading: false,
    },
    update: {
      error: null,
      loading: false,
    },
  },
  offer: {
    new: {
      error: null,
      loading: false,
    },
  },
};

export function networkReducer(state = INITIAL_STATE, action: ActionType): NetworkState {
  switch (action.type) {
    case AuthActions.ActionKey.LOGIN_WITH_EMAIL_REQUESTED:
    case AuthActions.ActionKey.SIGNUP_WITH_EMAIL_REQUESTED:
    case AuthActions.ActionKey.LOGIN_WITH_GOOGLE_REQUESTED:
    case AuthActions.ActionKey.LOGIN_WITH_FACEBOOK_REQUESTED:
      return {
        ...state,
        user: {
          ...state.user,
          auth: {
            error: null,
            loading: true,
          },
        },
      };
    case AuthActions.ActionKey.LOGIN_FAIL:
      return {
        ...state,
        user: {
          ...state.user,
          auth: {
            error: action.error instanceof Error
              ? action.error.message
              : action.error,
            loading: false,
          },
        },
      };
    case AuthActions.ActionKey.LOGIN_SUCCESS:
      return {
        ...state,
        user: {
          ...state.user,
          auth: {
            error: null,
            loading: false,
          },
        },
      };
    case OfferActions.ActionKey.NEW_OFFER_REQUESTED:
    case OfferActions.ActionKey.UPDATE_OFFER_REQUESTED:
      return {
        ...state,
        offer: {
          ...state.offer,
          new: {
            error: null,
            loading: true,
          },
        },
      };
    case OfferActions.ActionKey.NEW_OFFER_FAIL:
    case OfferActions.ActionKey.UPDATE_OFFER_FAIL:
      return {
        ...state,
        offer: {
          ...state.offer,
          new: {
            error: action.error,
            loading: false,
          },
        },
      };
    case OfferActions.ActionKey.NEW_OFFER_SUCCESS:
    case OfferActions.ActionKey.UPDATE_OFFER_SUCCESS:
      return {
        ...state,
        offer: {
          ...state.offer,
          new: {
            error: null,
            loading: false,
          },
        },
      };
    case OfferActions.ActionKey.NEW_APPLICATION_REQUEST:
      return {
        ...state,
        offer: {
          ...state.offer,
          [action.offer.id]: {
            error: null,
            loading: true,
          },
        },
      };
    case OfferActions.ActionKey.NEW_APPLICATION_SUCCESS:
      return {
        ...state,
        offer: {
          ...state.offer,
          [action.offerId]: {
            error: null,
            loading: false,
          },
        },
      };
    case OfferActions.ActionKey.NEW_APPLICATION_FAIL:
      return {
        ...state,
        offer: {
          ...state.offer,
          [action.offerId]: {
            error: action.error,
            loading: false,
          },
        },
      };
    case UserActions.ActionKey.USER_PROFILE_EDIT_REQUEST:
      return {
        ...state,
        user: {
          ...state.user,
          update: {
            error: null,
            loading: true,
          },
        },
      };
    case UserActions.ActionKey.USER_PROFILE_EDIT_FAIL:
      return {
        ...state,
        user: {
          ...state.user,
          update: {
            error: action.error,
            loading: false,
          },
        },
      };
    case UserActions.ActionKey.USER_PROFILE_EDIT_SUCCESS:
      return {
        ...state,
        user: {
          ...state.user,
          update: {
            error: null,
            loading: false,
          },
        },
      };
    case AuthActions.ActionKey.LOGOUT_FAIL:
    case AuthActions.ActionKey.LOGOUT_SUCCESS:
      return INITIAL_STATE;
    default:
      return state;
  }
}
