
import gui
from os import listdir
import ustruct

import PY_VTS.gui.colours as Colour
from PY_VTS.srn.base  import *
from PY_VTS.mgr.screens import *
from PY_VTS.mgr.CAN import *
from PY_VTS.mgr.Files import *


class Screen_INFO(Screen_Base):
    def __init__(self):
        super().__init__()

        super().set_title("Info")

        super().add_swipe_cb(self.swipe_right, "right")

        MgrCAN.signals["Angle Info"].callback = self.translate_sensor_info

        self.board_id = 0
        self.major_v = 0
        self.minor_v = 0
        self.baud_rate = 0
        self.encoder_res = 0
        self.amplification = 0
        self.offset = 0
        self.filtering = 0
        self.ad_board = 0
        self.frequency = 0

        self.board_id_l =       [gui.CTRL_TEXT,  250, 100, 29, 0, '']
        self.version_l =        [gui.CTRL_TEXT,  250, 160, 29, 0, '']
        self.baud_rate_l =      [gui.CTRL_TEXT,  250, 220, 29, 0, '']
        self.encoder_res_l =    [gui.CTRL_TEXT,  250, 280, 29, 0, '']
        self.frequency_l =      [gui.CTRL_TEXT,  250, 340, 29, 0, '']
        self.amplification_l =  [gui.CTRL_TEXT,  650, 100, 29, 0, '']
        self.offset_l =         [gui.CTRL_TEXT,  650, 160, 29, 0, '']
        self.filtering_l =      [gui.CTRL_TEXT,  650, 220, 29, 0, '']
        self.ad_board_l =       [gui.CTRL_TEXT,  650, 280, 29, 0, '']

        self.board_id_label_l =     [gui.CTRL_TEXT,  10, 100, 29, 0, 'Board ID']
        self.version_label_l =      [gui.CTRL_TEXT,  10, 160, 29, 0, 'Version']
        self.baud_rate_label_l =    [gui.CTRL_TEXT,  10, 220, 29, 0, 'Baud Rate']
        self.encoder_res_label_l = [gui.CTRL_TEXT,  10, 280, 29, 0, 'Encoder Res']
        self.frequency_label_l =    [gui.CTRL_TEXT,  10, 330, 29, 0, 'Frequency']
        self.frequency_label2_l =    [gui.CTRL_TEXT,  10, 355, 29, 0, 'Reported / Actual']
        self.amplification_label_l =[gui.CTRL_TEXT,  410, 100, 29, 0, 'Amplification']
        self.offset_label_l =       [gui.CTRL_TEXT,  410, 160, 29, 0, 'Offset']
        self.filtering_label_l =    [gui.CTRL_TEXT,  410, 220, 29, 0, 'Filtering']
        self.ad_board_label_l =     [gui.CTRL_TEXT,  410, 280, 29, 0, 'Ad Board']

        self.frequency_enum = ["500/470",
            "200/242",
            "100/123",
            "60/62",
            "50/50",
            "40/39",
            "30/33.2",
            "20/19.6",
            "15/16.7",
            "15/16.7",
            "12/12.5",
            "10/10",
            "8/8.33",
            "6/6.25",
            "5/4.17"
        ]

        self.ad_board_enum = ["Normal Mode", "Calibration Mode"]
        self.filter_enum = ["No Filter", "Quadratic Average", "5 Times Average", "10 Times Average", "Kalman Filter"]



        self.gui_l.extend([
            [gui.CTRL_FLATBUTTON, 525, 330, 150, 40, 29, "Refresh", self.reset_info],

            [Colour.BLACK],
            self.board_id_l,
            self.version_l,
            self.baud_rate_l,
            self.encoder_res_l,
            self.frequency_label2_l,
            self.frequency_l,
            self.amplification_l,
            self.offset_l,
            self.filtering_l,
            self.ad_board_l,

            self.board_id_label_l,
            self.version_label_l,
            self.baud_rate_label_l,
            self.encoder_res_label_l,
            self.frequency_label_l,
            self.amplification_label_l,
            self.offset_label_l,
            self.filtering_label_l,
            self.ad_board_label_l,
        ])

    def vsync(self, list):
        self.board_id_l[5] = "{}".format(self.board_id)
        self.version_l[5] = "{}.{}".format(self.major_v, self.minor_v)
        self.baud_rate_l[5] = "{}".format(self.baud_rate)
        self.encoder_res_l[5] = "{}".format(self.encoder_res)
        self.frequency_l[5] = "{} Hz".format(self.frequency)
        self.amplification_l[5] = "{:.6}".format(self.amplification)
        self.offset_l[5] = "{:.6}".format(self.offset)
        self.filtering_l[5] = "{}".format(self.filtering)
        self.ad_board_l[5] = "{}".format(self.ad_board)


    def swipe_right(self):
        Mgr_Screens.Set_Screen("Wheel")

    def reset_info(self, btn):
        MgrCAN.transmit(0x09, [ 0x78, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 3)

    def translate_sensor_info(self, signal):
        super().add_vsync_cb(self.vsync, "info")
        msg = signal.data
        msg_b = ustruct.pack("BBBBBBBB",    signal.data >> 56,
                                            signal.data >> 48,
                                            signal.data >> 40,
                                            signal.data >> 32,
                                            signal.data >> 24,
                                            signal.data >> 16,
                                            signal.data >> 8,
                                            signal.data >> 0,
                                            )
        if msg_b[0] == 0x01:
            tup = ustruct.unpack("<BIBBB",msg_b)
            self.board_id = tup[1]
            self.major_v = tup[2]
            self.minor_v = tup[3]
        elif msg_b[0] == 0x02:
            tup = ustruct.unpack("<BBHf",msg_b)
            self.baud_rate = tup[1]
            self.encoder_res = tup[2]
            self.amplification = tup[3]
        elif msg_b[0] == 0x03:
            tup = ustruct.unpack("<BfBBB",msg_b)
            self.offset = tup[1]
            self.filtering = self.filter_enum[tup[2] - 1]
            self.ad_board = self.ad_board_enum[(tup[4] >> 4)]
            self.frequency = self.frequency_enum[(tup[4] & 0x0F) - 1]
        else:
            print("unidentified mux")
