# -*- coding: utf-8 -*-
"""
@file   tmtcc_tc_p60dock.py
@brief  P60 Dock tests
@author J. Meier
@date   13.12.2020
"""
from tmtccmd.config import CmdTreeNode
from eive_tmtc.tmtc.power.common_power import (
    add_gomspace_nodes,
    pack_common_gomspace_cmds,
    req_hk_cmds,
    pack_common_power_cmds,
    SetId,
)
from tmtccmd.tmtc import DefaultPusQueueHelper
from eive_tmtc.gomspace.gomspace_common import (
    TableEntry,
    Channel,
    TableIds,
    pack_get_param_command,
    pack_gnd_wdt_reset_command,
    pack_ping_command,
    pack_reboot_command,
    pack_set_u8_param_command,
    pack_set_u16_param_command,
)
from eive_tmtc.config.object_ids import P60_DOCK_HANDLER
from tmtccmd.util import ObjectIdU32


class CmdString:
    STACK_3V3_ON = "stack_3v3_on"
    STACK_3V3_OFF = "stack_3v3_off"
    STACK_5V_ON = "stack_5v_on"
    STACK_5V_OFF = "stack_5v_off"
    TEST = "test"
    ACU_OFF = "acu_off"
    ACU_ON = "acu_on"


class CmdInfo:
    PREFIX = "P60 Dock"
    STACK_3V3_ON = f"{PREFIX}: Turn Stack 3V3 on"
    STACK_3V3_OFF = f"{PREFIX}: Turn Stack 3V3 off"
    STACK_5V_ON = f"{PREFIX}: Turn Stack 5V on"
    STACK_5V_OFF = f"{PREFIX}: Turn Stack 5V off"
    ACU_OFF = "Switch ACU off"
    ACU_ON = "Switch ACU on"


class P60DockTestProcedure:
    """
    @brief  Use this class to define the tests to perform for the P60Dock.
    @details    Setting all to True will run all tests.
                Setting all to False will only run the tests set to True.
    """

    all = False
    reboot = False
    read_gnd_wdt = False
    gnd_wdt_reset = False
    ping = False
    channel_3_off = False  # pdu2
    read_temperature1 = False
    read_channel_3_state = False  # pdu2
    read_cur_lu_lim_0 = False
    channel_3_on = False  # pdu2
    invalid_table_id_test = (
        False  # Test to check if software properly handles invalid table ids
    )
    invalid_address_test = (
        False  # Test to check if software properly handles invalid addresses
    )
    invalid_parameter_size_test = False


class P60DockConfigTable:
    out_en_0 = TableEntry(bytearray([0x00, 0x68]), TableEntry.uint8_size)  # ACU VCC
    out_en_1 = TableEntry(bytearray([0x00, 0x69]), TableEntry.uint8_size)  # PDU1 VCC
    out_en_2 = TableEntry(bytearray([0x00, 0x6A]), TableEntry.uint8_size)  # unused
    out_en_3 = TableEntry(bytearray([0x00, 0x6B]), TableEntry.uint8_size)  # PDU2 VCC
    out_en_4 = TableEntry(bytearray([0x00, 0x6C]), TableEntry.uint8_size)  # ACU VBAT
    out_en_5 = TableEntry(bytearray([0x00, 0x6D]), TableEntry.uint8_size)  # unused
    out_en_6 = TableEntry(bytearray([0x00, 0x6E]), TableEntry.uint8_size)  # PDU1 VBAT
    out_en_7 = TableEntry(bytearray([0x00, 0x6F]), TableEntry.uint8_size)  # PDU2 VBAT
    out_en_8 = TableEntry(bytearray([0x00, 0x70]), TableEntry.uint8_size)  # Stack VBAT
    out_en_9 = TableEntry(bytearray([0x00, 0x71]), TableEntry.uint8_size)  # Stack 3V3
    out_en_10 = TableEntry(bytearray([0x00, 0x72]), TableEntry.uint8_size)  # Stack 5V
    out_en_11 = TableEntry(
        bytearray([0x00, 0x73]), TableEntry.uint8_size
    )  # GS 3V3 (unused)
    out_en_12 = TableEntry(
        bytearray([0x00, 0x74]), TableEntry.uint8_size
    )  # GS 5V (unused)
    # When channel consumes more than cur_lu_lim, channel is turned of immediately
    cur_lu_lim_0 = TableEntry(bytearray([0x00, 0xF8]), TableEntry.uint16_size)


class P60DockHkTable:
    temperature1 = TableEntry(bytearray([0x00, 0x44]), TableEntry.uint16_size)
    temperature2 = TableEntry(bytearray([0x00, 0x46]), TableEntry.uint16_size)
    # Ground WDT value (remaining seconds until reboot)
    wdt_gnd_left = TableEntry(bytearray([0x00, 0xA8]), TableEntry.uint32_size)


def pack_p60dock_cmds(  # noqa C901: Complexity okay here.
    object_id: ObjectIdU32, q: DefaultPusQueueHelper, cmd_str: str
):
    objb = object_id.as_bytes
    pack_common_power_cmds("P60 Dock", object_id, q, cmd_str)
    pack_common_gomspace_cmds("P60 Dock", object_id, q, cmd_str)
    p60_dock_req_hk_cmds(q, cmd_str)
    if cmd_str == CmdString.STACK_3V3_ON:
        q.add_log_cmd(CmdInfo.STACK_3V3_ON)
        q.add_pus_tc(
            pack_set_u8_param_command(
                objb,
                P60DockConfigTable.out_en_9.parameter_address,
                Channel.on,
            )
        )
    if cmd_str == CmdString.STACK_3V3_OFF:
        q.add_log_cmd(CmdInfo.STACK_3V3_OFF)
        q.add_pus_tc(
            pack_set_u8_param_command(
                objb,
                P60DockConfigTable.out_en_9.parameter_address,
                Channel.off,
            )
        )
    if cmd_str == CmdString.STACK_5V_ON:
        q.add_log_cmd(CmdInfo.STACK_5V_ON)
        q.add_pus_tc(
            pack_set_u8_param_command(
                objb,
                P60DockConfigTable.out_en_10.parameter_address,
                Channel.on,
            )
        )
    if cmd_str == CmdString.STACK_5V_OFF:
        q.add_log_cmd(CmdInfo.STACK_5V_OFF)
        q.add_pus_tc(
            pack_set_u8_param_command(
                objb,
                P60DockConfigTable.out_en_10.parameter_address,
                Channel.off,
            )
        )
    if P60DockTestProcedure.all or P60DockTestProcedure.reboot:
        q.add_log_cmd("P60 Dock: Reboot")
        q.add_pus_tc(pack_reboot_command(object_id))
    if P60DockTestProcedure.all or P60DockTestProcedure.read_gnd_wdt:
        q.add_log_cmd("P60 Dock: Reading ground watchdog timer value")
        q.add_pus_tc(
            pack_get_param_command(
                objb,
                TableIds.HK,
                P60DockHkTable.wdt_gnd_left.parameter_address,
                P60DockHkTable.wdt_gnd_left.parameter_size,
            )
        )
    if P60DockTestProcedure.all or P60DockTestProcedure.gnd_wdt_reset:
        q.add_log_cmd("P60 Dock: Testing ground watchdog reset")
        q.add_pus_tc(pack_gnd_wdt_reset_command(object_id))
    if P60DockTestProcedure.all or P60DockTestProcedure.ping:
        q.add_log_cmd("P60 Dock: Ping")
        ping_data = bytearray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        q.add_pus_tc(pack_ping_command(object_id, ping_data))
    if P60DockTestProcedure.all or P60DockTestProcedure.channel_3_off:
        q.add_log_cmd("P60 Dock: Testing setting output channel 3 off")
        parameter = 0  # set channel off
        q.add_pus_tc(
            pack_set_u8_param_command(
                objb,
                P60DockConfigTable.out_en_3.parameter_address,
                parameter,
            )
        )
    if P60DockTestProcedure.all or P60DockTestProcedure.read_temperature1:
        q.add_log_cmd("P60 Dock: Testing temperature reading")
        q.add_pus_tc(
            pack_get_param_command(
                objb,
                TableIds.HK,
                P60DockHkTable.temperature1.parameter_address,
                P60DockHkTable.temperature1.parameter_size,
            )
        )
    if P60DockTestProcedure.all or P60DockTestProcedure.channel_3_on:
        q.add_log_cmd("P60 Dock: Testing  Output Channel 3 state (PDU2)")
        q.add_pus_tc(
            pack_get_param_command(
                objb,
                TableIds.CONFIG,
                P60DockConfigTable.out_en_3.parameter_address,
                P60DockConfigTable.out_en_3.parameter_size,
            )
        )
    if P60DockTestProcedure.all or P60DockTestProcedure.read_cur_lu_lim_0:
        q.add_log_cmd("P60 Dock: Reading current limit value of  output channel 0")
        q.add_pus_tc(
            pack_get_param_command(
                objb,
                TableIds.CONFIG,
                P60DockConfigTable.cur_lu_lim_0.parameter_address,
                P60DockConfigTable.cur_lu_lim_0.parameter_size,
            )
        )
    if P60DockTestProcedure.all or P60DockTestProcedure.channel_3_on:
        q.add_log_cmd("P60 Dock: Testing setting output channel 3 on")
        parameter = 1  # set channel on
        q.add_pus_tc(
            pack_set_u8_param_command(
                objb,
                P60DockConfigTable.out_en_3.parameter_address,
                parameter,
            )
        )
    if P60DockTestProcedure.all or P60DockTestProcedure.invalid_table_id_test:
        q.add_log_cmd("P60 Dock: Testing invalid table id handling")
        table_id_invalid = 5
        q.add_pus_tc(
            pack_get_param_command(
                objb,
                table_id_invalid,
                P60DockHkTable.temperature1.parameter_address,
                P60DockHkTable.temperature1.parameter_size,
            )
        )
    if P60DockTestProcedure.all or P60DockTestProcedure.invalid_address_test:
        q.add_log_cmd("P60 Dock: Testing invalid address handling in get param command")
        invalid_address = bytearray([0x01, 0xF4])
        q.add_pus_tc(
            pack_get_param_command(
                objb,
                TableIds.HK,
                invalid_address,
                P60DockHkTable.temperature1.parameter_size,
            )
        )
        q.add_log_cmd("P60 Dock: Testing invalid address handling in set param command")
        invalid_address = bytearray([0x01, 0xF4])
        parameter = 1
        q.add_pus_tc(pack_set_u16_param_command(objb, invalid_address, parameter))


def create_p60_dock_node() -> CmdTreeNode:
    node = CmdTreeNode(
        "p60_dock", "P60 PCDU dock device", hide_children_which_are_leaves=True
    )
    add_gomspace_nodes(node)
    node.add_child(CmdTreeNode(CmdString.STACK_3V3_ON, CmdInfo.STACK_3V3_ON))
    node.add_child(CmdTreeNode(CmdString.STACK_3V3_OFF, CmdInfo.STACK_3V3_OFF))
    node.add_child(CmdTreeNode(CmdString.STACK_5V_ON, CmdInfo.STACK_5V_ON))
    node.add_child(CmdTreeNode(CmdString.STACK_5V_OFF, CmdInfo.STACK_5V_OFF))
    node.add_child(CmdTreeNode(CmdString.TEST, "P60 Tests"))
    return node


def p60_dock_req_hk_cmds(q: DefaultPusQueueHelper, op_code: str):
    req_hk_cmds("P60 Dock", q, op_code, P60_DOCK_HANDLER, [SetId.CORE, SetId.AUX])