#!/usr/bin/env python3
from classes.user import User
from functions import LOG
from functions import load_config_variables
from functions import quote
from functions import run


class Gallery:
    def __init__(self) -> None:
        self.archive: bool = True
        self.skip_arg: str = ""
        self.link: str = ""
        self.dest: str = ""
        self.list: str = ""
        self.opt_args: str = ""
        self.command: str = ""

    def generate_command(self, user: User = User(1), is_comic: bool = False) -> None:
        """Generates a command string."""
        if is_comic:
            configs = load_config_variables()
            directory = quote(configs["comic"]["download-dir"])
            database = quote(configs["comic"]["database"])
            queue = quote(configs["comic"][f"{self.list}-list"]) if self.list else ""
        else:
            directory = quote(str(user.directories[self.dest]))
            database = quote(str(user.dbs["gallery"]))
            queue = quote(str(user.lists[self.list])) if self.list else ""

        command = f"gallery-dl --sleep {str(user.sleep)}"
        command += self.skip_arg if self.skip_arg else ""
        command += f" --dest {directory}" if self.dest or is_comic else ""
        command += f" --download-archive {database}" if self.archive else ""
        command += self.opt_args if self.opt_args else ""

        if self.link and not self.list:
            command += f" {quote(self.link)}"
        if self.list and not self.link:
            command += f" -i {queue}"

        LOG.debug(command)
        self.command = command

    def run_command(self, verbose: bool):
        run(self.command, verbose)
