##
# @module    unit_info
# @brief     Unit Class that can be used to help identify device and capabilities
# @version   1.1
##

from vts import unit_info
import gnss
import netw

class Unit:
    info = unit_info()
    engine_info = gnss.get_engine_info()
    device = 'PB' if ('1118' in info['PCB Info']) else 'VB'
    FW_Major, FW_Minor, FW_Revision, FW_Build = (int(x) for x in info['Firmware Version'].split('.'))
    network_hardware_info = netw.get_hwmodule_info()

    @classmethod
    def update_unit_info(cls):
        def get_gnss_info_field(info, field):
            for value in info.values():
                if str(value).startswith(field):
                    return value[len(field):]

        def get_gnss_info_fw(info):
            fw = "Not Searched"
            try:
                fw = get_gnss_info_field(info, 'FWVER=')
                if (fw is None) or (not (fw.startswith('HPG ') or fw.startswith('SPG '))):
                    raise Exception('unknown fw format')
                return float(fw[4:])
            except Exception as e:
                print("ENGINE VERSION FAILED: ", fw, ' - ', e)
                return 0.0

        cls.engine_info['Firmware Version'] = get_gnss_info_fw(cls.engine_info)

    @classmethod
    def is_pbox(cls):
        return True if (cls.device == 'PB') else False

    @classmethod
    def is_vbox(cls):
        return True if (cls.device == 'VB') else False

    @classmethod
    def supports_RTK(cls):
        return cls.gnss_is_uBloxF9P()

    @classmethod
    def gnss_is_uBloxF9P(cls):
        return True if (cls.info['GNSS Engine'] == 'UBLOX ZED-F9P') else False

    @classmethod
    def gnss_is_uBloxM9N(cls):
        return True if (cls.info['GNSS Engine'] == 'UBLOX NEO-M9N') else False

    @classmethod
    def gnss_is_uBloxM8Q(cls):
        return True if (cls.info['GNSS Engine'] == 'UBLOX NEO-M8Q') else False

    @classmethod
    def gnss_is_uBloxM8L(cls):
        return True if (cls.info['GNSS Engine'] == 'UBLOX NEO-M8L') else False

    @classmethod
    def gnss_supports_beidou(cls):
        if (cls.gnss_is_uBloxF9P() and cls.engine_info['Firmware Version'] >= 1.13):
            return True
        if (cls.gnss_is_uBloxM9N() and cls.engine_info['Firmware Version'] >= 1.14):
            return True
        return False

    @classmethod
    def gnss_supports_dgnss(cls):
        return cls.gnss_is_uBloxF9P()

    @classmethod
    def supports_hp_sample(cls):
        return True if ((cls.FW_Revision >= 2) and (cls.FW_Build > 21)) else False

    @classmethod
    def is_ublox(cls):
        return True if cls.network_hardware_info.mfr.replace('"', '') == 'u-blox' else False

    @classmethod
    def is_wifi_capable(cls):
        if cls.is_ublox():
            if cls.network_hardware_info.model.replace('"', '') == 'NINA-B2':
                return False
            else:
                return True
        else:
            return False

    @classmethod
    def is_MQTT_capable(cls):
        if cls.is_wifi_capable():
            if cls.network_hardware_info.model.replace('"', '') == 'ODIN-W2':
                if (int(cls.network_hardware_info.version.replace('"', '')[0]) > 6):
                    return True
                else:
                    return False
            else:
                return True
        else:
            return False

    @classmethod
    def name(cls):
        pass

Unit.update_unit_info()