"""Class to sort comic file based on it's metadata tags"""
import os
from shutil import Error, move

from ..comicapi.comicarchive import ComicArchive, MetaDataStyle
from .utils import cleanup_string


class FileSorter:
    """Class to move comic files based on it's metadata tags"""

    def __init__(self, directory):
        self.set_directory(directory)

    def set_directory(self, directory):
        """Directory where comic files are to be moved"""
        self.sort_directory = directory

    # def set_template(self, template):
    #     self.template = template

    def sort_comics(self, comic):
        """Method to move the comic file based on it's metadata tag"""
        comic_archive = ComicArchive(comic)
        if comic_archive.hasMetadata(MetaDataStyle.CIX):
            meta_data = comic_archive.readMetadata(MetaDataStyle.CIX)
        else:
            return False

        if meta_data is not None:
            # Cleanup the publisher & series metadata so they play nicely with filesystems.
            publisher = cleanup_string(meta_data.publisher)
            series = cleanup_string(meta_data.series)
            if meta_data.volume:
                volume = "v" + cleanup_string(meta_data.volume)
                new_path = (
                    self.sort_directory
                    + os.sep
                    + publisher
                    + os.sep
                    + series
                    + os.sep
                    + volume
                    + os.sep
                )
            else:
                new_path = (
                    self.sort_directory + os.sep + publisher + os.sep + series + os.sep
                )
        else:
            return False

        if not os.path.isdir(new_path):
            os.makedirs(new_path)

        try:
            move(comic, new_path)
            print(f"moved {os.path.basename(comic)} to {new_path}.")
        except Error:
            return False

        return True
