# -*- coding: utf-8 -*-
"""
@file   tmtcc_tc_heater.py
@brief  Command sequence to test the HeaterHandler
@author J. Meier
@date   30.01.2021
"""
from tmtccmd.config.definitions import QueueCommands
from tmtccmd.tc.packer import TcQueueT
from spacepackets.ecss.tc import PusTelecommand


class SwitchNumbers:
    HEATER_0 = 0
    HEATER_1 = 1
    HEATER_2 = 2
    HEATER_3 = 3
    HEATER_4 = 4
    HEATER_5 = 5
    HEATER_6 = 6
    HEATER_7 = 7
    NUMBER_OF_SWITCHES = 8


class ActionIds:
    SWITCH_HEATER = bytearray([0x0, 0x0, 0x0, 0x0])


def pack_heater_test_into(object_id: bytearray, tc_queue: TcQueueT):
    tc_queue.appendleft((QueueCommands.PRINT, "Testing Heater Switching"))

    heater_number = int(input("Type number of heater to switch [0-7]: "))
    if heater_number >= SwitchNumbers.NUMBER_OF_SWITCHES or heater_number < 0:
        print("Invalid heater switch number")
        return
    action = int(input("Turn switch on or off? (0 - off, 1 - on): "))
    if action != 0 and action != 1:
        print("Invalid action defined. Must be 0 (off) or 1 (on")
    debug_string = "Switching heater " + str(heater_number)
    tc_queue.appendleft((QueueCommands.PRINT, debug_string))
    command = pack_switch_heater_command(object_id, heater_number, action)
    command = PusTelecommand(service=8, subservice=128, ssc=300, app_data=command)
    tc_queue.appendleft(command.pack_command_tuple())


def pack_switch_heater_command(
    object_id: bytearray, switch_nr: int, switch_action: int
) -> bytearray:
    """Function to generate the command switch a heater
    @param object_id    The object id of the HeaterHandler object.
    @param switch_nr    The switch number identifying the heater to switch
    @param switch_action    Action to perform. 0 - Sets switch off, 1 - Sets switch on.
    """
    action_id = ActionIds.SWITCH_HEATER
    command = bytearray()
    command += object_id + action_id
    command.append(switch_nr)
    command.append(switch_action)
    return command