#!/usr/bin/env python

#This file imports the members defined in cellParams.h into a separated python module
# (from the "flat" module CLICopti.CLICopti generated by SWIG)

#The main struct used to hold CellParams
from CLICopti.CLICopti import CellParams

#The all the size_t offsets into the struct,
# used to dynamically specify which member should be used
from CLICopti.CLICopti import\
    off_h, off_a, off_d_n, off_a_n, off_f0, off_psi, \
    off_Q, off_vg, off_rQ, off_Es, off_Hs, off_Sc, \
    off_f1mn, off_Q1mn, off_A1mn

#Helper function to access the struct members by offset
#from CLICopti.CLICopti import getByOffset

#Array of offsets, used for e.g. creating databases
from CLICopti.CLICopti import size_t_array
size_t_array.__doc__ = \
"""Array of offsets (created via SWIG).
Used as argument to e.g. CellBase C++ constructors."""

cellOffsets = {\
    "h"   : off_h,\
    "a"   : off_a, \
    "a_n" : off_a_n,\
    "d_n" : off_d_n,\
    "f0"  : off_f0,\
    "psi" : off_psi,\
    "Q"   : off_Q,\
    "vg"  : off_vg,\
    "rQ"  : off_rQ,\
    "Es"  : off_Es,\
    "Hs"  : off_Hs,\
    "Sc"  : off_Sc,\
    "f1mn": off_f1mn,\
    "Q1mn": off_Q1mn,\
    "A1mn": off_A1mn\
    }

def getOffsetArray_byString(varNameList : list) -> tuple[size_t_array,int]:
    """
    Convert a lists of offsets to a size_t_array used by some C++ CellBase constructors.

    Parameters
    ----------

    varNameList : list
        A list of variable names to convert to offsets, as specified in
        CLICopti.CellParams.cellOffsets

    Returns
    -------

    offsets : size_t_array
        The offsets, a size_t_array of integers converted from CLICopti.CellParams.cellOffsets
    numOffsets : int
        The length of "offsets"

    Example
    -------
    
    >>> (offsets, numOffsets) = CLICopti.CellParams.getOffsetArray_byString(["h","a","psi"])
    >>> for i in range(numOffsets):
    >>>     print(offsets[i])
    0
    8
    40

    """
    numOffsets = len(varNameList)
    offsets = size_t_array(numOffsets)
    
    for v,i in zip(varNameList, range(len(varNameList))):
        offsets[i] = cellOffsets[v]
    
    return offsets,numOffsets

#Copy constructor (sort of)
from CLICopti.CLICopti import CellParams_copy

#Cell scaling function
from CLICopti.CLICopti import scaleCell

_scaleCell_CPP = scaleCell
def _scaleCell_PY(c,f0_new,scalingLevel=3):
    #Pythonicization: Create a a new cell!
    cc=CellParams_copy(c)
    _scaleCell_CPP(cc, f0_new, scalingLevel)
    return cc
scaleCell = _scaleCell_PY
