# -*- coding: utf-8 -*-
# Copyright 2024-2025 NXP
# SPDX-License-Identifier: BSD-3-Clause

#Base class for all RCON functions class
import gdb
import shlex
from gdb import Command   # @UnresolvedImport  # pylint: disable=F0401
from time import time
from rcon.scripts.utils import gdb_execute


from errors import Result, Error, ErrorCode

###################################################################
######### RCON base command
###################################################################
class RconBase(Command):
    """
        Base class for all RCON commands
    """

    def __init__(self, cmd_name, gdb_command_class, gdb_complete_class):
        super(RconBase, self).__init__(cmd_name,
            gdb_command_class)
        self._parser = None
        self._args = None
        self._output = None

    def invoke(self, arguments, from_tty):
        """
            Forward call to implementation dependent command with argument
            Implicitly call don't repeat to avoid that command be repeated
            when user enters a blank line
        """
        self.dont_repeat()

        try:
            self._args = self._parser.parse_args(shlex.split(arguments))
        except SystemExit as e:
            if e.code is not 0:
                Result(Error(ErrorCode.RCONParameterError, str(e))).unwrap()
            return
        
        self.can_execute(self._args)

        self.begin_command()

        try:
            self.execute(self._args)
            #self.__invoke(self._args, from_tty)
        except BaseException as e:
            Result(Error(ErrorCode.RCONUndefinedError, str(e))).unwrap()

        self.show_result()
        self.end_command()

    def can_execute(self, args):
        """
            Allow child class to throw an exception in case execution prerequisites
            are not met.
        """
        pass

    def get_probe(self):
        """
            Call this to get the current probe
        """
        return gdb_execute("mon ctx id :ccs get prop probe", to_string = True)


    def dont_repeat(self):
        """
            Call this in case that the command should not be repeated
            when user enter a blank line at command prompt
        """
        Command.dont_repeat(self)

    def complete(self, text, word):
        """
            Method called by GDB when user attempts to complete this command
            Right now do to gdb.COMPLETE_COMMAND RCON sent in __init__ this
            method has less usefulness
        """
        Command.complete(text, word)

    def execute(self, args):
        """
            Method called to actually execute a command with
            given parameters. A new command should overwrite this method
            to implement the expected behaviour or to format the specific
            remote command
        """
        pass
    
    def show_result(self):
        """
            General method to gather command result and format it and then
            send it to a stream
            A subclass should overwrite this method accordingly.
        """
        pass

    def begin_command(self):
        """
            General method used to do all particular stuff related to
            beginning procedure of a method. Should be overwrite accordingly
            by a subclass.
        """
        pass

    def end_command(self):
        """
            General method used to do all particular stuff related to
            ending procedure of a method. Should be overwrite accordingly
            by a subclass.
        """
        pass

    def __invoke(self, args, from_tty):
        """
            Internal method
        """
