import { UserBody, UserRole } from '../models';

enum ActionKey {
  INITIALIZE_USER_FLOW = 'AUTH/INITIALIZE_USER_FLOW',
  USER_UNAUTHORIZED = 'AUTH/USER_UNAUTHORIZED',
  USER_UID_RESOLVED = 'AUTH/USER_UID_RESOLVED',
  LOGIN_WITH_GOOGLE_REQUESTED = 'AUTH/LOGIN_WITH_GOOGLE_REQUESTED',
  LOGIN_WITH_FACEBOOK_REQUESTED = 'AUTH/LOGIN_WITH_FACEBOOK_REQUESTED',
  LOGIN_WITH_EMAIL_REQUESTED = 'AUTH/LOGIN_WITH_EMAIL_REQUESTED',
  SIGNUP_WITH_EMAIL_REQUESTED = 'AUTH/SIGNUP_WITH_EMAIL_REQUESTED',

  FIRST_LOGIN = 'AUTH/FIRST_LOGIN',
  APP_VERSION_SUPPORT_TERMINATED = 'AUTH/APP_VERSION_SUPPORT_TERMINATED',
  LOGIN_SUCCESS = 'AUTH/LOGIN_SUCCESS',
  LOGIN_FAIL = 'AUTH/LOGIN_FAIL',
  LOGOUT_REQUESTED = 'AUTH/LOGOUT_REQUESTED',
  LOGOUT_SUCCESS = 'AUTH/LOGOUT_SUCCESS',
  LOGOUT_FAIL = 'AUTH/LOGOUT_FAIL',
}

export type UserUnauthorized = {
  type: ActionKey.USER_UNAUTHORIZED,
};
export function UserUnauthorized(): UserUnauthorized {
  return {
    type: ActionKey.USER_UNAUTHORIZED,
  };
}
export type AppVersionSupportTerminated = {
  type: ActionKey.APP_VERSION_SUPPORT_TERMINATED
  message: string,
};
export function AppVersionSupportTerminated(message: string): AppVersionSupportTerminated {
  return {
    type: ActionKey.APP_VERSION_SUPPORT_TERMINATED,
    message,
  };
}

export type UserUidResolved = {
  type: ActionKey.USER_UID_RESOLVED
  userRole: UserRole
  uid: string,
};
export function userUidResolved(uid: string, userRole: UserRole): UserUidResolved {
  return {
    type: ActionKey.USER_UID_RESOLVED,
    userRole,
    uid,
  };
}

export type InitializeUserFlow = {
  type: ActionKey.INITIALIZE_USER_FLOW
  userRole: UserRole
  uid: string,
};
export function initializeUserFlow(uid: string, userRole: UserRole): InitializeUserFlow {
  return {
    type: ActionKey.INITIALIZE_USER_FLOW,
    userRole,
    uid,
  };
}

export type FirstLogin = {
  type: ActionKey.FIRST_LOGIN,
};
export function firstLogin(): FirstLogin {
  return {
    type: ActionKey.FIRST_LOGIN,
  };
}

export type SignupWithEmailRequested = {
  type: ActionKey.SIGNUP_WITH_EMAIL_REQUESTED
  userRole: UserRole
  name: string
  email: string
  password: string,
};

export function signupWithEmailRequested(
  name: string,
  email: string,
  password: string,
  userRole: UserRole,
): SignupWithEmailRequested {
  return {
    type: ActionKey.SIGNUP_WITH_EMAIL_REQUESTED,
    userRole,
    name,
    email,
    password,
  };
}

export type LoginWithGoogleRequested = {
  type: ActionKey.LOGIN_WITH_GOOGLE_REQUESTED
  userRole: UserRole,
};
export function loginWithGoogleRequested(userRole: UserRole): LoginWithGoogleRequested {
  return {
    type: ActionKey.LOGIN_WITH_GOOGLE_REQUESTED,
    userRole,
  };
}
export type LoginWithFacebookRequested = {
  type: ActionKey.LOGIN_WITH_FACEBOOK_REQUESTED
  userRole: UserRole,
};
export function loginWithFacebookRequested(userRole: UserRole): LoginWithFacebookRequested {
  return {
    type: ActionKey.LOGIN_WITH_FACEBOOK_REQUESTED,
    userRole,
  };
}

export type LoginWithEmailRequested = {
  type: ActionKey.LOGIN_WITH_EMAIL_REQUESTED
  userRole: UserRole
  email: string
  password: string,
};
export function loginWithEmailRequested(
  email: string,
  password: string,
  userRole: UserRole,
): LoginWithEmailRequested {
  return {
    type: ActionKey.LOGIN_WITH_EMAIL_REQUESTED,
    userRole,
    email,
    password,
  };
}

export type LoginSuccess = {
  type: ActionKey.LOGIN_SUCCESS
  user: UserBody
  userRole: UserRole,
};
export function loginSuccess(user: UserBody, userRole: UserRole): LoginSuccess {
  return {
    type: ActionKey.LOGIN_SUCCESS,
    user,
    userRole,
  };
}

export type LoginFail = {
  type: ActionKey.LOGIN_FAIL
  error: any
  userRole: UserRole,
};
export function loginFail(error: any, userRole: UserRole): LoginFail {
  return {
    type: ActionKey.LOGIN_FAIL,
    error,
    userRole,
  };
}

export type LogoutRequested = {
  type: ActionKey.LOGOUT_REQUESTED,
};
export function logoutRequested(): LogoutRequested {
  return {
    type: ActionKey.LOGOUT_REQUESTED,
  };
}

export type LogoutSuccess = {
  type: ActionKey.LOGOUT_SUCCESS,
};
export function logoutSuccess(): LogoutSuccess {
  return {
    type: ActionKey.LOGOUT_SUCCESS,
  };
}

export type LogoutFail = {
  type: ActionKey.LOGOUT_FAIL
  error: string,
};
export function logoutFail(error: string): LogoutFail {
  return {
    type: ActionKey.LOGOUT_FAIL,
    error,
  };
}

type ActionType =
  | LoginWithEmailRequested
  | SignupWithEmailRequested
  | UserUidResolved
  | LoginWithGoogleRequested
  | LoginWithFacebookRequested
  | AppVersionSupportTerminated
  | LoginSuccess
  | LoginFail
  | FirstLogin
  | InitializeUserFlow
  | LogoutRequested
  | LogoutSuccess
  | UserUnauthorized
  | LogoutFail;

export {ActionKey, ActionType};
