# -*- coding: utf-8 -*-
"""
Program: obsw_module_test.py
Date: 01.11.2019
Description: PUS Custom Service 8: Device Access, Native low-level commanding

@author: R. Mueller
"""
import struct
from typing import Deque
from tc.obsw_pus_tc_base import PusTelecommand


def pack_service3_test_into(tc_queue: Deque) -> Deque:
    tc_queue.appendleft(("print", "Testing Service 3"))
    # adding custom defintion to hk using test pool variables
    sid1 = bytearray([0x00, 0x00, 0x43, 0x00])
    sid2 = bytearray([0x00, 0x00, 0x44, 0x00])
    sid_gps = bytearray([0x00, 0x00, 0x1f, 0x00])
    collection_interval = struct.pack('>f', 3)
    number_of_parameters = struct.pack('B', 5)
    p1 = bytearray([0x01, 0x01, 0x01, 0x01])
    p2 = bytearray([0x02, 0x02, 0x02, 0x02])
    p3 = bytearray([0x03, 0x03, 0x03, 0x03])
    p4 = bytearray([0x04, 0x04, 0x04, 0x04])
    p5 = bytearray([0x05, 0x05, 0x05, 0x05])
    hkDefinition1 = sid1 + collection_interval + number_of_parameters + p1 + p2 + p3 + p4 + p5
    collection_interval = struct.pack('>f', 6)
    hkDefinition2 = sid2 + collection_interval + number_of_parameters + p1 + p2 + p3 + p4 + p5

    # deleting pre-defined test entry
    tc_queue.appendleft(("print", "Testing Service 3: Deleting pre-defined HK definition"))
    command = PusTelecommand(service=3, subservice=3, ssc=3000, app_data=sid1)
    tc_queue.appendleft(command.pack_command_tuple())

    # adding pre-defined definition to hk using test pool variables
    tc_queue.appendleft(("print", "Testing Service 3: Adding pre-defined HK definition"))
    command = PusTelecommand(service=3, subservice=1, ssc=3010, app_data=hkDefinition1)
    tc_queue.appendleft(command.pack_command_tuple())

    # adding custom definition to diagnostics using test pool variables
    tc_queue.appendleft(("print", "Testing Service 3: Adding custom diganostics definition"))
    command = PusTelecommand(service=3, subservice=2, ssc=3020, app_data=hkDefinition2)
    tc_queue.appendleft(command.pack_command_tuple())

    # enable custom hk definition
    tc_queue.appendleft(("print", "Testing Service 3: Enable custom definition"))
    command = PusTelecommand(service=3, subservice=5, ssc=3030, app_data=sid1)
    tc_queue.appendleft(command.pack_command_tuple())
    # enable custom diag definition
    tc_queue.appendleft(("print", "Testing Service 3: Enable custom diagnostics definition"))
    command = PusTelecommand(service=3, subservice=7, ssc=3040, app_data=sid2)
    tc_queue.appendleft(command.pack_command_tuple())
    # enable gps0
    tc_queue.appendleft(("print", "Testing Service 3: Enable GPS definition"))
    command = PusTelecommand(service=3, subservice=5, ssc=3050, app_data=sid_gps)
    tc_queue.appendleft(command.pack_command_tuple())

    # maybe wait a bit to receive at least 2 packets..
    tc_queue.appendleft(("wait", 3))

    # Disable custom hk definition
    tc_queue.appendleft(("print", "Testing Service 3: Disable custom definition"))
    command = PusTelecommand(service=3, subservice=6, ssc=3060, app_data=sid1)
    tc_queue.appendleft(command.pack_command_tuple())
    # Disable custom diag definition
    tc_queue.appendleft(("print", "Testing Service 3: Disable custom diagnostics definition"))
    command = PusTelecommand(service=3, subservice=8, ssc=3070, app_data=sid2)
    tc_queue.appendleft(command.pack_command_tuple())
    # disable gps0
    tc_queue.appendleft(("print", "Testing Service 3: Disable GPS definition"))
    command = PusTelecommand(service=3, subservice=6, ssc=3080, app_data=sid_gps)
    tc_queue.appendleft(command.pack_command_tuple())
    # report custom Diag definition
    tc_queue.appendleft(("print", "Testing Service 3: Reporting diag definition"))
    command = PusTelecommand(service=3, subservice=11, ssc=3100, app_data=sid2)
    tc_queue.appendleft(command.pack_command_tuple())
    # report gps definition
    tc_queue.appendleft(("print", "Testing Service 3: Reporting GPS definition"))
    command = PusTelecommand(service=3, subservice=9, ssc=3110, app_data=sid_gps)
    tc_queue.appendleft(command.pack_command_tuple())
    # generate one custom hk definition
    tc_queue.appendleft(("print", "Testing Service 3: Generate one custom hk definition"))
    command = PusTelecommand(service=3, subservice=27, ssc=3120, app_data=sid1)
    tc_queue.appendleft(command.pack_command_tuple())
    # generate one custom diag definition
    tc_queue.appendleft(("print", "\r\nTesting Service 3: Generate one custom diagnostics definition"))
    command = PusTelecommand(service=3, subservice=28, ssc=3120, app_data=sid2)
    tc_queue.appendleft(command.pack_command_tuple())
    # generate one gps 0 definition
    tc_queue.appendleft(("print", "\r\nTesting Service 3: Generate one gps 0 defintion"))
    command = PusTelecommand(service=3, subservice=27, ssc=3120, app_data=sid_gps)
    tc_queue.appendleft(command.pack_command_tuple())
    # modify custom hk definition interval
    newInterval = struct.pack('>f', 10.0)
    newIntervalCommand = sid1 + newInterval
    tc_queue.appendleft(("print", "\r\nTesting Service 3: Changing pre-defined HK definition interval"))
    command = PusTelecommand(service=3, subservice=31, ssc=3090, app_data=newIntervalCommand)
    tc_queue.appendleft(command.pack_command_tuple())
    # report custom HK definition
    tc_queue.appendleft(("print", "\r\nTesting Service 3: Reporting pre-defined HK definition with changed interval"))
    command = PusTelecommand(service=3, subservice=9, ssc=3090, app_data=sid1)
    tc_queue.appendleft(command.pack_command_tuple())
    # modify custom diag definition interval
    newIntervalCommand = sid2 + newInterval
    tc_queue.appendleft(("print", "\r\nTesting Service 3: Changing custom diag HK definition interval"))
    command = PusTelecommand(service=3, subservice=32, ssc=3090, app_data=newIntervalCommand)
    tc_queue.appendleft(command.pack_command_tuple())
    # report custom diag definition
    tc_queue.appendleft(("print", "\r\nTesting Service 3: Reporting diag definition"))
    command = PusTelecommand(service=3, subservice=11, ssc=3100, app_data=sid2)
    tc_queue.appendleft(command.pack_command_tuple())
    # append parameter to custom hk definiton
    # append parameter to custom diag definition

    # delete custom diag definition
    tc_queue.appendleft(("print", "\r\nTesting Service 3: Deleting custom diagnostics definition"))
    command = PusTelecommand(service=3, subservice=4, ssc=3120, app_data=sid2)
    tc_queue.appendleft(command.pack_command_tuple())

    # do some basic testing on predefined structs too
    # e.g. add one variable, change interval, report them....
    tc_queue.appendleft(("export", "log/tmtc_log_service3.txt"))
    return tc_queue