# Copyright 2024 University of Liège
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Matching interpolator class
# Paul Dechamps

from blast.interfaces.interpolators.blInterpolator import Interpolator

class MatchingInterpolator(Interpolator):
    """
    Matching Interpolator for inviscid and viscous data.

    Attributes:
    ----------
    _sections : list
        List of boundary layers sections.
    """
    def __init__(self, ndim, **kwargs):
        """
        Initialize the MatchingInterpolator.

        Parameters:
        ----------
        ndim : int
            Number of dimensions.
        kwargs : dict
            Optional arguments.

        Optional arguments:
        -------------------
        sections : list
            List of sections for 3D cases.
        """
        super().__init__(ndim)
        self._sections = kwargs.get('sections')
        if self.ndim == 2 and len(self._sections) > 1:
            raise RuntimeError('Multiple sections are not supported in 2D')

    def inviscidToViscous(self, iDict, vDict):
        """
        Interpolate inviscid data to viscous data.

        Parameters:
        ----------
        iDict : dict
            Inviscid data dictionary.
        vDict : dict
            Viscous data dictionary.
        """
        if self.ndim == 2:
            for iReg in range(len(iDict)):
                vDict[0][iReg].updateVars(iDict[iReg].V, iDict[iReg].M, iDict[iReg].Rho)
        elif self.ndim == 3:
            for iSec, ysec in enumerate(self._sections):
                for iReg in range(2):
                    print(iDict[iReg].nodesCoord[iDict[iReg].nodesCoord[:,1] == ysec])
                    print(iDict[iReg].V[iDict[iReg].nodesCoord[:,1] == ysec])
                    vDict[iSec][iReg].updateVars(iDict[iReg].V[iDict[iReg].nodesCoord[:,1] == ysec], iDict[iReg].Rho[iDict[iReg].nodesCoord[:,1] == ysec])

    def viscousToInviscid(self, iDict, vDict):
        """
        Interpolate viscous data to inviscid data.

        Parameters:
        ----------
        iDict : dict
            Inviscid data dictionary.
        vDict : dict
            Viscous data dictionary.
        """
        if self.ndim == 2:
            for iReg in range(2):
                iDict[iReg].blowingVel = vDict[0][iReg].blowingVel
        else:
            raise RuntimeError('Incorrect number of dimensions', self.ndim)