# -*- coding: utf-8 -*-
"""
@file   ploc_udpater.py
@brief  Commands to initiate update transfer to ploc supervisor. This only updates the software of the MPSoC, it is not
        possible to update the software of the supervisor.
        The supervisor is programmed by Thales.
@author J. Meier
@date   10.07.2021
"""
import struct

from tmtccmd.config.definitions import QueueCommands

from tmtccmd.tc.packer import TcQueueT
from spacepackets.ecss.tc import PusTelecommand


latchup_id_dict = {
    "0": "0.85V",
    "1": "1.8V",
    "2": "MISC",
    "3": "3.3V",
    "4": "NVM_4XO",
    "5": "MISSION",
    "6": "SAFECOTS",
}


class UpdaterActionIds:
    UPDATE_A_UBOOT = 0
    UPDATE_A_BITSTREAM = 1
    UPDATE_A_LINUX = 2
    UPDATE_A_APP_SW = 3
    UPDATE_B_UBOOT = 4
    UPDATE_B_BITSTREAM = 5
    UPDATE_B_LINUX = 6
    UPDATE_B_LINUX = 7


class ImagePathDefs:
    imageAuboot = "/mnt/sd0/ploc/updateAuboot.bin"
    imageAbitsream = "/mnt/sd0/ploc/updateAbitstream.bin"
    imageAlinux = "/mnt/sd0/ploc/updateAlinux.bin"
    imageAappsw = "/mnt/sd0/ploc/updateAappsw.bin"
    imageBuboot = "/mnt/sd0/ploc/updateBuboot.bin"
    imageBbitsream = "/mnt/sd0/ploc/updateBbitstream.bin"
    imageBlinux = "/mnt/sd0/ploc/updateBlinux.bin"
    imageBappsw = "/mnt/sd0/ploc/updateBappsw.bin"


def pack_ploc_updater_test_into(
    object_id: bytearray, tc_queue: TcQueueT, op_code: str
) -> TcQueueT:
    tc_queue.appendleft(
        (
            QueueCommands.PRINT,
            "Testing PLOC updater with object id: 0x" + object_id.hex(),
        )
    )

    if op_code == "0":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "PLOC Supervisor: Update uboot on partition A")
        )
        command = (
            object_id
            + struct.pack("!I", UpdaterActionIds.UPDATE_A_UBOOT)
            + bytearray(ImagePathDefs.imageAuboot, "utf-8")
        )
        command = PusTelecommand(service=8, subservice=128, ssc=30, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "1":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "PLOC Supervisor: Update bitstream on parition A")
        )
        command = (
            object_id
            + struct.pack("!I", UpdaterActionIds.UPDATE_A_BITSTREAM)
            + bytearray(ImagePathDefs.imageAbitsream, "utf-8")
        )
        command = PusTelecommand(service=8, subservice=128, ssc=31, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "2":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "PLOC Supervisor: Update linux on partition A")
        )
        command = (
            object_id
            + struct.pack("!I", UpdaterActionIds.UPDATE_A_LINUX)
            + bytearray(ImagePathDefs.imageAlinux, "utf-8")
        )
        command = PusTelecommand(service=8, subservice=128, ssc=32, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "3":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "PLOC Supervisor: Update application on partition A")
        )
        command = (
            object_id
            + struct.pack("!I", UpdaterActionIds.UPDATE_A_APP_SW)
            + bytearray(ImagePathDefs.imageAappsw, "utf-8")
        )
        command = PusTelecommand(service=8, subservice=128, ssc=33, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "4":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "PLOC Supervisor: Update uboot on partition B")
        )
        command = (
            object_id
            + struct.pack("!I", UpdaterActionIds.UPDATE_B_UBOOT)
            + bytearray(ImagePathDefs.imageBuboot, "utf-8")
        )
        command = PusTelecommand(service=8, subservice=128, ssc=30, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "5":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "PLOC Supervisor: Update bitstream on parition B")
        )
        command = (
            object_id
            + struct.pack("!I", UpdaterActionIds.UPDATE_B_BITSTREAM)
            + bytearray(ImagePathDefs.imageBbitsream, "utf-8")
        )
        command = PusTelecommand(service=8, subservice=128, ssc=31, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "6":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "PLOC Supervisor: Update linux on partition B")
        )
        command = (
            object_id
            + struct.pack("!I", UpdaterActionIds.UPDATE_B_LINUX)
            + bytearray(ImagePathDefs.imageBlinux, "utf-8")
        )
        command = PusTelecommand(service=8, subservice=128, ssc=32, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())
    if op_code == "7":
        tc_queue.appendleft(
            (QueueCommands.PRINT, "PLOC Supervisor: Update application on partition B")
        )
        command = (
            object_id
            + struct.pack("!I", UpdaterActionIds.UPDATE_B_APP_SW)
            + bytearray(ImagePathDefs.imageBappsw, "utf-8")
        )
        command = PusTelecommand(service=8, subservice=128, ssc=33, app_data=command)
        tc_queue.appendleft(command.pack_command_tuple())