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

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

print("Loading AveryDennisonFirmwareInterpreter")

class AveryDennisonFirmwareInterpreter(FirmwareInterpreter):

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

        
    def ValidateVersion(self, rawVersion, *args):
        versionPattern = r'^\d+\.\d+\.\d+\.\d+\.\d+$'
        version_match = re.search(versionPattern, rawVersion)
        if version_match:
            return True
        return False
    
    def CompareVersions(self, deviceStateVersionRaw, firmwareFileVersionRaw):
        versionPattern = r'^\d+(\.\d+)*$'
        deviecStateVersionMatch = re.search(versionPattern, deviceStateVersionRaw)
        if not deviecStateVersionMatch:
            raise ValueError('Could not interpret device state version') 
        firmwareFileVersionMatch = re.search(versionPattern, firmwareFileVersionRaw)
        if not firmwareFileVersionMatch:
            raise ValueError('Could not interpret firmware file version') 

        if deviecStateVersionMatch.group() > firmwareFileVersionMatch.group():
            return 1
        elif firmwareFileVersionMatch.group() > deviecStateVersionMatch.group():
            return -1
        else:
            return 0
        
plugin = AveryDennisonFirmwareInterpreter("AveryDennisonFirmwareInterpreter")

store.Add(plugin)