// This file is part of GQCG-GQCP.
//
// Copyright (C) 2017-2020  the GQCG developers
//
// GQCG-GQCP is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GQCG-GQCP is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GQCG-GQCP.  If not, see <http://www.gnu.org/licenses/>.

#include "Basis/NonOrthogonalBasis/GNonOrthogonalStateBasis.hpp"
#include "Basis/NonOrthogonalBasis/RNonOrthogonalStateBasis.hpp"
#include "Basis/NonOrthogonalBasis/UNonOrthogonalStateBasis.hpp"
#include "QCModel/NOCI/NOCIExpansion.hpp"
#include "Utilities/complex.hpp"
#include "gqcpy/include/utilities.hpp"

#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>


namespace gqcpy {


// Provide some shortcuts for frequent namespaces.
namespace py = pybind11;
using namespace GQCP;


// Due to the different kinds of `NOCIExpansion`s we can encounter, we opt for an interface approach in order to avoid duplicate code.

/**
 *  Add Python bindings for some APIs related to the expansion of a NOCI calculation.
 *
 *  @tparam Class               The type of the Pybind11 `class_` (generated by the compiler).
 *
 *  @param py_class             The Pybind11 `class_` that should obtain APIs related to `NOCIExpansion`.
 */
template <typename Class>
void bindQCModelNOCIExpansionInterface(Class& py_class) {

    // The C++ type corresponding to the Python class.
    using Type = typename Class::type;
    using NonOrthogonalBasis = typename Type::NonOrthogonalBasis;

    py_class

        .def(py::init([](const NonOrthogonalBasis& non_orthogonal_basis, const Eigen::MatrixXd& coefficients) {
                 return Type(non_orthogonal_basis, coefficients);
             }),
             py::arg("non_orthogonal_basis"),
             py::arg("coefficients"),
             "Return an expansion inside the given non-orthogonal basis, with corresponding expansion coefficients.")


        /**
         * MARK: Access
         */

        .def("coefficient",
             &Type::coefficient,
             py::arg("index"),
             "Return the i'th expansion coefficient of this expansion wave function model.")

        .def("coefficients",
             &Type::coefficients,
             "Return the expansion coefficients of this expansion wave function model.")

        .def("nonOrthogonalStateBasis",
             &Type::nonOrthogonalStateBasis,
             "Return the non-orthogonal basis that is related to this expansion wave function model.")


        /**
         * MARK: Density Matrices
         */

        .def(
            "calculate1DM",
            [](const Type& NOCI_expansion) {
                return NOCI_expansion.calculate1DM();
            },
            "Calculate the one-electron density matrix of this NOCI expansion wave function model.");
}


void bindNOCIExpansions(py::module& module) {

    // Define the python classes related to a real valued NOCI expansion in a R/U/GNonOrthogonalStateBasis and expose the necessary interfaces.
    py::class_<NOCIExpansion<double, GNonOrthogonalStateBasis<double>>> py_NOCIExpansion_GNonOrthogonalStateBasis_d {module, "py_NOCIExpansion_GNonOrthogonalStateBasis_d", "The real-valued expansion (non-orthogonal configuration interaction) wave function model in a non-orthogonal basis of `generalized` states."};
    py::class_<NOCIExpansion<double, RNonOrthogonalStateBasis<double>>> py_NOCIExpansion_RNonOrthogonalStateBasis_d {module, "py_NOCIExpansion_RNonOrthogonalStateBasis_d", "The real-valued expansion (non-orthogonal configuration interaction) wave function model in a non-orthogonal basis of `restricted` states."};
    py::class_<NOCIExpansion<double, UNonOrthogonalStateBasis<double>>> py_NOCIExpansion_UNonOrthogonalStateBasis_d {module, "py_NOCIExpansion_UNonOrthogonalStateBasis_d", "The real-valued expansion (non-orthogonal configuration interaction) wave function model in a non-orthogonal basis of `unrestricted` states."};

    py_NOCIExpansion_GNonOrthogonalStateBasis_d

        .def(
            "calculate2DM",
            [](const NOCIExpansion<double, GNonOrthogonalStateBasis<double>>& NOCI_expansion) {
                return NOCI_expansion.calculate2DM();
            },
            "Calculate the two-electron density matrix of this NOCI expansion wave function model.");


    // Expose the NOCI expansion interface.
    bindQCModelNOCIExpansionInterface(py_NOCIExpansion_GNonOrthogonalStateBasis_d);
    bindQCModelNOCIExpansionInterface(py_NOCIExpansion_RNonOrthogonalStateBasis_d);
    bindQCModelNOCIExpansionInterface(py_NOCIExpansion_UNonOrthogonalStateBasis_d);

    // Define the python classes related to a complex valued NOCI expansion in a R/U/GNonOrthogonalStateBasis and expose the necessary interfaces.
    py::class_<NOCIExpansion<complex, GNonOrthogonalStateBasis<complex>>> py_NOCIExpansion_GNonOrthogonalStateBasis_cd {module, "py_NOCIExpansion_GNonOrthogonalStateBasis_cd", "The complex-valued expansion (non-orthogonal configuration interaction) wave function model in a non-orthogonal basis of `generalized` states."};
    py::class_<NOCIExpansion<complex, RNonOrthogonalStateBasis<complex>>> py_NOCIExpansion_RNonOrthogonalStateBasis_cd {module, "py_NOCIExpansion_RNonOrthogonalStateBasis_cd", "The complex-valued expansion (non-orthogonal configuration interaction) wave function model in a non-orthogonal basis of `restricted` states."};
    py::class_<NOCIExpansion<complex, UNonOrthogonalStateBasis<complex>>> py_NOCIExpansion_UNonOrthogonalStateBasis_cd {module, "py_NOCIExpansion_UNonOrthogonalStateBasis_cd", "The complex-valued expansion (non-orthogonal configuration interaction) wave function model in a non-orthogonal basis of `unrestricted` states."};

    py_NOCIExpansion_GNonOrthogonalStateBasis_cd

        .def(
            "calculate2DM",
            [](const NOCIExpansion<complex, GNonOrthogonalStateBasis<complex>>& NOCI_expansion) {
                return NOCI_expansion.calculate2DM();
            },
            "Calculate the two-electron density matrix of this NOCI expansion wave function model.");

    // Expose the NOCI expansion interface.
    bindQCModelNOCIExpansionInterface(py_NOCIExpansion_GNonOrthogonalStateBasis_cd);
    bindQCModelNOCIExpansionInterface(py_NOCIExpansion_RNonOrthogonalStateBasis_cd);
    bindQCModelNOCIExpansionInterface(py_NOCIExpansion_UNonOrthogonalStateBasis_cd);
}


}  // namespace gqcpy
