
from pathlib import Path
from ctypes import CDLL, c_double, c_void_p

from ..lib.logger import logger


diffusion_lib = None
try:
    diffusion_lib = CDLL(str(Path(__file__).parents[1] / 'clib' / 'diffusion.so'))

    diffusion_lib.anistropicDiffusion.restype = c_double
    diffusion_lib.anistropicDiffusion.argtypes = (c_double, c_void_p)

    HAS_C_FUNCS = True
except OSError:
    HAS_C_FUNCS = False

lib = None
try:
    lib = CDLL(str(Path(__file__).parents[1] / 'clib' / 'integrate.so'))

    # FFHS integrand
    lib.ffhsSD.restype = c_double
    lib.ffhsSD.argtypes = (c_double, c_void_p)

    # Log-Gaussian integrands
    lib.logGaussian_imag_high.restype = c_double
    lib.logGaussian_imag_high.argtypes = (c_double, c_void_p)
    lib.logGaussian_imag_low.restype = c_double
    lib.logGaussian_imag_low.argtypes = (c_double, c_void_p)

    lib.logGaussian_real_high.restype = c_double
    lib.logGaussian_real_high.argtypes = (c_double, c_void_p)
    lib.logGaussian_real_low.restype = c_double
    lib.logGaussian_real_low.argtypes = (c_double, c_void_p)

    lib.logGaussianCorrelation.restype = c_double
    lib.logGaussianCorrelation.argtypes = (c_double, c_void_p)

    # integrands for distribution of energies
    lib.energyDist_SD.restype = c_double
    lib.energyDist_SD.argtypes = (c_double, c_void_p)

    lib.energyDistCorrelation.restype = c_double
    lib.energyDistCorrelation.argtypes = (c_double, c_void_p)

    lib.energyDistSuscReal.restype = c_double
    lib.energyDistSuscReal.argtypes = (c_double, c_void_p)
    lib.energyDistSuscImag.restype = c_double
    lib.energyDistSuscImag.argtypes = (c_double, c_void_p)

    HAS_C_FUNCS = True
    logger.info('Use C functions')
except OSError:
    HAS_C_FUNCS = False
    logger.info('Use python functions')
