# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/06_core.rq_collection.ipynb (unless otherwise specified).

__all__ = ['get_user_collection_folders', 'create_new_user_collection_folder', 'get_folder_metadata',
           'change_collection_folder_name', 'delete_collection_folder', 'get_collection_folder_by_release',
           'get_collection_items_by_folder', 'add_release_to_collection_folder',
           'move_release_to_another_collection_folder', 'change_rating_of_release_in_collection_folder',
           'delete_release_instance_from_collection_folder', 'list_custom_fields_for_collection_folders',
           'edit_custom_field_value_for_release_instance_from_collection_folder', 'get_collection_value']

# Cell
import requests
from typing import Union
from . import *

# Cell


def get_user_collection_folders(user: Union[UserWithoutAuthentication,
                                      UserWithUserTokenBasedAuthentication],
                                username: str
                                ) -> requests.models.Response:
    """
    Get a list of folders in a user’s collection.

    Note: If you are not authenticated as the collection owner,
          only folder ID 0 (the “All” folder) will be visible (if the requested user’s collection is public).

    No user Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders"
    headers = user.headers
    params = user.params

    return requests.get(url, headers=headers, params=params)

# Cell


def create_new_user_collection_folder(user: UserWithUserTokenBasedAuthentication,
                                      username: str,
                                      folder_name: str
                                      ) -> requests.models.Response:
    """
    Create a new folder in a user’s collection.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders"

    params = user.params

    data = {"name": folder_name}

    return requests.post(url, params=params, json=data)

# Cell


def get_folder_metadata(user: Union[UserWithoutAuthentication,
                                    UserWithUserTokenBasedAuthentication],
                        username: str,
                        folder_id: int
                        ) -> requests.models.Response:
    """
    Get metadata about a folder in a user’s collection.

    Note: If folder_id is not 0, authentication as the collection owner is required.

    No user Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{folder_id}"
    headers = user.headers
    params = user.params

    return requests.get(url, headers=headers, params=params)

# Cell


def change_collection_folder_name(user: UserWithUserTokenBasedAuthentication,
                                  username: str,
                                  folder_id: int,
                                  new_folder_name: str
                                  ) -> requests.models.Response:
    """
    Change the name of an existing collection folder in a user’s collection.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{folder_id}"
    params = user.params

    data = {"name": new_folder_name}

    return requests.post(url, params=params, json=data)

# Cell


def delete_collection_folder(user: UserWithUserTokenBasedAuthentication,
                             username: str,
                             folder_id: int
                             ) -> requests.models.Response:
    """
    Delete an existing collection folder from a user’s collection.

    Note: Only possible if folder is empty.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{folder_id}"
    headers = user.headers
    params = user.params

    return requests.delete(url, headers=headers, params=params)

# Cell


def get_collection_folder_by_release(user: UserWithUserTokenBasedAuthentication,
                                     username: str,
                                     release_id: int,
                                     page: Union[int, None] = None,
                                     per_page: Union[int, None] = None,
                                     ) -> requests.models.Response:
    """
    Get the user’s collection folders which contain a specified release.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/releases/{release_id}"
    headers = user.headers
    params = user.params

    if page:
        params["page"] = max(1, page)
    if per_page:
        params["per_page"] = max(1, per_page)

    return requests.get(url, headers=headers, params=params)

# Cell


def get_collection_items_by_folder(user: Union[UserWithoutAuthentication,
                                               UserWithUserTokenBasedAuthentication],
                                   username: str,
                                   folder_id: int,
                                   page: Union[int, None] = None,
                                   per_page: Union[int, None] = None,
                                   sort: Union[SortOptionsCollection, None] = None,
                                   sort_order: Union[SortOrder, None] = None
                                   ) -> requests.models.Response:
    """
    Get a list of item in a folder in a user’s collection.

    Note: If folder_id is not 0, or the collection has been made private by its owner,
          authentication as the collection owner is required.

    No user Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{folder_id}/releases"
    headers = user.headers
    params = user.params

    if page:
        params["page"] = max(1, page)
    if per_page:
        params["per_page"] = max(1, per_page)
    if sort:
        params["sort"] = sort.name
    if sort_order:
        params["sort_order"] = sort_order.name

    return requests.get(url, headers=headers, params=params)

# Cell


def add_release_to_collection_folder(user: UserWithUserTokenBasedAuthentication,
                                     username: str,
                                     folder_id: int,
                                     release_id: int
                                     ) -> requests.models.Response:
    """
    Add a release to a folder in a user’s collection.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{folder_id}/releases/{release_id}"
    headers = user.headers
    params = user.params

    return requests.post(url, headers=headers, params=params)

# Cell


def move_release_to_another_collection_folder(user: UserWithUserTokenBasedAuthentication,
                                              username: str,
                                              source_folder_id: int,
                                              destination_folder_id: int,
                                              release_id: int,
                                              instance_id: int
                                              ) -> requests.models.Response:
    """
    Move the instance of an release to another folder.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{source_folder_id}/releases/{release_id}/instances/{instance_id}"
    params = user.params

    data = {"folder_id": destination_folder_id}

    return requests.post(url, params=params, json=data)

# Cell


def change_rating_of_release_in_collection_folder(user: UserWithUserTokenBasedAuthentication,
                                                  username: str,
                                                  folder_id: int,
                                                  release_id: int,
                                                  instance_id: int,
                                                  rating: int
                                                  ) -> requests.models.Response:
    """
    Change the rating of the instance of an release in given collection folder.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{folder_id}/releases/{release_id}/instances/{instance_id}"
    params = user.params

    rating = min(max(0, rating), 5)
    data = {"rating": rating}

    return requests.post(url, params=params, json=data)

# Cell


def delete_release_instance_from_collection_folder(user: UserWithUserTokenBasedAuthentication,
                                                   username: str,
                                                   folder_id: int,
                                                   release_id: int,
                                                   instance_id: int
                                                   ) -> requests.models.Response:
    """
    Remove an instance of a release from a given collection folder.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{folder_id}/releases/{release_id}/instances/{instance_id}"
    headers = user.headers
    params = user.params

    return requests.delete(url, headers=headers, params=params)

# Cell


def list_custom_fields_for_collection_folders(user: Union[UserWithoutAuthentication,
                                                          UserWithUserTokenBasedAuthentication],
                                              username: str
                                              ) -> requests.models.Response:
    """
    Retrieve a list of user-defined collection notes fields.
    These fields are available on every release in the collection.

    Note: If you are not authenticated as the collection owner,
          only fields with public set to true will be visible.

    No user Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/fields"
    headers = user.headers
    params = user.params

    return requests.get(url, headers=headers, params=params)

# Cell


def edit_custom_field_value_for_release_instance_from_collection_folder(user: UserWithUserTokenBasedAuthentication,
                                                                        username: str,
                                                                        value: str,
                                                                        folder_id: int,
                                                                        release_id: int,
                                                                        instance_id: int,
                                                                        field_id: int,
                                                                        ) -> requests.models.Response:
    """
    Change the value of a custom field on a particular instance of an release in given collection folder.

    No user Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/folders/{folder_id}/releases/{release_id}/instances/{instance_id}/fields/{field_id}"
    params = user.params
    data = {"value": value}

    return requests.post(url, params=params, json=data)

# Cell


def get_collection_value(user: UserWithUserTokenBasedAuthentication,
                         username: str
                         ) -> requests.models.Response:
    """
    Get the minimum, median, and maximum value of a given collection.

    User Authentication needed.
    """

    url = f"{USERS_URL}/{username}/collection/value"
    headers = user.headers
    params = user.params

    return requests.get(url, headers=headers, params=params)