# -*- coding: utf-8 -*-
"""ACU commands
@author J. Meier, R. Mueller
@date   21.12.2020
"""
import struct

from config.definitions import CustomServiceList
from tmtccmd.config import TmtcDefinitionWrapper, OpCodeEntry
from tmtccmd.config.tmtc import tmtc_definitions_provider

from tmtccmd.tc import DefaultPusQueueHelper
from tmtccmd.tc.pus_3_fsfw_hk import (
    make_sid,
    generate_one_diag_command,
    generate_one_hk_command,
)
import gomspace.gomspace_common as gs
from gomspace.gomspace_common import GomspaceOpCodes
from gomspace.gomspace_common import GsInfo as GsInfo
from config.object_ids import ACU_HANDLER_ID
from pus_tc.devs.p60dock import P60DockConfigTable
from tmtccmd.tc.pus_8_funccmd import generate_action_command
from tmtccmd.util import ObjectIdU32


class ACUConfigTable:
    mppt_mode = gs.TableEntry(bytearray([0x00, 0x00]), gs.TableEntry.uint8_size)
    mppt_d_mode = gs.TableEntry(bytearray([0x00, 0x01]), gs.TableEntry.uint8_size)
    vboost = gs.TableEntry(bytearray([0x00, 0x02]), gs.TableEntry.uint16_size)
    vbat_max_hi = gs.TableEntry(bytearray([0x00, 0x10]), gs.TableEntry.uint16_size)
    vbat_max_lo = gs.TableEntry(bytearray([0x00, 0x12]), gs.TableEntry.uint16_size)
    ov_mode = gs.TableEntry(bytearray([0x00, 0x1A]), gs.TableEntry.uint8_size)


class ACUHkTable:
    temperature1 = gs.TableEntry(bytearray([0x00, 0x1C]), gs.TableEntry.uint16_size)
    temperature2 = gs.TableEntry(bytearray([0x00, 0x1D]), gs.TableEntry.uint16_size)
    temperature3 = gs.TableEntry(bytearray([0x00, 0x1E]), gs.TableEntry.uint16_size)
    # Ground WDT value (remaining seconds until reboot)
    wdt_gnd_left = gs.TableEntry(bytearray([0x00, 0x74]), gs.TableEntry.uint32_size)


class OpCodes:
    TEST = ["0", "test"]


class Info:
    TEST = "ACU Test"


@tmtc_definitions_provider
def add_acu_cmds(defs: TmtcDefinitionWrapper):
    oce = OpCodeEntry()
    oce.add(
        keys=GomspaceOpCodes.REQUEST_CORE_HK_ONCE,
        info=GsInfo.REQUEST_CORE_HK_ONCE,
    )
    oce.add(
        keys=GomspaceOpCodes.REQUEST_AUX_HK_ONCE,
        info=GsInfo.REQUEST_AUX_HK_ONCE,
    )
    oce.add(
        keys=GomspaceOpCodes.REQUEST_AUX_HK_ONCE,
        info=GsInfo.REQUEST_AUX_HK_ONCE,
    )
    oce.add(
        keys=GomspaceOpCodes.GET_PARAM,
        info=GsInfo.GET_PARAMETER,
    )
    oce.add(
        keys=GomspaceOpCodes.SET_PARAM,
        info=GsInfo.SET_PARAMETER,
    )
    oce.add(keys=OpCodes.TEST, info=Info.TEST)
    defs.add_service(
        name=CustomServiceList.ACU.value,
        info="ACU Device",
        op_code_entry=oce,
    )


def pack_acu_commands(object_id: ObjectIdU32, q: DefaultPusQueueHelper, op_code: str):
    q.add_log_cmd("Handling ACU command")
    if op_code in GomspaceOpCodes.PRINT_SWITCH_V_I:
        q.add_log_cmd("ACU: Print channel stats")
        q.add_pus_tc(
            generate_action_command(
                object_id=object_id.as_bytes,
                action_id=gs.GomspaceDeviceActionIds.PRINT_SWITCH_V_I,
            )
        )
    if op_code in GomspaceOpCodes.REQUEST_CORE_HK_ONCE:
        q.add_log_cmd(f"PDU1: {GsInfo.REQUEST_CORE_HK_ONCE}")
        hk_sid = make_sid(object_id=object_id.as_bytes, set_id=gs.SetIds.ACU_CORE)
        q.add_pus_tc(generate_one_diag_command(sid=hk_sid))
    if op_code in GomspaceOpCodes.REQUEST_AUX_HK_ONCE:
        q.add_log_cmd(f"PDU1: {GsInfo.REQUEST_AUX_HK_ONCE}")
        hk_sid = make_sid(object_id=object_id.as_bytes, set_id=gs.SetIds.ACU_AUX)
        q.add_pus_tc(generate_one_hk_command(sid=hk_sid))
    if op_code in GomspaceOpCodes.GET_PARAM:
        q.add_log_cmd(f"PDU1: {GsInfo.GET_PARAMETER}")
        table_id = int(input("Specify table ID: "))
        memory_address = int(input("Specify memory address: 0x"), 16)
        parameter_size = int(input("Specify parameter size: "))
        q.add_pus_tc(
            gs.pack_get_param_command(
                object_id.as_bytes, table_id, memory_address, parameter_size
            )
        )
    if op_code in GomspaceOpCodes.SET_PARAM:
        q.add_log_cmd(f"PDU1: {GsInfo.SET_PARAMETER}")
        memory_address = int(input("Specify memory address: 0x"), 16)
        memory_address = struct.pack("!H", memory_address)
        parameter_size = int(input("Specify parameter size: "))
        parameter = int(input("Specify parameter: "))
        q.add_pus_tc(
            gs.pack_set_param_command(
                object_id.as_bytes, memory_address, parameter_size, parameter
            )
        )
    pack_test_cmds(object_id=object_id, q=q)


class ACUTestProcedure:
    """
    @brief  Use this class to define the tests to perform for the ACU.
    @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
    read_temperature1 = False
    read_temperature2 = False
    read_temperature3 = False
    read_mppt_mode = False
    read_vboost = False
    read_vbat_max_hi = False
    read_vbat_max_lo = False
    read_ov_mode = False
    off = False


def pack_test_cmds(object_id: ObjectIdU32, q: DefaultPusQueueHelper):
    if ACUTestProcedure.all or ACUTestProcedure.reboot:
        q.add_log_cmd("ACU: Reboot")
        q.add_pus_tc(gs.pack_reboot_command(object_id))
    if ACUTestProcedure.all or ACUTestProcedure.read_gnd_wdt:
        q.add_log_cmd("ACU: Reading ground watchdog timer value")
        q.add_pus_tc(
            gs.pack_get_param_command(
                object_id.as_bytes,
                gs.TableIds.hk,
                ACUHkTable.wdt_gnd_left.parameter_address,
                ACUHkTable.wdt_gnd_left.parameter_size,
            )
        )
    if ACUTestProcedure.all or ACUTestProcedure.gnd_wdt_reset:
        q.add_log_cmd("ACU: Testing ground watchdog reset")
        q.add_pus_tc(gs.pack_gnd_wdt_reset_command(object_id))
    if ACUTestProcedure.all or ACUTestProcedure.ping:
        q.add_log_cmd("ACU: Ping Test")
        ping_data = bytearray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        q.add_pus_tc(gs.pack_ping_command(object_id, ping_data))
    if ACUTestProcedure.all or ACUTestProcedure.read_temperature3:
        q.add_log_cmd("ACU: Reading temperature 3")
        q.add_pus_tc(
            gs.pack_get_param_command(
                object_id.as_bytes,
                gs.TableIds.hk,
                ACUHkTable.temperature3.parameter_address,
                ACUHkTable.temperature3.parameter_size,
            )
        )
    if ACUTestProcedure.all or ACUTestProcedure.read_vboost:
        q.add_log_cmd("ACU: Reading vboost value")
        q.add_pus_tc(
            gs.pack_get_param_command(
                object_id.as_bytes,
                gs.TableIds.config,
                ACUConfigTable.vboost.parameter_address,
                ACUConfigTable.vboost.parameter_size,
            )
        )
    if ACUTestProcedure.all or ACUTestProcedure.read_vbat_max_hi:
        q.add_log_cmd("ACU: Reading vbat_max_hi")
        q.add_pus_tc(
            gs.pack_get_param_command(
                object_id.as_bytes,
                gs.TableIds.config,
                ACUConfigTable.vbat_max_hi.parameter_address,
                ACUConfigTable.vbat_max_hi.parameter_size,
            )
        )
    if ACUTestProcedure.all or ACUTestProcedure.read_vbat_max_lo:
        q.add_log_cmd("ACU: Reading vbat_max_lo")
        q.add_pus_tc(
            gs.pack_get_param_command(
                object_id.as_bytes,
                gs.TableIds.config,
                ACUConfigTable.vbat_max_lo.parameter_address,
                ACUConfigTable.vbat_max_lo.parameter_size,
            )
        )
    if ACUTestProcedure.all or ACUTestProcedure.read_ov_mode:
        q.add_log_cmd("ACU: Reading ov_mode")
        q.add_pus_tc(
            gs.pack_get_param_command(
                object_id.as_bytes,
                gs.TableIds.config,
                ACUConfigTable.ov_mode.parameter_address,
                ACUConfigTable.ov_mode.parameter_size,
            )
        )
    if ACUTestProcedure.all or ACUTestProcedure.off:
        q.add_log_cmd("P60 Dock: Turning off ACU")
        q.add_pus_tc(
            gs.pack_set_param_command(
                ACU_HANDLER_ID,
                P60DockConfigTable.out_en_0.parameter_address,
                P60DockConfigTable.out_en_0.parameter_size,
                gs.Channel.off,
            )
        )