
from libs.pyfap.fap import CommandBase
from libs.spk_v.runtime import SPKRPkgTask
from libs.spk_v.tasks.PrintLinuxPdInfoTask import PrintLinuxPdInfoTask
from libs.spk_v.tasks.PrintOsInfoTask import PrintOsInfoTask
from libs.spk_v.tasks.RunPkgTask import RunPkgTask
from libs.spk_v.tasks.SearchTask import SearchTask


class NoCommand(CommandBase):
    def __init__(self, syspack_instance):
        super().__init__(None)
        self.syspack_instance = syspack_instance

    def run(self, args):
        f = args.get('args_l2').get('file')
        if f is not None:
            if f != self.core.NO_VALUE:
                self.syspack_instance.runtime.import_tasks_from_file(f)
                self.syspack_instance.runtime.exec_tasks()
            else:
                print('argument --file require a value. Example: syspack --file=tasks.json')
        else:
            print("No command specifed")
            self.core.print_help()
            return 2

class RunCommand(CommandBase):
    def __init__(self, syspack_instance):
        super().__init__('run')
        self.desc = 'Command run specified package'
        self.usage = 'syspack run hello-world?version=1.0 text=Hello_Python_World!'
        self.sp = syspack_instance

    def run(self, args):
        self.sp.task_manager.queue.append(RunPkgTask('run', args.get('args_l0')[0], args.get('args_l0')[1:]))

class RemoveCommand(CommandBase):
    def __init__(self, syspack_instance):
        super().__init__('remove')
        self.desc = 'Command run specified package'
        self.usage = 'syspack run hello-world?version=1.0 text=Hello_Python_World!'
        self.sp = syspack_instance

    def run(self, args):
        self.sp.task_manager.queue.append(RunPkgTask('remove', args.get('args_l0')[0], args.get('args_l0')[1:]))


class InstallCommand(CommandBase):
    def __init__(self, syspack_instance):
        super().__init__('install')
        self.desc = 'Command install specified package'
        self.usage = 'syspack install hello-world?version=1.0 text=Hello_Python_World!'
        self.sp = syspack_instance
    def run(self, args):
        self.sp.task_manager.queue.append(RunPkgTask('install', args.get('args_l0')[0], args.get('args_l0')[1:]))


class OsInfo(CommandBase):
    def __init__(self, app):
        super().__init__('os-version')
        self.desc = 'Command print os distribution info, as seen by the SysPack engine'
        self.usage = 'syspack os-version'
        self.sp = app
    def run(self, args):
        self.sp.task_manager.queue.append(PrintOsInfoTask(args))

class LinuxPdInfo(CommandBase):
    def __init__(self, app):
        super().__init__('linux-pd-info')
        self.desc = 'Print info from Linux Platform Detector module'
        self.usage = 'syspack linux-pd-info'
        self.sp = app
    def run(self, args):
        self.sp.task_manager.queue.append(PrintLinuxPdInfoTask(args))

class PkgList(CommandBase):
    def __init__(self, syspack_instance):
        super().__init__('search')
        self.desc = 'Search pkgs in repo'
        self.usage = [
            'syspack search any?arch=any+os=linux',
            'syspack search iperf?pkg=rpm',
        ]
        self.sp = syspack_instance

    def run(self, args):
        a = args
        self.sp.task_manager.queue.append(SearchTask(args))
class PkgList2(CommandBase):
    def __init__(self, syspack_instance):
        super().__init__('list')
        self.desc = 'Print all pkgs'
        self.usage = 'syspack list'
        self.sp = syspack_instance

    def run(self, args):
        a = args
        self.sp.task_manager.queue.append(SearchTask(args))
class About(CommandBase):
    def __init__(self, syspack_instance):
        super().__init__('about')
        self.desc = 'Print info about this software'
        self.usage = 'syspack about'
        self.sp = syspack_instance

    def run(self, args):
        a = args
        print(f"""
        {self.sp.APP_NAME} ver: {self.sp.APP_VERSION}
        
        {self.sp.APP_DESC}        
        PR_Automata (C) 2020-2025
    
        Created by {self.sp.APP_AUTHOR}
        
        peace. love. code.
        """)

def spk_reg_all(cli, syspack_instance):
    cli.reg(RunCommand(syspack_instance))
    cli.reg(InstallCommand(syspack_instance))
    cli.reg(RemoveCommand(syspack_instance))
    cli.reg(OsInfo(syspack_instance))
    cli.reg(LinuxPdInfo(syspack_instance))
    cli.reg(PkgList(syspack_instance))
    cli.reg(PkgList2(syspack_instance))
    cli.reg(About(syspack_instance))
    cli.no_command_action = NoCommand(syspack_instance)
    cli.no_command_action.core = cli
