#!/usr/bin/env python3
from rmoptions import RMOptionHandler
from rmsectkf.core.commands.module_command import ModuleCommand
from rmsectkf.core.commands.create_command import CreateCommand
from rmsectkf.core.commands.add_command import AddCommand
from rmsectkf.core.commands.note_command import NoteCommand
from rmsectkf.core.commands.flag_command import FlagCommand
from rmsectkf.core.module.module_loader import ModuleLoader
from rmsectkf.core.helpers.update_helper import UpdateHelper
import os
from pathlib import Path

# initialize rm-sec-toolkit
# check if shared dir exists
# if not create it, load the git repo to it and move the modules in it
ModuleLoader.load_default_modules_from_github()

# create the option handler and set the commands
option_handler = RMOptionHandler()
option_create = option_handler.create_option("create", "create a resource",
                                             short_name="c", required=False,
                                             quit_after_this_option=True)

option_add = option_handler.create_option("add", "add a resource",
                                          short_name="a", required=False,
                                          quit_after_this_option=True)

option_note = option_handler.create_option("note", "add a note",
                                           short_name="n", required=False,
                                           quit_after_this_option=True)

option_flag = option_handler.create_option("flag", "add a flag for ctf's",
                                           short_name="f", required=False,
                                           quit_after_this_option=True)

option_module = option_handler.create_option("module", "choose a module",
                                             short_name="m", required=False,
                                             quit_after_this_option=True)

option_version = option_handler.create_option("version", "show the current version",
                                              short_name="v",
                                              required=False,
                                              quit_after_this_option=True)

option_interactive = option_handler.create_option("interactive", "use the interactive mode (default)",
                                                  short_name="i",
                                                  required=False,
                                                  needs_value=False)

# check the options
if not option_handler.check():
    option_handler.print_error()
    option_handler.print_usage()
    exit()

# handle the commands
if option_handler.activated_main_option:
    success = False
    if option_handler.activated_main_option == option_module:
        module_command = ModuleCommand()
        success = module_command.handle_option(option_module)
    elif option_handler.activated_main_option == option_create:
        create_command = CreateCommand()
        success = create_command.handle_option(option_create)
    elif option_handler.activated_main_option == option_add:
        add_command = AddCommand()
        success = add_command.handle_option(option_add)
    elif option_handler.activated_main_option == option_note:
        note_command = NoteCommand()
        success = note_command.handle_option(option_note)
    elif option_handler.activated_main_option == option_flag:
        flag_command = FlagCommand()
        success = flag_command.handle_option(option_flag)
    elif option_handler.activated_main_option == option_version:
        print(UpdateHelper.get_current_version_number())
        success = True
    if not success:
        option_handler.print_usage()
    exit()


# interactive mode
def main():
    module_loader = ModuleLoader(ModuleLoader.get_available_paths())
    while True:
        # print lists, get selections and load modules if selected
        selection = module_loader.show_list_and_get_selection()

        if selection:
            if module_loader.is_folder_in_path_a_category(selection):
                module_loader.append_to_current_path(selection)
            elif module_loader.is_folder_in_path_a_module(selection):
                module = module_loader.get_module_with_selection(selection).import_and_get_class()
                module.init_module()
                module.start_module()


if __name__ == "__main__":
    main()
