#!/usr/bin/env python
"""Module provides human-friendly output from the MalwareBazaar Service"""

import logging

from hurry.filesize import size
from tabulate import tabulate

from ioccheck.cli.formatters.formatter import Formatter
from ioccheck.services import MalwareBazaar

logger = logging.getLogger(__name__)

f_handler = logging.FileHandler("ioccheck.log")
f_handler.setLevel(logging.INFO)

f_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
f_handler.setFormatter(f_format)

logger.addHandler(f_handler)


class MalwareBazaarFormatter(Formatter):
    """Provide pre-formatted output from the MalwareBazaar Service"""

    def __init__(self, service: MalwareBazaar):
        Formatter.__init__(self, service)

    @property
    def tags(self):
        """Pre-formatted output for user-submitted tags"""
        return ", ".join(self.service.tags) if self.service.tags else None

    @property
    def file_size(self):
        """Pre-formatted output for sample file size"""
        return size(self.service.file_size)

    @property
    def file_type(self):
        """Pre-formatted output for sample file type"""
        return f".{self.service.file_type}"

    @property
    def hashes(self):
        """Pre-formatted output for other hashing algorithm results"""
        results = [[k, v] for k, v in self.service.hashes.items() if v]

        table = [["Type", "Hash"]]

        for result in results:
            table.append(result)

        return tabulate(table, headers="firstrow", tablefmt="fancy_grid")
