from adam.commands.command import Command
from adam.config import Config
from adam.utils_apps.app_session import AppSession
from adam.utils_apps.apps import Apps
from adam.utils_repl.repl_state import ReplState
from adam.presentation.tabulize import tabulize

class ShowAppActions(Command):
    COMMAND = 'show app actions'

    # the singleton pattern
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, 'instance'): cls.instance = super(ShowAppActions, cls).__new__(cls)

        return cls.instance

    def __init__(self, successor: Command=None):
        super().__init__(successor)

    def command(self):
        return ShowAppActions.COMMAND

    def required(self):
        return ReplState.A

    def run(self, cmd: str, state: ReplState):
        if not (args := self.args(cmd)):
            return super().run(cmd, state)

        with self.validate(args, state) as (args, state):
            with self.context(args) as (_, ctx):
                actions = []
                for typ in Apps().app_types():
                    actions.extend(typ.actions)

                lines = tabulize(actions,
                                lambda a: str(a),
                                header='ACTION,ARGS,DESCRIPTION',
                                separator=',',
                                ctx=ctx)
                ctx.log()

                app_session: AppSession = AppSession.create(state.app_env or 'c3', state.app_app or 'c3')
                endpoint = Config().get('app.console-endpoint', 'https://{host}/{env}/{app}/static/console/index.html')
                endpoint = endpoint.replace('{host}', app_session.host).replace('{env}', app_session.env).replace('{app}', state.app_app or 'c3')
                tabulize([f'CONSOLE:,{endpoint}'],
                         separator=',',
                         ctx=ctx)

                return lines

    def completion(self, state: ReplState):
        return super().completion(state)

    def help(self, state: ReplState):
        return super().help(state, 'show registered app actions')