"""
Plugin task
====================================
A core functionality to execute different plugin tasks.
"""
import os
from enum import Enum
from functools import partial


def _add_action(name: str, directory: str = None) -> None:
    """Create a new plugin

    It will generate all the required structure for a new plugin (files and folders).

    Parameters
    ----------
    name : str
        Name of the plugin to create.
    directory : str
        The path to create the plugin.
    """
    path = directory if directory is not None else os.getcwd()
    if os.path.exists(f"{path}/{name}"):
        raise FileExistsError(f"Directory {path}/{name} already exists.")
    os.mkdir(f"{path}/{name}")
    plugin_name = f"{name.capitalize()}Plugin"
    with open(f"{path}/{name}/__init__.py", 'w') as init_file:
        init_file.write(f"import .{name} from {plugin_name}\n")
        init_file.close()
    with open(f"{path}/{name}/{name}.py", 'w') as plugin_file:
        plugin_file.write(f"from openvariant.plugins.plugin import Plugin\n\n\n")
        plugin_file.write(f"class {plugin_name}(Plugin):\n")
        plugin_file.write(f"\t\"\"\"\n")
        plugin_file.write(f"\t{plugin_name} class generated by OpenVariant\n\n")
        plugin_file.write(f"\tMethods\n")
        plugin_file.write(f"\t-------\n")
        plugin_file.write(f"\trun(row: str)\n")
        plugin_file.write(f"\t\tMain method to execute data transformation in each row.\n")
        plugin_file.write(f"\t\"\"\"\n\n")
        plugin_file.write(f"\tdef run(self, row: dict) -> dict:\n")
        plugin_file.write(f"\t\t\"\"\"\n")
        plugin_file.write(f"\t\tData transformation of a single row\n\n")
        plugin_file.write(f"\t\tParameters\n")
        plugin_file.write(f"\t\t-------\n")
        plugin_file.write(f"\t\trow : dict\n")
        plugin_file.write(f"\t\t\tRow parsed by Annotation file (represented as a dict, where Keys are fields)\n")
        plugin_file.write(f"\t\t\n")
        plugin_file.write(f"\t\tReturns\n")
        plugin_file.write(f"\t\t-------\n")
        plugin_file.write(f"\t\tdict\n")
        plugin_file.write(f"\t\t\tRow parsed with the application of data transformation.\n")
        plugin_file.write(f"\t\t\"\"\"\n")
        plugin_file.write(f"\n")
        plugin_file.write(f"\t\treturn row\n")


class PluginActions(Enum):
    """Enum to get the function of each action"""

    """Create a new plugin"""
    CREATE = partial(_add_action)
