"""Mariner open command."""

import logging

from cliff import command

from mariner.services.downloader import Service as DownloadService
from mariner.utils import color, path


class Open(command.Command):
    """Open torrent in the default torrent application."""

    log = logging.getLogger(__name__)

    def get_parser(self, prog_name):
        """Add arguments to argument parser.

        Args:
            prog_name: Application name.

        Returns:
            Instance of argument parser.

        """
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "ID",
            nargs="+",
            help="ID of the torrents to open",
            type=int,
        )
        return parser

    def _get_torrent_link(self, torrent):
        """Get a link to a torrent, that can be opened by xdg-open.

        Args:
            torrent: Torrent object.

        Returns:
            Openable torrent link.

        """
        if torrent.download:
            downloader = DownloadService(
                client=self.app.http_client,
                download_path=path.cache(),
            )
            paths = downloader.download([(torrent.download, torrent.filename)])
            return str(paths[0])
        return torrent.magnet

    def take_action(self, parsed_args):
        """Open chosen torrent in the default torrent application.

        Args:
            parsed_args: List of parsed arguments.

        """
        for tid in parsed_args.ID:
            torrent = self.app.engine.result(tid)
            self.log.debug("tid=%s torrent=%s", tid, torrent)
            self.log.info(f"Opening {color.yellow(torrent.name)}.")
            link = self._get_torrent_link(torrent)
            if self.app.options.verbose_level > 1:
                path.open_with_default_app(link, verbose=True)
            else:
                path.open_with_default_app(link)
            try:
                # Try deleting the file, if it exists
                link.unlink()
            except AttributeError:
                pass
