#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#  Copyright 2021 Matthew "gargargarrick" Ellison <earthisthering@posteo.de>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.

"""Get info on URLs."""

import json, requests, sys, typing, pprint


def getAllPermalinks(j: dict) -> list:
    """
    Get URLs for all comics on the site.

    Parameters
    ----------
    j : dict
        The site JSON contents.

    Returns
    -------
    list of list
        A list of lists -- one for each category -- of comic page URLs
        on the site.
    """
    return [sorted([page["url"] for page in cat["strips"]]) for cat in j["categories"]]


def getCatPermalinks(j: dict, cat_name: str) -> list:
    """
    Get URLs for one category on the site.

    Parameters
    ----------
    j : dict
        The site JSON contents.
    cat_name : str
        The name of the category to search.

    Returns
    -------
    list of str
        A list of all comic page URLs in the category.
    """
    try:
        (cat,) = [item for item in j["categories"] if item["category"] == cat_name]
        return sorted([page["url"] for page in cat["strips"]])
    except ValueError:
        print(
            f"""Category {cat_name} not found. Available categories: {"; ".join([item["category"] for item in j["categories"]])}"""
        )
        return False


def getChapPermalinks(j: dict, cat_name: str, chap_num: int) -> list:
    """
    Get URLs for one chapter.

    Parameters
    ----------
    j : dict
        The site JSON contents.
    cat_name : str
        The name of the category to search.
    chap_num : int
        The chapter number to retrieve pages from.

    Returns
    -------
    list of str
        A list of all comic page URLs in the chapter.
    """
    try:
        (cat,) = [item for item in j["categories"] if item["category"] == cat_name]
    except ValueError:
        print(
            f"""Category {cat_name} not found. Available categories: {"; ".join([item["category"] for item in j["categories"]])}"""
        )
        return False
    return sorted(
        [
            item["url"]
            for item in cat["strips"]
            if "chapter" in item.keys() and item["chapter"] == str(chap_num)
        ]
    )
