# Created byMartin.cz
# Copyright (c) Martin Strohalm. All rights reserved.

# See the Rebrickable API documentation at https://rebrickable.com/api/v3/docs/

from . import config
from .request import request


def get_categories(page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details for all available part categories.
    
    Args:
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "part_categories/"
    
    return request(path, parameters)


def get_category(category_id, api_key=None):
    """
    Gets details about specific part category.
    
    Args:
        category_id: str or int
            Rebrickable category id.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {'key': api_key}
    
    path = config.API_LEGO_URL + "part_categories/%s/" % category_id
    
    return request(path, parameters)


def get_colors(page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details for all available colors.
    
    Args:
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "colors/"
    
    return request(path, parameters)


def get_color(color_id, api_key=None):
    """
    Gets details about specific color.
    
    Args:
        color_id: str or int
            Rebrickable color id.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {'key': api_key}
    
    path = config.API_LEGO_URL + "colors/%s/" % color_id
    
    return request(path, parameters)


def get_element(element_id, api_key=None):
    """
    Gets details about specific element.
    
    Args:
        element_id: str or int
            Rebrickable element id.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {'key': api_key}
    
    path = config.API_LEGO_URL + "elements/%s/" % element_id
    
    return request(path, parameters)


def get_moc(moc_id, api_key=None):
    """
    Gets details about specific MOC.
    
    Args:
        moc_id: str or int
            Rebrickable MOC id.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    if 'MOC' not in str(moc_id):
        moc_id = "MOC-%s" % moc_id
    
    parameters = {'key': api_key}
    
    path = config.API_LEGO_URL + "mocs/%s/" % moc_id
    
    return request(path, parameters)


def get_moc_elements(moc_id, part_details=False, page=None, page_size=None, ordering=None, api_key=None):
    """Gets list of elements for a specific MOC.
    
    Args:
        moc_id: str or int
            Rebrickable MOC id.
        
        part_details: bool
            If set to True part details will be retrieved.
        
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    if 'MOC' not in str(moc_id):
        moc_id = "MOC-%s" % moc_id
    
    parameters = {
        'inc_part_details': int(part_details) or None,
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "mocs/%s/parts/" % moc_id
    
    return request(path, parameters)


def get_parts(search=None, part_id=None, part_ids=None, part_cat_id=None, color_id=None, bricklink_id=None, brickowl_id=None, lego_id=None, ldraw_id=None, part_details=False, page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details for all available parts with optional filters.
    
    Args:
        search: str, int or None
            Search term e.g. part ID or name.
        
        part_id: str, int or None
            Rebrickable part ID.
        
        part_ids: (str,), (int,) or None
            Rebrickable part IDs.
        
        part_cat_id: str, int or None
            Rebrickable category ID.
        
        color_id: str, int or None
            Rebrickable color ID.
        
        bricklink_id: str, int or None
            BrickLink part ID.
        
        brickowl_id: str, int or None
            BrickOwl part ID.
        
        lego_id: str, int or None
            LEGO part ID.
        
        ldraw_id: str, int or None
            LDraw part ID.
        
        part_details: bool
            If set to True part details will be retrieved.
        
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    if part_ids:
        part_ids = ",".join(str(i) for i in part_ids)
    
    parameters = {
        'search': search,
        'part_num': part_id,
        'part_nums': part_ids,
        'part_cat_id': part_cat_id,
        'color_id': color_id,
        'bricklink_id': bricklink_id,
        'brickowl_id': brickowl_id,
        'lego_id': lego_id,
        'ldraw_id': ldraw_id,
        'inc_part_details': int(part_details) or None,
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "parts/"
    
    return request(path, parameters)


def get_part(part_id, api_key=None):
    """
    Gets details about specific part.
    
    Args:
        part_id: str or int
            Rebrickable part id.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {'key': api_key}
    
    path = config.API_LEGO_URL + "parts/%s/" % part_id
    
    return request(path, parameters)


def get_part_color(part_id, color_id, api_key=None):
    """
    Gets details about specific part/color combination.
    
    Args:
        part_id: str or int
            Rebrickable part id.
        
        color_id: str or int
            Rebrickable color id.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {'key': api_key}
    
    path = config.API_LEGO_URL + "parts/%s/colors/%s/" % (part_id, color_id)
    
    return request(path, parameters)


def get_part_colors(part_id, page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details about available colors for specific part.
    
    Args:
        part_id: str or int
            Rebrickable part id.
        
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "parts/%s/colors/" % part_id
    
    return request(path, parameters)


def get_part_color_sets(part_id, color_id, page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details about available sets containing specific part/color
    combination.
    
    Args:
        part_id: str or int
            Rebrickable part id.
        
        color_id: str or int
            Rebrickable color id.
        
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "parts/%s/colors/%s/sets/" % (part_id, color_id)
    
    return request(path, parameters)


def get_sets(search=None, theme_id=None, min_year=None, max_year=None, min_pieces=None, max_pieces=None, page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details for all available sets with optional filters.
    
    Args:
        search: str, int or None
            Search term e.g. set ID or name.
        
        theme_id: str, int or None
            Rebrickable theme ID.
        
        min_year: int or None
            Minimum allowed release year.
        
        max_year: int or None
            Maximum allowed release year.
        
        min_pieces: int or None
            Minimum number of pieces.
        
        max_pieces: int or None
            Maximum number of pieces.
        
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {
        'search': search,
        'theme_id': theme_id,
        'min_year': min_year,
        'max_year': max_year,
        'min_parts': min_pieces,
        'max_parts': max_pieces,
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "sets/"
    
    return request(path, parameters)


def get_set(set_id, api_key=None):
    """
    Gets details about specific set.
    
    Args:
        set_id: str or int
            Rebrickable set id.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    if '-' not in str(set_id):
        set_id = "%s-1" % set_id
    
    parameters = {'key': api_key}
    
    path = config.API_LEGO_URL + "sets/%s/" % set_id
    
    return request(path, parameters)


def get_set_elements(set_id, part_details=False, page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details about available elements for specific set.
    
    Args:
        set_id: str or int
            Rebrickable set id.
        
        part_details: bool
            If set to True part details will be retrieved.
        
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    if '-' not in str(set_id):
        set_id = "%s-1" % set_id
    
    parameters = {
        'inc_part_details': int(part_details) or None,
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "sets/%s/parts/" % set_id
    
    return request(path, parameters)


def get_set_alternates(set_id, page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details about available alternate builds for specific set.
    
    Args:
        set_id: str or int
            Rebrickable set id.
        
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    if '-' not in str(set_id):
        set_id = "%s-1" % set_id
    
    parameters = {
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "sets/%s/alternates/" % set_id
    
    return request(path, parameters)


def get_themes(page=None, page_size=None, ordering=None, api_key=None):
    """
    Gets details for all available set themes.
    
    Args:
        page: int or None
            A page number within the paginated result set.
        
        page_size: int or None
            Number of results to return per page.
        
        ordering: str or None
            Specifies the field to use for results ordering.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {
        'page': page,
        'page_size': page_size,
        'ordering': ordering,
        'key': api_key}
    
    path = config.API_LEGO_URL + "themes/"
    
    return request(path, parameters)


def get_theme(theme_id, api_key=None):
    """
    Gets details about specific set theme.
    
    Args:
        theme_id: str or int
            Rebrickable theme id.
        
        api_key: str or None
            Rebrickable API access key. If set to None the one set by
            rebrick.init() is used.
    
    Returns:
        http.client.HTTPResponse
            Server response.
    """
    
    parameters = {'key': api_key}
    
    path = config.API_LEGO_URL + "themes/%s/" % theme_id
    
    return request(path, parameters)
