# -*- coding: utf-8 -*-
"""
@file   tmtcc_tc_acu.py
@brief  ACU tests
@author J. Meier
@date   21.12.2020
"""
import struct
from tmtccmd.tc.packer import TcQueueT
from tmtccmd.config.definitions import QueueCommands
from gomspace.gomspace_common import *
from pus_tc.p60dock import P60DockConfigTable

from config.object_ids import P60_DOCK_HANDLER


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 = True
    read_mppt_mode = True
    read_vboost = True
    read_vbat_max_hi = True
    read_vbat_max_lo = True
    read_ov_mode = True


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


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


class CommandId:
    PRINT_CHANNEL_STATS = 51


def pack_acu_test_into(
    object_id: bytearray, tc_queue: TcQueueT, op_code: str
) -> TcQueueT:
    tc_queue.appendleft((QueueCommands.PRINT, "Testing ACU"))

    if op_code == "51":
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Print channel stats"))
        command = object_id + struct.pack("!I", CommandId.PRINT_CHANNEL_STATS)
        command = PusTelecommand(service=8, subservice=128, ssc=30, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
        return

    if ACUTestProcedure.all or ACUTestProcedure.reboot:
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Reboot"))
        command = pack_reboot_command(object_id)
        # command = PusTelecommand(service=8, subservice=128, ssc=22, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if ACUTestProcedure.all or ACUTestProcedure.read_gnd_wdt:
        tc_queue.appendleft(
            (QueueCommands.PRINT, "ACU: Reading ground watchdog timer value")
        )
        command = pack_get_param_command(
            object_id,
            TableIds.hk,
            ACUHkTable.wdt_gnd_left.parameter_address,
            ACUHkTable.wdt_gnd_left.parameter_size,
        )
        # command = PusTelecommand(service=8, subservice=128, ssc=20, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if ACUTestProcedure.all or ACUTestProcedure.gnd_wdt_reset:
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Testing ground watchdog reset"))
        command = pack_gnd_wdt_reset_command(object_id)
        # command = PusTelecommand(service=8, subservice=128, ssc=21, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if ACUTestProcedure.all or ACUTestProcedure.ping:
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Ping Test"))
        ping_data = bytearray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        command = pack_ping_command(object_id, ping_data)
        # command = PusTelecommand(service=8, subservice=128, ssc=22, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if ACUTestProcedure.all or ACUTestProcedure.read_temperature3:
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Reading temperature 3"))
        command = pack_get_param_command(
            object_id,
            TableIds.hk,
            ACUHkTable.temperature3.parameter_address,
            ACUHkTable.temperature3.parameter_size,
        )
        # command = PusTelecommand(service=8, subservice=128, ssc=23, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if ACUTestProcedure.all or ACUTestProcedure.read_vboost:
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Reading vboost value"))
        command = pack_get_param_command(
            object_id,
            TableIds.config,
            ACUConfigTable.vboost.parameter_address,
            ACUConfigTable.vboost.parameter_size,
        )
        # command = PusTelecommand(service=8, subservice=128, ssc=23, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if ACUTestProcedure.all or ACUTestProcedure.read_vbat_max_hi:
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Reading vbat_max_hi"))
        command = pack_get_param_command(
            object_id,
            TableIds.config,
            ACUConfigTable.vbat_max_hi.parameter_address,
            ACUConfigTable.vbat_max_hi.parameter_size,
        )
        # command = PusTelecommand(service=8, subservice=128, ssc=23, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if ACUTestProcedure.all or ACUTestProcedure.read_vbat_max_lo:
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Reading vbat_max_lo"))
        command = pack_get_param_command(
            object_id,
            TableIds.config,
            ACUConfigTable.vbat_max_lo.parameter_address,
            ACUConfigTable.vbat_max_lo.parameter_size,
        )
        # command = PusTelecommand(service=8, subservice=128, ssc=23, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if ACUTestProcedure.all or ACUTestProcedure.read_ov_mode:
        tc_queue.appendleft((QueueCommands.PRINT, "ACU: Reading ov_mode"))
        command = pack_get_param_command(
            object_id,
            TableIds.config,
            ACUConfigTable.ov_mode.parameter_address,
            ACUConfigTable.ov_mode.parameter_size,
        )
        # command = PusTelecommand(service=8, subservice=128, ssc=23, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())

    tc_queue.appendleft((QueueCommands.PRINT, "P60 Dock: Turning off ACU"))
    command = pack_set_param_command(
        p60dock_object_id,
        P60DockConfigTable.out_en_0.parameter_address,
        P60DockConfigTable.out_en_0.parameter_size,
        Channel.off,
    )
    # command = PusTelecommand(service=8, subservice=128, ssc=23, app_data=command)
    tc_queue.appendleft(command.pack_command_tuple())

    return tc_queue