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

from eive_tmtc.config.definitions import CustomServiceList
from spacepackets.ecss.tc import PusTelecommand
from tmtccmd.config.tmtc import (
    tmtc_definitions_provider,
    OpCodeEntry,
    TmtcDefinitionWrapper,
)
from tmtccmd.tc import DefaultPusQueueHelper
from tmtccmd.tc.pus_200_fsfw_mode import create_mode_command, Mode
from tmtccmd.util import ObjectIdU32
from eive_tmtc.config.object_ids import CCSDS_HANDLER_ID


class ActionId(enum.IntEnum):
    # 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). Please note that this
    # is a legacy command. It is recommended to use mode commands instead
    EN_TRANSMITTER = 2
    # Disables the syrlinks transmitter (by using RS485 enables lines). Please note that this is
    # a legacy command. It is recommended to use mode commands instead.
    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


class Submode(enum.IntEnum):
    # Informative, do not command this.
    UNSET = 0
    DATARATE_LOW = 1
    DATARATE_HIGH = 2
    DATARATE_DEFAULT = 3


class OpCode:
    ENABLE_WITH_LOW_DATARATE = ["enable_low_datarate"]
    ENABLE_WITH_HIGH_DATARATE = ["enable_high_datarate"]
    DISABLE = ["disable"]
    ENABLE_ACTION = ["legacy_enable_tx"]
    DISABLE_ACTION = ["legacy_disable_tx"]


class Info:
    ENABLE_WITH_LOW_DATARATE = "Enable TX with low datarate"
    ENABLE_WITH_HIGH_DATARATE = "Enable TX with high datarate"
    DISABLE = "Disable TX"
    ENABLE_ACTION = "Enable TX (legacy)"
    DISABLE_ACTION = "Disable TX (legacy)"


def pack_ccsds_handler_test(
    object_id: ObjectIdU32, q: DefaultPusQueueHelper, op_code: str
):
    obyt = object_id.as_bytes
    prefix = "CCSDS Handler"
    q.add_log_cmd(f"Testing CCSDS handler with object id: {object_id.as_hex_string}")
    if op_code in OpCode.ENABLE_WITH_LOW_DATARATE:
        q.add_log_cmd(f"{prefix}: {Info.ENABLE_WITH_LOW_DATARATE}")
        q.add_pus_tc(create_mode_command(obyt, Mode.ON, Submode.DATARATE_LOW))
    if op_code in OpCode.ENABLE_WITH_HIGH_DATARATE:
        q.add_log_cmd(f"{prefix}: {Info.ENABLE_WITH_HIGH_DATARATE}")
        q.add_pus_tc(create_mode_command(obyt, Mode.ON, Submode.DATARATE_HIGH))
    if op_code in OpCode.DISABLE:
        q.add_log_cmd(f"{prefix}: {Info.DISABLE}")
        q.add_pus_tc(create_mode_command(obyt, Mode.OFF, 0))
    if op_code in OpCode.ENABLE_ACTION:
        q.add_log_cmd(f"{prefix}: {Info.ENABLE_ACTION}")
        command = obyt + struct.pack("!I", ActionId.EN_TRANSMITTER)
        q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))
    if op_code in OpCode.DISABLE_ACTION:
        q.add_log_cmd(f"{prefix}: {Info.DISABLE_ACTION}")
        command = obyt + struct.pack("!I", ActionId.DIS_TRANSMITTER)
        q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))
    if op_code == "4":
        q.add_log_cmd("CCSDS Handler: Set arbitrary bitrate")
        bitrate = int(input("Specify bit rate (bps): "))
        command = (
            obyt
            + struct.pack("!I", ActionId.ARBITRARY_BITRATE)
            + struct.pack("!I", bitrate)
        )
        q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))
    if op_code == "5":
        q.add_log_cmd("CCSDS Handler: Enable tx clock manipulator")
        command = obyt + struct.pack("!I", ActionId.ENABLE_TX_CLK_MANIPULATOR)
        q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))
    if op_code == "6":
        q.add_log_cmd("CCSDS Handler: Disable tx clock manipulator")
        command = obyt + struct.pack("!I", ActionId.DISABLE_TX_CLK_MANIPULATOR)
        q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))
    if op_code == "7":
        q.add_log_cmd("CCSDS Handler: Update tx data on rising edge of tx clock")
        command = obyt + struct.pack("!I", ActionId.UPDATE_ON_RISING_EDGE)
        q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))
    if op_code == "8":
        q.add_log_cmd("CCSDS Handler: Update tx data on falling edge of tx clock")
        command = obyt + struct.pack("!I", ActionId.UPDATE_ON_FALLING_EDGE)
        q.add_pus_tc(PusTelecommand(service=8, subservice=128, app_data=command))


@tmtc_definitions_provider
def add_ccsds_cmds(defs: TmtcDefinitionWrapper):
    oce = OpCodeEntry()
    oce.add(OpCode.ENABLE_WITH_LOW_DATARATE, Info.ENABLE_WITH_LOW_DATARATE)
    oce.add(OpCode.ENABLE_WITH_HIGH_DATARATE, Info.ENABLE_WITH_HIGH_DATARATE)
    oce.add(OpCode.DISABLE, Info.DISABLE)
    oce.add(OpCode.ENABLE_ACTION, Info.ENABLE_ACTION)
    oce.add(OpCode.DISABLE_ACTION, Info.DISABLE_ACTION)
    oce.add("4", "CCSDS Handler: Set arbitrary bitrate")
    oce.add("5", "CCSDS Handler: Enable tx clock manipulator")
    oce.add("6", "CCSDS Handler: Disable tx clock manipulator")
    oce.add("7", "CCSDS Handler: Update tx data on rising edge")
    oce.add("8", "CCSDS Handler: Update tx data on falling edge")
    defs.add_service(CustomServiceList.CCSDS_HANDLER.value, "CCSDS Handler", oce)