# -*- coding: utf-8 -*-
"""
    Implementation of Flash Programmer current command

Copyright 2015 Freescale Semiconductor, Inc.
Copyright 2017-2020, 2024 NXP.

SPDX-License-Identifier: BSD-3-Clause

"""

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

from flash.gdbcommands.base_command import CwFlashBase
from flash.gdbcommands.cmd_name import CMD_CW_FLASH_CURRENT

from flash.scripts.utils import get_soc_ctx_from_current
from flash.scripts.remote import execute_remote
from flash.scripts.utils import gdb_execute

###################################################################
######### flash programmer current command
###################################################################
class CwFlashCurrent(CwFlashBase):
    """
usage: [-h] dev

S32DS Flash Programming current command.

positional arguments:
  dev         select current device

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

    def __init__(self):
        """
            Register new command with GDB
            Add a parameter parser with only one parameter used to select
            current device
        """
        super(CwFlashCurrent, self).__init__(CMD_CW_FLASH_CURRENT,
            gdb.COMMAND_USER, -1)
        parser = argparse.ArgumentParser(description='S32DS \
                                         Flash Programming current command.')
        parser.add_argument('dev',
                            help = 'select current device')
        self._parser = parser

    def complete(self, text, word):
        """
            Method called by GDB when user attempts to complete this command
            Right now do to gdb.COMPLETE_COMMAND flash sent in __init__ this
            method has less usefulness
        """
        # get soc context first
        cmd_string = 'mon ctx get current'
        result = execute_remote(cmd_string, to_string = True)
        soc_ctx = '.'
        if result:
            result = result.rstrip('\n')
            soc_ctx = get_soc_ctx_from_current(result)
        cmd_string = 'mon ctx find %s | has prop type = flash' % soc_ctx
        found_devs = execute_remote(cmd_string, to_string = True)
        matching_devs = [dev for dev in found_devs.split()\
            if dev.startswith(word)]
        return matching_devs

    def execute(self, args, from_tty):
        """
            Execute command called with args parameters
            Build command string in the form of a monitor command 
            and call internal gdb execution.
            Change gdb prompt to reflect newly selected device in case of success
        """
        cmd_string = 'mon flash current %s' % args.dev.lower()

        #execute command
        execute_remote(cmd_string, from_tty, no_wait = True)

        #change prompt to flash programming prompt
        gdb_execute('set prompt (%s) ' % args.dev, from_tty)

    def show_progress_bar(self):
        pass

    def end_progress_bar(self):
        pass

def create_command():
    """
        Instantiate flash programmer current command
    """
    CwFlashCurrent()
