// 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/ScalarBasis/GTOShell.hpp"
#include "Basis/SpinorBasis/RSpinOrbitalBasis.hpp"
#include "Utilities/complex.hpp"
#include "gqcpy/include/interfaces.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;


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

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


    /*
     *  MARK: Constructors
     */

    py_class
        .def(py::init<const Molecule&, const std::string&>(),
             py::arg("molecule"),
             py::arg("basisset_name"));


    // Expose some Mulliken API to the Python class;
    bindSpinorBasisMullikenInterface(py_class);
}


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

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


    /*
     *  MARK: General information
     */

    py_class
        .def(
            "numberOfSpatialOrbitals",
            &Type::numberOfSpatialOrbitals,
            "Return the number of different spatial orbitals that are used in this spinor basis.")


        .def(
            "scalarBasis",
            &Type::scalarBasis,
            "Return the underlying scalar basis with respect to which the basis coefficients are expressed.")

            
        .def(
            "evalBasisSetAtPoint",
            [](const Type& self, const GQCP::Vector<double, 3>& r) {
                return self.evalBasisSetAtPoint(r);
            },
            py::arg("r"),
            "Evaluate the underlying AO basis functions at a given spatial point.")
        
        
        .def(
            "evalGradBasisSetAtPoint",
            [](const Type& self, const GQCP::Vector<double, 3>& r) {
                return self.evalGradBasisSetAtPoint(r);
            },
            py::arg("r"),
            "Evaluate the gradient of the underlying AO basis functions at a given spatial point.");


    // Expose the `SimpleSpinorBasis` API to the Python class.
    bindSpinorBasisInterface(py_class);


    // Expose some quantization API to the Python class;
    bindSpinorBasisQuantizationInterface(py_class);
}


/**
 *  Register `RSpinOrbitalBasis_d` and `RSpinOrbitalBasis_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 bindRSpinOrbitalBases(py::module& module) {

    // Define the Python class for `RSpinOrbitalBasis_d`.
    py::class_<RSpinOrbitalBasis<double, GTOShell>> py_RSpinOrbitalBasis_d {module, "RSpinOrbitalBasis_d", "A class that represents a real, restricted spinor basis with underlying GTO shells."};
    bindRSpinOrbitalBasisInterface(py_RSpinOrbitalBasis_d);
    bindGTORSpinOrbitalBasisInterface(py_RSpinOrbitalBasis_d);

    py_RSpinOrbitalBasis_d
        .def(
            "quantize",
            [](const RSpinOrbitalBasis<double, GTOShell>& spin_orbital_basis, const ElectronicDipoleOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the electronic dipole operator expressed in this spinor basis.")

        .def(
            "quantize",
            [](const RSpinOrbitalBasis<double, GTOShell>& spin_orbital_basis, const ElectronicDensityOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the electronic density operator expressed in this spinor basis.");


    // Define the Python class for `RSpinOrbitalBasis_cd`.
    py::class_<RSpinOrbitalBasis<complex, GTOShell>> py_RSpinOrbitalBasis_cd {module, "RSpinOrbitalBasis_cd", "A class that represents a complex, restricted spinor basis with underlying GTO shells."};
    bindRSpinOrbitalBasisInterface(py_RSpinOrbitalBasis_cd);
    bindGTORSpinOrbitalBasisInterface(py_RSpinOrbitalBasis_cd);

    py_RSpinOrbitalBasis_cd
        .def(
            "quantize",
            [](const RSpinOrbitalBasis<complex, GTOShell>& spin_orbital_basis, const AngularMomentumOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the angular momentum operator expressed in this spinor basis.")

        .def(
            "quantize",
            [](const RSpinOrbitalBasis<complex, GTOShell>& spin_orbital_basis, const LinearMomentumOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the linear momentum operator expressed in this spinor basis.")

        .def(
            "quantize",
            [](const RSpinOrbitalBasis<complex, GTOShell>& spin_orbital_basis, const CurrentDensityOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the current density operator expressed in this spinor basis.");


    // Define the Python class for `LondonRSpinOrbitalBasis`.
    py::class_<RSpinOrbitalBasis<complex, LondonGTOShell>> py_LondonRSpinOrbitalBasis {module, "LondonRSpinOrbitalBasis", "A class that represents a complex, restricted spin-orbital basis with underlying London GTO shells."};

    py_LondonRSpinOrbitalBasis
        .def(
            "quantize",
            [](const RSpinOrbitalBasis<complex, LondonGTOShell>& spin_orbital_basis, const ElectronicDipoleOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the electronic dipole operator expressed in this London spinor basis.")
            
        .def(
            "quantize",
            [](const RSpinOrbitalBasis<complex, LondonGTOShell>& spin_orbital_basis, const ElectronicQuadrupoleOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the electronic quadrupole operator expressed in this London spinor basis.")
        
        .def(
            "quantize",
            [](const RSpinOrbitalBasis<complex, LondonGTOShell>& spin_orbital_basis, const LinearMomentumOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the linear momentum operator expressed in this London spinor basis.");

    bindRSpinOrbitalBasisInterface(py_LondonRSpinOrbitalBasis);
    bindLondonSpinorBasisQuantizationInterface(py_LondonRSpinOrbitalBasis);
}


}  // namespace gqcpy
