import clr
clr.AddReference('Soti.Connect.SemanticEngine.Conversions')

from Soti.Connect.SemanticEngine.Conversions.Interpreters import FirmwareInterpreter
from Soti.Connect.SemanticEngine.Conversions.Interpreters import FirmwareMetadata
from System.Collections.Generic import List

print("Loading HikvisionFirmwareInterpreter")

class HikvisionFirmwareInterpreter(FirmwareInterpreter):

    def GetMetadata(self, fileName, fileBytes):
        # no known info to read from file
        return FirmwareMetadata('', List[str]())

    def ValidateVersion(self, rawVersion):
        if not rawVersion[0] == "V":
            return False

        content = rawVersion[1:-1] # remove first and last char
        parts = content.split(".") # split by . 

        # check to make sure everything is a digit
        for part in parts:
            if not part.isdigit():
                return False

        return True

    def CompareVersions(self, deviceStateVersionRaw, firmwareFileVersionRaw):
        device = deviceStateVersionRaw[1:]
        firmware = firmwareFileVersionRaw[1:]

        d_major, d_minor, d_patch = device.split('.')
        f_major, f_minor, f_patch = firmware.split('.')

        d_major, d_minor, d_patch = int(d_major), int(d_minor), int(d_patch)
        f_major, f_minor, f_patch = int(f_major), int(f_minor), int(f_patch)

        if d_major > f_major:
            return 1
        if d_major < f_major:
            return -1

        if d_minor > f_minor:
            return 1
        if d_minor < f_minor:
            return -1

        if d_patch > f_patch:
            return 1
        if d_patch < f_patch:
            return -1

        return 0


plugin = HikvisionFirmwareInterpreter("HikvisionFirmwareInterpreter")

store.Add(plugin)