import {
  USER_LOGGED_IN,
  USER_LOGGED_OUT,
  REQUEST_LOGIN_FAILURE,
  REQUEST_LOGIN,
  REQUEST_LOGOUT,
  FETCH_CURRENT_USER_REQUEST,
  FETCH_CURRENT_USER_SUCCESS,
  FETCH_CURRENT_USER_FAILURE,
  REQUEST_REGISTER,
  REQUEST_REGISTER_SUCCESS,
  REQUEST_REGISTER_FAILURE,
  SUBSCRIBE_NEWSLETTER_REQUEST,
  SUBSCRIBE_NEWSLETTER_SUCCESS,
  SUBSCRIBE_NEWSLETTER_FAILURE,
  SEND_CONFIRMATION_EMAIL_REQUEST,
  SEND_CONFIRMATION_EMAIL_SUCCESS,
  SEND_CONFIRMATION_EMAIL_FAILURE,
  ACCOUNT_CONFIRMATION_REQUEST,
  ACCOUNT_CONFIRMATION_SUCCESS,
  ACCOUNT_CONFIRMATION_FAILURE,
  VERIFY_USER_REQUEST,
  RESET_PASSWORD_REQUEST,
  RESET_PASSWORD_SUCCESS,
  RESET_PASSWORD_FAILURE,
  SUBSCRIBE_USER_TO_PUSH_REQUEST,
  SUBSCRIBE_USER_TO_PUSH_SUCCESS,
  SUBSCRIBE_USER_TO_PUSH_FAILURE,
  UPDATE_USER_PICTURE_REQUEST,
  UPDATE_USER_PICTURE_SUCCESS,
  UPDATE_USER_PICTURE_FAILURE,
  FETCH_USER_SUBSCRIPTION_TO_PUSH_REQUEST,
  FETCH_USER_SUBSCRIPTION_TO_PUSH_SUCCESS,
  FETCH_USER_SUBSCRIPTION_TO_PUSH_FAILURE

} from "../types";

export function user(state = {
}, action = {}) {
  switch (action.type) {
    case REQUEST_LOGIN:
      return {
        loading: true,
        fetched: false,
      }
    case USER_LOGGED_IN:
      return {
        ...action.user,
        loading: false,
        fetched: false,
      }
    case REQUEST_LOGOUT:
      return {
        loading: true,
        fetched: false,
      };
    case USER_LOGGED_OUT:
      return {
        loading: false,
        fetched: false,
      };
    case REQUEST_LOGIN_FAILURE:
      return {
        loading: false,
        errors: action.errors,
      }
    case VERIFY_USER_REQUEST:
    case FETCH_CURRENT_USER_REQUEST:
      return {
        ...state,
        loading: true,
        fetched: false,
      }

    case FETCH_CURRENT_USER_SUCCESS:
      return {
        loading: false,
        fetched: true,
        ...action.user,
      }
    case FETCH_CURRENT_USER_FAILURE:
      return {
        loading: false,
        fetched: false,
      }

    case UPDATE_USER_PICTURE_REQUEST:
      return {
        ...state,
        pictureLoading: true,
      }
    case UPDATE_USER_PICTURE_SUCCESS:
      return {
        ...state,
        pictureLoading: false,
        picture: action.data.file
      }
    case UPDATE_USER_PICTURE_FAILURE:
      return {
        ...state,
        pictureLoading: false,
      }

    case REQUEST_REGISTER:
      return {
        loading: true,
      }

    case REQUEST_REGISTER_SUCCESS:
      return {
        loading: false,
      }

    case REQUEST_REGISTER_FAILURE:
      return {
        loading: false,
        errors: action.err,
      }

    case SUBSCRIBE_NEWSLETTER_REQUEST:
      return {
        ...state,
        loading: true,
      }

    case SUBSCRIBE_NEWSLETTER_SUCCESS:
      return {
        ...state,
        loading: false,
        NewsLetterRegistrationSuccess: true
      }

    case SUBSCRIBE_NEWSLETTER_FAILURE:
      return {
        ...state,
        loading: false,
        errors: action.err,
        NewsLetterRegistrationSuccess: false
      }

    case SEND_CONFIRMATION_EMAIL_REQUEST:
    case SEND_CONFIRMATION_EMAIL_SUCCESS:
    case SEND_CONFIRMATION_EMAIL_FAILURE:
      return {
      }

    case ACCOUNT_CONFIRMATION_REQUEST:
      return {
        loading: true,
      }
    case ACCOUNT_CONFIRMATION_SUCCESS:
      return {
        loading: false,
      }
    case ACCOUNT_CONFIRMATION_FAILURE:
      return {
        loading: false,
        errors: action.err,
      }
    case RESET_PASSWORD_REQUEST:
      return {
        loading: true,
      }
    case RESET_PASSWORD_SUCCESS:
      return {
        loading: false,
        passwordReset: true
      }
    case RESET_PASSWORD_FAILURE:
      return {
        loading: false,
        errors: action.err,
        passwordReset: false
      }
    case SUBSCRIBE_USER_TO_PUSH_REQUEST:
      return {
        ...state,
        loading: true,
      }
    case SUBSCRIBE_USER_TO_PUSH_SUCCESS:
      return {
        ...state,
        loading: false,
        isUserSubscribedToPush: true
      }
    case SUBSCRIBE_USER_TO_PUSH_FAILURE:
      return {
        ...state,
        loading: false,
        isUserSubscribedToPush: false,
        errors: action.err,
      }
    case FETCH_USER_SUBSCRIPTION_TO_PUSH_REQUEST:
      return {
        ...state,
        loading: true,
        isUserSubscribedToPush: false,
      }
    case FETCH_USER_SUBSCRIPTION_TO_PUSH_SUCCESS:
      return {
        ...state,
        loading: false,
        isUserSubscribedToPush: action.res.isUserSubscribed,
      }
    case FETCH_USER_SUBSCRIPTION_TO_PUSH_FAILURE:
      return {
        ...state,
        loading: false,
        errors: action.err,
      }
    
    default:
      return state;
  }
}
