import argparse
import sys
import os
from pyfiglet import Figlet

from .analyzer import PEAnalyzer, PEPrinter
from .virustotal import VirusTotal
from .logger import Logger


def main():
    parser = argparse.ArgumentParser(
        prog=f'{os.path.basename(sys.argv[0])} <PE file>')

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-F', '--file', metavar="FILE", help='Target file')
    group.add_argument('-H', '--hash', metavar="HASH", help='Target hash (VirusTotal Search requires VT_API_KEY)')
    parser.add_argument('-a', '--all', required=False, action='store_true', help='Show all info')
    parser.add_argument('-e', '--exports', required=False, action='store_true', help='List exports')
    parser.add_argument('-i', '--imports', required=False, action='store_true', help='List imports')
    parser.add_argument('-s', '--sections', required=False, action='store_true', help='List sections')
    parser.add_argument('-c', '--certificates', required=False, action='store_true', help='Get certificates information')
    parser.add_argument('-q', '--quiet', required=False, action='store_true', help='Do not print the banner')
    
    args = parser.parse_args()
    
    f = Figlet(font='slant')
    banner = f.renderText("percer")

    # Do not print the banner if run in quiet mode
    if not args.quiet:
        print(banner)

    # Initialize logger
    log = Logger("percer")

    try:
        if args.file:
            pex_obj = PEAnalyzer.from_file(args.file)
            
        elif args.hash:
            with VirusTotal() as scanner:
                hash = scanner.resolve_hash(args.hash)
                v_obj_content = scanner.get_content(hash)
                pex_obj = PEAnalyzer.from_bytes(v_obj_content)

        printer = PEPrinter(pex_obj)

        if args.all:
            print(pex_obj.handle)

        elif args.exports:
            printer.print_header()
            printer.print_exports()

        elif args.imports:
            printer.print_header()
            printer.print_imports()

        elif args.sections:
            printer.print_header()
            printer.print_sections()

        elif args.certificates:
            printer.print_header()
            printer.print_certificates()

        else:
            printer.print_header()
            printer.print_information()

    except FileNotFoundError:
        log.err(f"File not found: {args.file}")
        sys.exit(1)
    except ValueError as E:
        log.err(f"Value error: {E}")
        sys.exit(1)
    except Exception as E:
        log.err(f"Unexpected error: {E}")
        sys.exit(1)

    return 0
