# copyright ############################### #
# This file is part of the Xboinc Package.  #
# Copyright (c) CERN, 2025.                 #
# ######################################### #

import sys

import xobjects as xo
import xboinc as xb

from ..general import __version__, __xsuite__versions__


# Check that the active environment has the correct pinned versions
def assert_versions():
    error = ""
    for mod in __xsuite__versions__.keys():
        __import__(mod)
        version = sys.modules[mod].__version__
        if version != __xsuite__versions__[mod]:
            error += f"This xboinc version ({__version__}) needs "
            error += f"{mod} {__xsuite__versions__[mod]}, "
            error +=  f"but active version is {version}!\n"
    if error != "":
        if xb._skip_xsuite_version_check:
            print(f"Warning: {error}")
        else:
            raise ImportError(error)


# XXX.YYY.ZZZ to XXXYYY  (ignore patch)
def _version_to_int(version):
    vers = version.split('.')
    return int(vers[0])*1000 + int(vers[1])

# XXXYYY to XXX.YYY
def _int_to_version(verint):
    XXX = int(verint / 1000)
    YYY = int(verint - XXX*1000)
    return f"{XXX}.{YYY}"


app_version = '.'.join(__version__.split('.')[:2])
app_version_int = _version_to_int(__version__)


# This class overloads the first field from the XbState,
# in order to read the correct version from the binary
class XbVersion(xo.Struct):
    xboinc_version = xo.Int64    # version XXX.YYY as int

    def __init__(self, **kwargs):
        """
        Parameters
        ----------
        None - autogenerated.
        """
        if '_xobject' not in kwargs:
            kwargs['xboinc_version'] = _version_to_int(__version__)
        super().__init__(**kwargs)

    def assert_version(self, raise_error=True, filename=None):
        if app_version_int != self.xboinc_version:
            filename = 'This file' if filename is None else f'File {filename}'
            error  = f"{filename} needs {_int_to_version(self.xboinc_version)}, "
            error += f"but current version is {__version__}.\n"
            if raise_error:
                raise ImportError(f"Incompatible xboinc version! {error}")
            else:
                print(f"Warning: {error}")
                return False
        return True
