import { Location, User, UserBody } from '../models';

enum ActionKey {
  USER_LOCATION_UPDATE_REQUEST = 'USER/USER_LOCATION_UPDATE_REQUEST',
  USER_LOCATION_UPDATE_SUCCESS = 'USER/USER_LOCATION_UPDATE_SUCCESS',
  USER_PROFILE_REQUEST = 'USER/USER_PROFILE_REQUEST',
  USER_PROFILE_SUCCESS = 'USER/USER_PROFILE_SUCCESS',
  USER_PROFILE_FAIL = 'USER/USER_PROFILE_FAIL',
  USER_PROFILE_EDIT_REQUEST = 'USER/USER_PROFILE_EDIT_REQUEST',
  USER_PROFILE_EDIT_SUCCESS = 'USER/USER_PROFILE_EDIT_SUCCESS',
  USER_PROFILE_EDIT_FAIL = 'USER/USER_PROFILE_EDIT_FAIL',
  USER_PHOTO_UPLOAD_REQUEST = 'USER/USER_PHOTO_UPLOAD_REQUEST',
  USER_PHOTO_UPLOAD_SUCCESS = 'USER/USER_PHOTO_UPLOAD_SUCCESS',
  USER_PHOTO_UPLOAD_FAIL = 'USER/USER_PHOTO_UPLOAD_FAIL',
}

export type UserProfileRequest = {
  type: ActionKey.USER_PROFILE_REQUEST;
  user: User,
};
export function userProfileRequest(user: User): UserProfileRequest {
  return {
    type: ActionKey.USER_PROFILE_REQUEST,
    user,
  };
}

export type UserProfileSuccess = {
  type: ActionKey.USER_PROFILE_SUCCESS;
  user: UserBody,
};
export function userProfileSuccess(user: UserBody): UserProfileSuccess {
  return {
    type: ActionKey.USER_PROFILE_SUCCESS,
    user,
  };
}

export type UserProfileFail = {
  type: ActionKey.USER_PROFILE_SUCCESS;
  error: string,
};
export function userProfileFail(error: string): UserProfileFail {
  return {
    type: ActionKey.USER_PROFILE_SUCCESS,
    error,
  };
}

export type UserProfileEditRequest = {
  type: ActionKey.USER_PROFILE_EDIT_REQUEST;
  data: UserBody;
  userId: string;
};
export function userProfileEditRequest(userId: string, data: UserBody): UserProfileEditRequest {
  return {
    type: ActionKey.USER_PROFILE_EDIT_REQUEST,
    data,
    userId,
  };
}

export type UserLocationUpdateRequest = {
  type: ActionKey.USER_LOCATION_UPDATE_REQUEST;
  location: Location;
  userId: string;
};
export function UserLocationUpdateRequest(userId: string, location: Location): UserLocationUpdateRequest {
  return {
    type: ActionKey.USER_LOCATION_UPDATE_REQUEST,
    location,
    userId,
  };
}
export type UserLocationUpdateSuccess = {
  type: ActionKey.USER_LOCATION_UPDATE_SUCCESS;
  userId: string;
};
export function UserLocationUpdateSuccess(userId: string): UserLocationUpdateSuccess {
  return {
    type: ActionKey.USER_LOCATION_UPDATE_SUCCESS,
    userId,
  };
}

export type UserProfileEditSuccess = {
  type: ActionKey.USER_PROFILE_EDIT_SUCCESS;
  user: UserBody,
};
export function userProfileEditSuccess(user: UserBody): UserProfileEditSuccess {
  return {
    type: ActionKey.USER_PROFILE_EDIT_SUCCESS,
    user,
  };
}

export type UserProfileEditFail = {
  type: ActionKey.USER_PROFILE_EDIT_FAIL;
  error: string;
};
export function userProfileEditFail(error: string): UserProfileEditFail {
  return {
    type: ActionKey.USER_PROFILE_EDIT_FAIL,
    error,
  };
}
export type UserPhotoUploadRequested = {
  type: ActionKey.USER_PHOTO_UPLOAD_REQUEST;
  path: string;
  userId: string;
};
export function userPhotoUploadRequested(userId: string, path: string): UserPhotoUploadRequested {
  return {
    type: ActionKey.USER_PHOTO_UPLOAD_REQUEST,
    path,
    userId,
  };
}

export type UserPhotoUploadSuccess = {
  type: ActionKey.USER_PHOTO_UPLOAD_SUCCESS;
  downloadUrl: string,
};
export function userPhotoUploadSuccess(downloadUrl: string): UserPhotoUploadSuccess {
  return {
    type: ActionKey.USER_PHOTO_UPLOAD_SUCCESS,
    downloadUrl,
  };
}

export type UserPhotoUploadFail = {
  type: ActionKey.USER_PHOTO_UPLOAD_FAIL;
  error: string;
};
export function userPhotoUploadFail(error: string): UserPhotoUploadFail {
  return {
    type: ActionKey.USER_PHOTO_UPLOAD_FAIL,
    error,
  };
}

type ActionType = UserProfileRequest
  | UserProfileSuccess
  | UserProfileFail
  | UserProfileEditRequest
  | UserProfileEditSuccess
  | UserProfileEditFail
  | UserPhotoUploadRequested
  | UserPhotoUploadSuccess
  | UserLocationUpdateRequest
  | UserLocationUpdateSuccess
  | UserPhotoUploadFail;

export { ActionKey, ActionType };
