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 PrintronixFirmwareInterpreter")

class PrintronixFirmwareInterpreter(FirmwareInterpreter):

    def GetMetadata(self, fileName, fileBytes):
        #models
        models = List[str]()
        version = ""
              
        # Decode the bytearray to an ASCII string
        fileString = bytes(fileBytes).decode('ascii', errors='ignore')

        #for printronix we need 11th character

        modelIdentifier = fileString[10]
        if(modelIdentifier == 'R'):
                #all t8000 series
                models.Add('T8204')
                models.Add('T8206')
                models.Add('T8208')
                models.Add('T8304')
                models.Add('T8306')
                models.Add('T8308')
        if(modelIdentifier == 'E'):
                models.Add('T4204')
                models.Add('T4304')
        if(modelIdentifier == 'J'):
                models.Add('T820')
                models.Add('T830')
        if(modelIdentifier == 'N'):
                models.Add('T6204')
                models.Add('T6206')
                models.Add('T6304')
                models.Add('T6306')
        if(modelIdentifier == 'T'):
                models.Add('T6204e')
                models.Add('T6206e')
                models.Add('T6304e')
                models.Add('T6306e')
                models.Add('T6604e')
        
        ##version
        pattern = r'(?<=P)\d+'

        match = re.search(pattern, fileName)
        if match:
            version = match.group()


        return FirmwareMetadata(version, models)

        
    def ValidateVersion(self, rawVersion):
        return rawVersion.isdigit() and len(rawVersion) == 6
    
    ##version 1 is device state property
    ##version 2 is firmware file version
    def CompareVersions(self, deviceStateVersionRaw, firmwareFileVersionRaw):
        deviceStateVersionRegex = r'(?<=#P)\d+'
        firmwareFileVersion = 0
        try:
            firmwareFileVersion = int(firmwareFileVersionRaw)
        except ValueError:
            print("Could not interpret firmware file version:", firmwareFileVersionRaw)
            raise

        deviceStateVersionMatch = re.search(deviceStateVersionRegex, deviceStateVersionRaw)
        if deviceStateVersionMatch:
              deviceStateVersion = int(deviceStateVersionMatch.group(0))
              if deviceStateVersion > firmwareFileVersion:
                    return 1
              elif firmwareFileVersion > deviceStateVersion:
                    return -1
              else:
                    return 0
        print("Could not interpret device state version:", deviceStateVersionRaw)

        raise ValueError('Could not interpret device state version')


plugin = PrintronixFirmwareInterpreter("PrintronixFirmwareInterpreter")

store.Add(plugin)