import clr
clr.AddReference('Soti.Connect.SemanticEngine.Conversions')
from System.Collections.Generic import List

from Soti.Connect.SemanticEngine.Conversions.Interpreters import FirmwareInterpreter
from Soti.Connect.SemanticEngine.Conversions.Interpreters import FirmwareMetadata
import re

print("Loading TSCFirmwareInterpreter")

class TSCFirmwareInterpreter(FirmwareInterpreter):

    def GetMetadata(self, fileName, fileBytes):
        #models
        models = List[str]()
        version = ""
        return FirmwareMetadata(version, models)

    def ValidateVersion(self, rawVersion):
        versionPattern = r'^[A-Za-z]\d+\.\d{2}\.\d{3}$'
        version_match = re.search(versionPattern, rawVersion)
        if version_match:
            return True
        return False
    
    ##version 1 is device state property
    ##version 2 is firmware file version
    def CompareVersions(self, deviceStateVersionRaw, firmwareFileVersionRaw):
        deviceStateVersion = deviceStateVersionRaw.replace('.', '')
        firmwareFileVersion = firmwareFileVersionRaw.replace('.', '')

        if deviceStateVersion[0] > firmwareFileVersion[0]:
            return 1
        elif firmwareFileVersion[0] > deviceStateVersion[0]:
            return -1
        else:
            deviceStateVersionInt = int(deviceStateVersion.replace(deviceStateVersion[0], ''))
            firmwareFileVersionInt = int(firmwareFileVersion.replace(firmwareFileVersion[0], ''))
            
            if deviceStateVersionInt > firmwareFileVersionInt:
                return 1
            elif firmwareFileVersionInt > deviceStateVersionInt:
                return -1
            else:
                 return 0


plugin = TSCFirmwareInterpreter("TSCFirmwareInterpreter")

store.Add(plugin)