﻿using System.Runtime.InteropServices;

namespace APIclient_Csharp
{
    /// <summary>
    /// Note that PILSS must be installed. PILSS_DLL.dll depends on several other libraries.
    /// </summary>
    public class PilssFortranInterfaceDefinitions
    {
        // Todo: Specify full or relative path to the desired version of PILSS_DLL.dll,
        // internal const string pilssDllPathFullOrRelative = "..\..\PILSS_DLL.dll"; // Example of relative path.
        internal const string pilssDllPathFullOrRelative = @"C:\Program Files (x86)\DNV\PILSS V2.2-00\bin\x64\PILSS_DLL.dll"; // Absolute path to default installation folder (64-bit binaries).


        // Legacy v1.2 API does not accept any string arguments.
        // If writing results to file, these files will be written to the current work folder (where the C# client was called from)
        [DllImport(pilssDllPathFullOrRelative, EntryPoint = "PILSSInterface", CallingConvention = CallingConvention.StdCall)]
        public static extern void PILSSInterface(
            [In] double[] inCross_real,
            [In] double[] inHydro_real,
            [In] int[] inHydro_int,
            [In] int[] seedArray_phase,
            [In] int[] seedArray_crest,
            [In] double[] inSoil_real,
            [In] int[] inSoil_int,
            [In] double[] inTime_real,
            [In] int[] inTime_int,
            [In] ref int NrOfTimesteps,
            [In] ref int NrOfTimesteps_signal,
            [In] ref int NrOfTimesteps_res,
            [In] double[] inDyn_real,
            [In] int[] inDyn_int,
            [In][Out] double[] t_vector_U,
            [In][Out] double[,] U_t_matrix,
            [In][Out] double[,] A_t_matrix,
            [In][Out] double[,] F_H_matrix,
            [In][Out] double[,] F_L_matrix,
            [In] ref int NrOfPlotsteps,
            [In] ref int addedPlotRows,
            [In] ref double delta_t_plot,
            [In] ref int dummyInteger,
            [In] ref int printToFile,
            [In] ref int readWaveVelFromFile,
            [In] ref bool generatePlots,
            [In] ref bool ExplicitAcceleration,
            [In] ref bool dummyBoolean,
            [In] ref bool dummyBoolean2,
            [In][Out] double[,] result_matrix,
            [In][Out] double[,] maxMatrix,
            [In][Out] double[,] minMatrix,
            [In][Out] double[,] maxAbsMatrix,
            [In][Out] double[,] t_matrix_plot,
            [In][Out] double[,] y_matrix_plot,
            [In][Out] double[,] z_matrix_plot,
            [In][Out] double[,] v_matrix_plot,
            [In][Out] double[,] a_matrix_plot,
            [In][Out] double[,] Fsoil_matrix_plot,
            [In][Out] double[,] Fstiff_matrix_plot,
            [In][Out] double[,] FHrel_matrix_plot,
            [In][Out] double[,] FLrel_matrix_plot,
            [In][Out] double[,] yref_matrix_plot,
            [In][Out] int[,] iElPl_matrix_plot,
            [In][Out] int[,] outvar_int_matrix);

        // PILSS v2 API takes in optional strings used to specify results folder and prefix for results files.
        [DllImport(pilssDllPathFullOrRelative, EntryPoint = "PILSSInterface_v2", CallingConvention = CallingConvention.StdCall)]
        public static extern void PILSSInterface_v2(
            [In] double[] inCross_real,
            [In] double[] inHydro_real,
            [In] int[] inHydro_int,
            [In] int[] seedArray_phase,
            [In] int[] seedArray_crest,
            [In] double[] inSoil_real,
            [In] int[] inSoil_int,
            [In] double[] inTime_real,
            [In] int[] inTime_int,
            [In] ref int NrOfTimesteps,
            [In] ref int NrOfTimesteps_signal,
            [In] ref int NrOfTimesteps_res,
            [In] double[] inDyn_real,
            [In] int[] inDyn_int,
            [In][Out] double[] t_vector_U,
            [In][Out] double[,] U_t_matrix,
            [In][Out] double[,] A_t_matrix,
            [In][Out] double[,] F_H_matrix,
            [In][Out] double[,] F_L_matrix,
            [In] ref int NrOfPlotsteps,
            [In] ref int addedPlotRows,
            [In] ref double delta_t_plot,
            [In] ref int dummyInteger,
            [In] ref int printToFile,
            [In] ref int readWaveVelFromFile,
            [In] ref bool generatePlots,
            [In] ref bool ExplicitAcceleration,
            [In] ref bool dummyBoolean,
            [In] ref bool dummyBoolean2,
            [In][Out] double[,] result_matrix,
            [In][Out] double[,] maxMatrix,
            [In][Out] double[,] minMatrix,
            [In][Out] double[,] maxAbsMatrix,
            [In][Out] double[,] t_matrix_plot,
            [In][Out] double[,] y_matrix_plot,
            [In][Out] double[,] z_matrix_plot,
            [In][Out] double[,] v_matrix_plot,
            [In][Out] double[,] a_matrix_plot,
            [In][Out] double[,] Fsoil_matrix_plot,
            [In][Out] double[,] Fstiff_matrix_plot,
            [In][Out] double[,] FHrel_matrix_plot,
            [In][Out] double[,] FLrel_matrix_plot,
            [In][Out] double[,] yref_matrix_plot,
            [In][Out] int[,] iElPl_matrix_plot,
            [In][Out] int[,] outvar_int_matrix,
            [In][Optional] char[] resultsPath,
            [In][Optional] char[] caseName);
            //[In][Optional][MarshalAs(UnmanagedType.LPStr)] string resultsPath,
            //[In][Optional][MarshalAs(UnmanagedType.LPStr)] string caseName);
            // NOTE: The two different way of defining string in the Fortran call seems to work the same way.
    }
}