#!python
"""
CLI (Command line interface for the virus scanner)
Parameters:
    |
    |__ Virus Scanner 
             |_ file name to scan 
             |_ URL to scan 
"""

import click

# Importing file from different folders
import sys
import os
from os import path
import platform
import virus_scanner as vs

# Config file
if platform.system() == "Windows":
    config_path = os.path.dirname(vs.__file__) + "\\config.yaml"
else:
    config_path = os.path.dirname(vs.__file__) + "/config.yaml"

# checks if user has created a config file. If no config is present, it prompts the user for the API keys
if path.isfile(config_path) != True:
    print("No config file")
    f = open(config_path, "w+")
    metakey = input("Enter API key for MetaDefender: ")
    virusscankey = input("Enter API key for VirusTotal: ")
    f.write("Meta_Defender_API_key: " + metakey + "\n")
    f.write("Virus_Total_API_key: " + virusscankey + "\n")
    f.close()

from virus_scanner.vscan_config import *

# Import the interactive main file from the Main directory
import virus_scanner.Main.main as m

# Import the metadefender file
import virus_scanner.Meta_Defender.MetaDefenderMain as meta

# Import the VirusTotal files
import virus_scanner.VirusTotal_API.VirusTotal_API_File as vfile
import virus_scanner.VirusTotal_API.VirusTotal_API_URL as vurl


@click.group()
def main():
    pass


@main.command()
@click.option("--VirusTotalFile", "-VTF", help="Scan a file using the VirusTotal API")
@click.option("--VirusTotalURL", "-VTU", help="Scan a website using the VirusTotal API")
@click.option("--MetaDefender", "-M", help="Scan a file using the MetaDefender API")
@click.option("-i", is_flag=True, help="Opens in interactive command line mode")
@click.option("-e", is_flag=True, help="Opens a text editor to change API keys")
@click.option(
    "-v",
    is_flag=True,
    help="Gives a verbose output which prints the scan result of each scanner",
)
def main(virustotalfile, virustotalurl, metadefender, i, e, v):
    """This is a CLI application to scan a file or a website using multiple scanners to check for any form of malware"""
    verbose = False
    if v:
        verbose = True
    if virustotalfile:
        """
        Parameters (<file to scan>,<virus total api key>)
        """
        vfile.ScanFile(virustotalfile, Virus_Total_API_key(), verbose)

    elif virustotalurl:
        """
        Parameters (<url to scan>,<virus total api key>)
        """
        vurl.ScanURL(virustotalurl, Virus_Total_API_key(), verbose)

    elif metadefender:
        meta.scanFile(metadefender, Meta_Defender_API_key(), verbose)

    elif i:
        m.mainfile(verbose)
    elif e:
        if platform.system() == "Windows":
            os.system(f"notepad {config_path}")
        else:
            os.system(f"nano {config_path}")
    else:  # shows help options if no options are input
        ctx = click.get_current_context()
        click.echo(ctx.get_help())


if __name__ == "__main__":
    main()
