from modbus import Modbus
from mb_registers import SysReg
from colorama import Fore
import time
import random
from datetime import datetime, timedelta, timezone

models = {0x100: 'MDIO_88', 0x200: 'MAO_4', 0x300: 'MAI_12'}
test_state = {0: 'No Test', 1: 'Test OK', 2: 'Test FAIL'}


class IO_SysParams:
    
    SYS_TEST_OK = 0x0001
    SAVE_SYS_SETTINGS = 0x0001
    SAVE_SETTIGNS = 0x0001
    
    def __init__(self, modbus: Modbus):
        self.modbus = modbus

        self.MODEL_MDIO_88 = 0x0100
        self.MODEL_MAO_8 = 0x0200
        self.MODEL_MAI_12 = 0x0300

        t = datetime.now(timezone.utc).astimezone()
        self.utc_offset = t.utcoffset() // timedelta(seconds=1)
        

    def get_system_vars(self):
        """Запрос системных настроек и параметров"""

        print(Fore.LIGHTMAGENTA_EX + "\nSystem settings and params:\n")

        # Модель
        model = self.modbus.read_holding_registers(SysReg.MODEL.value, 1)[0]
        print("Model:       ", Fore.LIGHTMAGENTA_EX + models[model])

        # Ревизия платы
        hw_rev = self.modbus.read_holding_registers(SysReg.HW_REV.value, 1)[0]
        print("HW revision: ", Fore.LIGHTMAGENTA_EX + str(hw_rev))

        # Дата производства    
        prod_date = self.modbus.read_uint32_holding(SysReg.PRODATE.value)
        t = time.gmtime(prod_date)
        print("Prod date:   ", Fore.LIGHTMAGENTA_EX + "{}.{}.{} {}:{}:{}".format(t.tm_mday, t.tm_mon, t.tm_year,
                                                      t.tm_hour, t.tm_min, t.tm_sec))

        # Серийный номер
        serial_number = self.modbus.read_uint32_holding(SysReg.SN.value)
        print("SN:          ", Fore.LIGHTMAGENTA_EX + str(serial_number))

        # Версия ПО
        fw_bytes = b''
        data = self.modbus.read_holding_registers(SysReg.FW_VER.value, 4)
        fw_raw = list(data)
        fw_raw.reverse()
        for i in fw_raw:
            fw_bytes += i.to_bytes(2, 'little')

        fw = fw_bytes.decode('utf-8')
        print("FW version:  ", Fore.LIGHTMAGENTA_EX + fw)

        # Статус тестирования
        status = self.modbus.read_holding_registers(SysReg.TEST_STATUS.value, 1)[0]
        print("Test status: ", Fore.LIGHTMAGENTA_EX + test_state[status])

        # Локальное время модуля
        t = time.ctime(self.get_rtc()/1000 - self.utc_offset)
        print("Local time : ", Fore.LIGHTMAGENTA_EX + t)

        # Пользовательская информация
        info_bytes = b''
        data = self.modbus.read_holding_registers(SysReg.INFO.value, 16)
        info_raw = list(data)
        info_raw.reverse()
        for i in info_raw:
            info_bytes += i.to_bytes(2, 'little')
        info = info_bytes.decode('utf-8')
        print("Info:        ", Fore.LIGHTMAGENTA_EX + info)


    def set_system_vars(self, password: int):
        """Установка системных настроек"""
        # Отправка пароля для разблокирования доступа к системным настройкам
        print("Send password...")
        self.modbus.write_holding_register(SysReg.PASSWORD.value, password)
        time.sleep(0.1)

        # Дата производства. На день назад от текущего момента.
        prod_time = int(time.time()) - time.timezone - 3600*24
        print("Set production date: ", prod_time)
        self.modbus.write_uint32(SysReg.PRODATE.value, prod_time)

        # Серийный номер
        sn = random.randint(10000, 1000000)
        print("Set SN: ", sn)
        self.modbus.write_uint32(SysReg.SN.value, sn)

        # Статус тестирования
        print("Set test status: Test OK")
        self.modbus.write_holding_register(SysReg.TEST_STATUS.value, self.SYS_TEST_OK)

        # Команда сохранения системных настроек
        print("Save system settings...")
        self.modbus.write_holding_register(SysReg.SAVE_SYS.value, self.SAVE_SYS_SETTINGS)

    def save_settings(self):
        self.modbus.write_holding_register(SysReg.COMMAND.value, self.SAVE_SETTIGNS)

    def get_uptime(self):
        return self.modbus.read_uint32_holding(SysReg.UPTIME.value)

    def get_save_mode(self):
        return self.modbus.read_holding_registers(SysReg.SAVE_MODE.value, 1)[0]

    def get_save_delay(self):
        return self.modbus.read_holding_registers(SysReg.SAVE_DELAY.value, 1)[0]

    def get_rtc(self):
        return self.modbus.read_uint64_holding(SysReg.RTC.value)

    def get_bat_votage(self):
        return self.modbus.read_holding_registers(SysReg.BAT_VOLTAGE.value, 1)[0]

    def get_module_state(self):
        '''Состояние модуля'''
        return self.modbus.read_uint32_holding(SysReg.ERR.value)



    def set_rtc(self):
        """Установить текущее время"""
        t = int((time.time() + self.utc_offset) * 1000)
        self.modbus.write_uint64(SysReg.RTC.value, t)

    def set_info(self, value: str):
        ascii_data = [ord(char) for char in value]
        if len(ascii_data) < 32:
            for _ in range(32 - len(ascii_data)):
                ascii_data += b'\0'
        ascii_data.reverse()
        self.modbus.write_holding_registers_raw(SysReg.INFO.value, bytes(ascii_data))

    def set_save_mode(self, value):
        if value in [0, 1]:
            self.modbus.write_holding_register(SysReg.SAVE_MODE.value, value)

    def set_save_delay(self, value):
        if value in range(5, 65536):
            self.modbus.write_holding_register(SysReg.SAVE_DELAY.value, value)

    def wdt_stop(self):
        """Программно отключает watchdog"""
        self.modbus.write_holding_register(SysReg.COMMAND.value, SysReg.WDT_STOP.value)


# def set_manager_register(dev: IO_Module, commands: list):
#     foo = 0
#     for command in commands:
#         foo |= command
#     print(foo)
    
#     # dev.write_holding_register(reg_table['param_manager'])


def main():
    pass

if __name__ == '__main__':
    main()