# -*- coding: utf-8 -*-
"""
    Implementation of RCON Read command

Copyright 2024 NXP
SPDX-License-Identifier: BSD-3-Clause

"""

import gdb   # @UnresolvedImport  # pylint: disable=F0401
import argparse
import binascii
import importlib

from rcon.gdbcommands.base_command import RconBase
from rcon.gdbcommands.cmd_name import CMD_RCON_READ
from rcon.scripts.utils import string_to_value
from errors import Result, Error, ErrorCode

global RCON
###################################################################
######### RCON read command
###################################################################
class RconRead(RconBase):
    """
    \n
usage: Read RCON value. [-h] No parameters.

S32DS RCON read command.

positional arguments:
  start         Start address to read from
  
  length         Number of bytes to read

optional arguments:
  -h, --help            show this help message and exit
  
  \n
    """

    def __init__(self):
        """
            Register new command with GDB

        """
        super(RconRead, self).__init__(CMD_RCON_READ,
        gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)
        parser = argparse.ArgumentParser(usage='[-h] offset size',
                                         description='S32DS \
                                         RCON read command.')
        parser.add_argument('start',
                                    type = string_to_value,
                                    help = 'Start address for the read operation')
        parser.add_argument('count',
                                    type = string_to_value,
                                    help = 'Number of bytes to be read')
        self._parser = parser


    def execute(self, args):
        #execute command
        if args.count < 1:
            Result(Error(ErrorCode.RCONParameterError,'F-ERROR: Minimum of 1 byte must be read.')).unwrap()
        self._output = RCON.RCON_READ(args.start, args.count)

        gdb.write ("RCON data from EEPROM:  " + str(hex(self._output)) + "\n")

    def begin_command(self):
        gdb.write('Reading....\n')

    def end_command(self):
        gdb.write('Read complete....\n')


def create_command(RCONfilePath):
    """
        Instantiate RCON read command
    """
    global RCON 
    RCON = importlib.import_module(RCONfilePath[:-3])
    RconRead()
