// 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 "QCModel/HF/RHF.hpp"
#include "Utilities/complex.hpp"
#include "gqcpy/include/interfaces.hpp"

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


namespace gqcpy {


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


/**
 *  Add Python bindings for some APIs related to `QCModel::RHF`.
 * 
 *  @tparam Class               The type of the Pybind11 `class_` (generated by the compiler).
 *  
 *  @param py_class             The Pybind11 `class_` that should obtain APIs related to `QCModel::RHF`.
 */
template <typename Class>
void bindQCModelRHFInterface(Class& py_class) {

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

    py_class

        /*
         *  MARK: Energy
         */

        .def(
            "spinOrbitalEnergiesBlocked",
            &QCModel::RHF<Scalar>::spinOrbitalEnergiesBlocked,
            "Return all the spin-orbital energies, with the alpha spin-orbital energies appearing before the beta spin-orbital energies.")

        .def(
            "spinOrbitalEnergiesInterleaved",
            &QCModel::RHF<Scalar>::spinOrbitalEnergiesInterleaved,
            "Return all the spin-orbital energies, with the alpha spin-orbital energies appearing before the beta spin-orbital energies.");


    // Expose the `HartreeFock` interface.
    bindQCModelHartreeFockInterface(py_class);
}


/**
 *  Register `QCModel_RHF_d` and `QCModel_RHF_cd` to the gqcpy module and expose parts of their C++ interfaces to Python.
 * 
 *  @param module           The Pybind11 module in which the classes should be registered.
 */
void bindQCModelRHF(py::module& module) {

    // Define the Python class for `QCModel_RHF_d`.
    py::class_<QCModel::RHF<double>> py_QCModel_RHF_d {module, "QCModel_RHF_d", "The (real-valued) restricted Hartree-Fock wave function model."};
    bindQCModelRHFInterface(py_QCModel_RHF_d);

    py_QCModel_RHF_d
        .def_static(
            "calculateOrbitalHessianForImaginaryResponse",
            [](const RSQHamiltonian<double>& sq_hamiltonian, const OrbitalSpace& orbital_space) {
                return QCModel::RHF<double>::calculateOrbitalHessianForImaginaryResponse(sq_hamiltonian, orbital_space).asMatrix();
            },
            py::arg("sq_hamiltonian"),
            py::arg("orbital_space"),
            "Calculate the RHF orbital Hessian (H_RI -i H_II), which can be used as a response force constant when solving the CP(R)HF equations for a purely imaginary response.")

        .def_static(
            "calculateScalarBasisDirectMatrix",
            [](const Orbital1DM<double>& D, const RSQHamiltonian<double>& sq_hamiltonian) {
                return QCModel::RHF<double>::calculateScalarBasisDirectMatrix(D, sq_hamiltonian);
            },
            py::arg("density_matrix"),
            py::arg("hamiltonian"),
            "Return the Coulomb (J) matrix.")

        .def_static(
            "calculateScalarBasisExchangeMatrix",
            [](const Orbital1DM<double>& D, const RSQHamiltonian<double>& sq_hamiltonian) {
                return QCModel::RHF<double>::calculateScalarBasisExchangeMatrix(D, sq_hamiltonian);
            },
            py::arg("density_matrix"),
            py::arg("hamiltonian"),
            "Return the exchange (K) matrix.")

        .def_static(
            "calculateScalarBasisFockMatrix",
            [](const Orbital1DM<double>& D, const RSQHamiltonian<double>& sq_hamiltonian) {
                return QCModel::RHF<double>::calculateScalarBasisFockMatrix(D, sq_hamiltonian);
            },
            py::arg("density_matrix"),
            py::arg("hamiltonian"),
            "Return the Fock matrix (F).")

        .def(
            "calculateMagneticFieldResponseForce",
            [](const QCModel::RHF<double>& rhf_parameters, const VectorRSQOneElectronOperator<complex>& L_op) {
                return rhf_parameters.calculateMagneticFieldResponseForce(L_op);
            },
            py::arg("L_op"),
            "Calculate the RHF response force for the perturbation due to a magnetic field.")

        .def(
            "calculateGaugeOriginTranslationResponseForce",
            [](const QCModel::RHF<double>& rhf_parameters, const VectorRSQOneElectronOperator<complex>& p_op) {
                return rhf_parameters.calculateGaugeOriginTranslationResponseForce(p_op);
            },
            py::arg("p_op"),
            "Calculate the RHF response force for the perturbation due to a gauge origin translation of the magnetic field.");


    // Define the Python class for `QCModel_RHF_cd`.
    py::class_<QCModel::RHF<complex>> py_QCModel_RHF_cd {module, "QCModel_RHF_cd", "The (complex-valued) restricted Hartree-Fock wave function model."};
    bindQCModelRHFInterface(py_QCModel_RHF_cd);

    py_QCModel_RHF_cd
        .def_static(
            "calculateScalarBasisDirectMatrix",
            [](const Orbital1DM<complex>& D, const RSQHamiltonian<complex>& sq_hamiltonian) {
                return QCModel::RHF<complex>::calculateScalarBasisDirectMatrix(D, sq_hamiltonian);
            },
            py::arg("density_matrix"),
            py::arg("hamiltonian"),
            "Return the Coulomb (J) matrix.")

        .def_static(
            "calculateScalarBasisExchangeMatrix",
            [](const Orbital1DM<complex>& D, const RSQHamiltonian<complex>& sq_hamiltonian) {
                return QCModel::RHF<complex>::calculateScalarBasisExchangeMatrix(D, sq_hamiltonian);
            },
            py::arg("density_matrix"),
            py::arg("hamiltonian"),
            "Return the exchange (K) matrix.")

        .def_static(
            "calculateScalarBasisFockMatrix",
            [](const Orbital1DM<complex>& D, const RSQHamiltonian<complex>& sq_hamiltonian) {
                return QCModel::RHF<complex>::calculateScalarBasisFockMatrix(D, sq_hamiltonian);
            },
            py::arg("density_matrix"),
            py::arg("hamiltonian"),
            "Return the Fock matrix (F).")
            
        .def_static(
            "calculateIpsocentricMagneticInducibility",
            [](const Vector<double, 3>& r, const OrbitalSpace& orbital_space, const Matrix<complex, Dynamic, 3>& x_B, const Matrix<complex, Dynamic, 6>& x_G, const VectorEvaluableRSQOneElectronOperator<CurrentDensityMatrixElement<complex, CartesianGTO>>& j_op) {
                return QCModel::RHF<complex>::calculateIpsocentricMagneticInducibility(r, orbital_space, x_B, x_G, j_op);
            },
            py::arg("r"),
            py::arg("orbital_space"),
            py::arg("x_B"),
            py::arg("x_g"),
            py::arg("j_op"),
            "Evaluate the magnetic inducibility at the given point using the ipsocentric CSGT method.")

        .def_static(
            "calculateIpsocentricMagneticInducibility",
            [](const CubicGrid& grid, const OrbitalSpace& orbital_space, const Matrix<complex, Dynamic, 3>& x_B, const Matrix<complex, Dynamic, 6>& x_G, const VectorEvaluableRSQOneElectronOperator<CurrentDensityMatrixElement<complex, CartesianGTO>>& j_op) {
                return QCModel::RHF<complex>::calculateIpsocentricMagneticInducibility(grid, orbital_space, x_B, x_G, j_op);
            },
            py::arg("grid"),
            py::arg("orbital_space"),
            py::arg("x_B"),
            py::arg("x_g"),
            py::arg("j_op"),
            "Calculate the magnetic inducibility on the given grid using the ipsocentric CSGT method.");
}


}  // namespace gqcpy
