# -*- coding: utf-8 -*-
"""
@file   ccsds_handler.py
@brief  Test commanding of CCSDS Handler
@author J. Meier
@date   20.11.2021
"""
import struct

from tmtccmd.config.definitions import QueueCommands
from tmtccmd.tc.packer import TcQueueT
from spacepackets.ecss.tc import PusTelecommand


class CommandIds:
    # Configures input rate of syrlinks to 400 Khz (results in downlink rate of 200 kbps)
    SET_LOW_RATE = 0
    # Configures input rate of syrlinks to 2000 Khz (results in downlink rate of 1000 kbps)
    SET_HIGH_RATE = 1
    # Enables the syrlinks transmitter (by using RS485 enables lines)
    EN_TRANSMITTER = 2
    # Disables the syrlinks transmitter (by using RS485 enables lines)
    DIS_TRANSMITTER = 3
    # Sets an arbitrary bitrate. Normally only set low and set high rate commands should be
    # required
    ARBITRARY_BITRATE = 4
    ENABLE_TX_CLK_MANIPULATOR = 5
    DISABLE_TX_CLK_MANIPULATOR = 6
    # Tx data will be updated on rising edge of tx clock
    UPDATE_ON_RISING_EDGE = 7
    # Tx data will be updated on falling edge of tx clock
    UPDATE_ON_FALLING_EDGE = 8


def pack_ccsds_handler_test(
    object_id: bytearray, tc_queue: TcQueueT, op_code: str
) -> TcQueueT:
    tc_queue.appendleft(
        (
            QueueCommands.PRINT,
            "Testing CCSDS handler with object id: 0x" + object_id.hex(),
        )
    )
    if op_code == "0":
        tc_queue.appendleft((QueueCommands.PRINT, "CCSDS Handler: Set low rate"))
        command = object_id + struct.pack("!I", CommandIds.SET_LOW_RATE)
        command = PusTelecommand(service=8, subservice=128, ssc=20, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "1":
        tc_queue.appendleft((QueueCommands.PRINT, "CCSDS Handler: Set high rate"))
        command = object_id + struct.pack("!I", CommandIds.SET_HIGH_RATE)
        command = PusTelecommand(service=8, subservice=128, ssc=21, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "2":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "CCSDS Handler: Enables the transmitter")
        )
        command = object_id + struct.pack("!I", CommandIds.EN_TRANSMITTER)
        command = PusTelecommand(service=8, subservice=128, ssc=22, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "3":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "CCSDS Handler: Disables the transmitter")
        )
        command = object_id + struct.pack("!I", CommandIds.DIS_TRANSMITTER)
        command = PusTelecommand(service=8, subservice=128, ssc=23, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "4":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "CCSDS Handler: Set arbitrary bitrate")
        )
        bitrate = int(input("Specify bit rate (bps): "))
        command = (
            object_id
            + struct.pack("!I", CommandIds.ARBITRARY_BITRATE)
            + struct.pack("!I", bitrate)
        )
        command = PusTelecommand(service=8, subservice=128, ssc=24, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "5":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "CCSDS Handler: Enable tx clock manipulator")
        )
        command = object_id + struct.pack("!I", CommandIds.ENABLE_TX_CLK_MANIPULATOR)
        command = PusTelecommand(service=8, subservice=128, ssc=24, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "6":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "CCSDS Handler: Disable tx clock manipulator")
        )
        command = object_id + struct.pack("!I", CommandIds.DISABLE_TX_CLK_MANIPULATOR)
        command = PusTelecommand(service=8, subservice=128, ssc=24, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "7":
        tc_queue.appendleft(
            (
                QueueCommands.PRINT,
                "CCSDS Handler: Update tx data on rising edge of tx clock",
            )
        )
        command = object_id + struct.pack("!I", CommandIds.UPDATE_ON_RISING_EDGE)
        command = PusTelecommand(service=8, subservice=128, ssc=24, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "8":
        tc_queue.appendleft(
            (
                QueueCommands.PRINT,
                "CCSDS Handler: Update tx data on falling edge of tx clock",
            )
        )
        command = object_id + struct.pack("!I", CommandIds.UPDATE_ON_FALLING_EDGE)
        command = PusTelecommand(service=8, subservice=128, ssc=24, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())

    return tc_queue