// 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/USpinOrbitalBasis.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 `USpinOrbitalBasis` 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 bindGTOUSpinOrbitalBasisInterface(Class& py_class) {

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


    py_class

        /*
         *  MARK: Constructors
         */

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


        /*
         *  MARK: Named constructors
         */

        .def_static(
            "FromRestricted",
            [](const RSpinOrbitalBasis<Scalar, GTOShell>& r_spin_orbital_basis) {
                return USpinOrbitalBasis<Scalar, GTOShell>::FromRestricted(r_spin_orbital_basis);
            },
            py::arg("r_spin_orbital_basis"),
            "Create an unrestricted spin-orbital basis from a restricted spin-orbital basis, leading to alpha- and beta- coefficient matrices that are equal.");


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


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

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


    py_class

        /*
         *  MARK: General information
         */

        .def(
            "numberOfSpinors",
            &Type::numberOfSpinors,
            "Return the total number of spinors/spin-orbitals that this spin-orbital basis describes.");


    // Expose some spinor basis APIs to the Python class.
    bindSpinorBasisInterface(py_class);


    /*
     *  MARK: Quantization of first-quantized operators
     */

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

    py_class
        .def(
            "quantize",
            [](const Type& spin_orbital_basis, const ElectronicSpin_zOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the spin z operator expressed in this spinor basis.")

        .def(
            "quantize",
            [](const USpinOrbitalBasis<double, GTOShell>& spin_orbital_basis, const ElectronicSpinSquaredOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the spin Squared operator expressed in this spinor basis.");
}


/**
 *  Register `USpinOrbitalBasis_d` and `USpinOrbitalBasis_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 bindUSpinOrbitalBases(py::module& module) {

    // Define the Python class for `USpinOrbitalBasis_d`.
    py::class_<USpinOrbitalBasis<double, GTOShell>> py_USpinOrbitalBasis_d {module, "USpinOrbitalBasis_d", "A class that represents a real, unrestricted spin-orbital basis with underlying GTO shells."};

    bindUSpinOrbitalBasisInterface(py_USpinOrbitalBasis_d);
    bindGTOUSpinOrbitalBasisInterface(py_USpinOrbitalBasis_d);

    py_USpinOrbitalBasis_d
        .def(
            "quantize",
            [](const USpinOrbitalBasis<double, GTOShell>& spin_orbital_basis, const ElectronicDipoleOperator& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the electronic dipole operator expressed in this spin-orbital basis.");


    // Define the Python class for `USpinOrbitalBasis_cd`.
    py::class_<USpinOrbitalBasis<complex, GTOShell>> py_USpinOrbitalBasis_cd {module, "USpinOrbitalBasis_cd", "A class that represents a complex, unrestricted spinor basis with underlying GTO shells."};

    bindUSpinOrbitalBasisInterface(py_USpinOrbitalBasis_cd);
    bindGTOUSpinOrbitalBasisInterface(py_USpinOrbitalBasis_cd);


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

    bindUSpinOrbitalBasisInterface(py_LondonUSpinOrbitalBasis);
    bindLondonSpinorBasisQuantizationInterface(py_LondonUSpinOrbitalBasis);

    py_LondonUSpinOrbitalBasis
        .def(
            "quantize",
            [](const USpinOrbitalBasis<complex, LondonGTOShell>& spin_orbital_basis, const FQMolecularPauliHamiltonian& op) {
                return spin_orbital_basis.quantize(op);
            },
            "Return the molecular Pauli Hamiltonian expressed in this spinor basis.");
}


}  // namespace gqcpy
