# Copyright 2018-2020, 2024-2025 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_CLOSE

from flash.scripts.remote import execute_remote



###################################################################
######### flash programmer write command
###################################################################
class CwFlashWriteElf(CwFlashBase):
    """
    usage: [-h] 

    S32DS Flash Programming close command.

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

    def __init__(self):
        """
                    Register new command with GDB
                    Add a parameter parser to easily specify parameters
                    An optional --erase parameter is added to specify if an erase of the
                    affected area should be performed first
        """
        super(CwFlashWriteElf, self).__init__(CMD_CW_FLASH_CLOSE,
                                              gdb.COMMAND_USER, gdb.COMPLETE_FILENAME)
        parser = argparse.ArgumentParser(usage='', \
                                     description='S32DS Flash Programming close command.')
        self._parser = parser
        CwFlashWriteElf.print_result = None
        
    def can_execute(self, args):
        """
            Internal method
        """
        pass
        
    def execute(self, args, from_tty):
        cmd_string = "mon ctx get current"
        reply = execute_remote(cmd_string, from_tty, True)
        
        ctx = reply[:reply.find(":", 5)] + ":SoC#0"
        cmd_string = "mon template config %s 4 100" % ctx
        current_flash = execute_remote(cmd_string, from_tty)
        
        cmd_string = "mon template config %s 3 100" % ctx
        current_flash = execute_remote(cmd_string, from_tty)
        
        cmd_string = "mon ctx id :ccs set prop disconnect_mode nothing"
        current_flash = execute_remote(cmd_string, from_tty)
        
        cmd_string = "mon shutdown"
        current_flash = execute_remote(cmd_string, from_tty)
        
        cmd_string = "disconnect"
        current_flash = execute_remote(cmd_string, from_tty)
        
        cmd_string = "quit"
        current_flash = execute_remote(cmd_string, from_tty)

    def show_progress_bar(self):
        pass

    def end_progress_bar(self):
        pass


def create_command():
    """
    Instantiate flash programmer write command
    """
    CwFlashWriteElf()
