from empire.server.common.empire import MainMenu
from empire.server.core.module_models import EmpireModule
from empire.server.core.module_service import auto_finalize, auto_get_source


class Module:
    @staticmethod
    @auto_get_source
    @auto_finalize
    def generate(
        main_menu: MainMenu,
        module: EmpireModule,
        params: dict,
        obfuscate: bool = False,
        obfuscation_command: str = "",
        script: str = "",
    ):
        reset = params["Reset"]

        script = script + "\n"
        script_end = ""
        if reset.lower() == "true":
            # if the flag is set to restore the security settings
            script_end += "Reset-SecuritySettings "
        else:
            script_end += "Disable-SecuritySettings "

        for option, values in params.items():
            if (
                (
                    option.lower() != "agent"
                    and option.lower() != "reset"
                    and option.lower() != "outputfunction"
                )
                and values
                and values != ""
            ):
                if values.lower() == "true":
                    # if we're just adding a switch
                    script_end += " -" + str(option)
                else:
                    script_end += " -" + str(option) + " " + str(values)

        outputf = params.get("OutputFunction", "Out-String")
        script_end += (
            f" | {outputf} | "
            + '%{$_ + "`n"};"`n'
            + str(module.name.split("/")[-1])
            + ' completed!"'
        )

        return script, script_end
