// 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 "Mathematical/Optimization/Eigenproblem/EigenproblemEnvironment.hpp"
#include "Utilities/complex.hpp"

#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 `EigenproblemEnvironment`.
 * 
 *  @tparam Class               The type of the Pybind11 `class_` (generated by the compiler).
 *  
 *  @param py_class             The Pybind11 `class_` that should obtain APIs related to `EigenproblemEnvironment`.
 */
template <typename Class>
void bindEigenproblemEnvironmentInterface(Class& py_class) {

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


    // Define read/write properties.
    py_class
        .def_readwrite(
            "dimension",
            &EigenproblemEnvironment<Scalar>::dimension)


        // Bind properties with a custom setter (to allow for non-native Eigen types).
        .def_property(
            "A",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.A;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_A) {
                environment.A = SquareMatrix<Scalar>(new_A);
            })

        .def_property(
            "diagonal",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.diagonal;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>& new_diagonal) {
                environment.diagonal = VectorX<Scalar>(new_diagonal);
            })

        .def_property(
            "eigenvalues",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.eigenvalues;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<double, Eigen::Dynamic, 1>& new_eigenvalues) {
                environment.eigenvalues = VectorX<double>(new_eigenvalues);
            })

        .def_property(
            "eigenvectors",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.eigenvectors;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_eigenvectors) {
                environment.eigenvectors = MatrixX<Scalar>(new_eigenvectors);
            })

        .def_property(
            "S",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.S;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_S) {
                environment.S = SquareMatrix<Scalar>(new_S);
            })

        .def_property(
            "Lambda",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.Lambda;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<double, Eigen::Dynamic, 1>& new_Lambda) {
                environment.Lambda = VectorX<double>(new_Lambda);
            })

        .def_property(
            "Z",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.Z;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_Z) {
                environment.Z = MatrixX<Scalar>(new_Z);
            })

        .def_property(
            "V",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.V;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_V) {
                environment.V = MatrixX<Scalar>(new_V);
            })

        .def_property(
            "VA",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.VA;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_VA) {
                environment.VA = MatrixX<Scalar>(new_VA);
            })

        .def_property(
            "X",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.X;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_X) {
                environment.X = MatrixX<Scalar>(new_X);
            })

        .def_property(
            "R",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.R;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_R) {
                environment.R = MatrixX<Scalar>(new_R);
            })

        .def_property(
            "Delta",
            [](const EigenproblemEnvironment<Scalar>& environment) {
                return environment.Delta;
            },
            [](EigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_Delta) {
                environment.Delta = MatrixX<Scalar>(new_Delta);
            });
}


/**
 *  Register `EigenproblemEnvironment_d` and `EigenproblemEnvironment_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 bindEigenproblemEnvironment(py::module& module) {

    // Define the Python class for `EigenproblemEnvironment_d`.
    py::class_<EigenproblemEnvironment<double>> py_EigenproblemEnvironment_d {module, "EigenproblemEnvironment_d", "A real-valued environment used to solve eigenvalue problems for self-adjoint matrices."};
    bindEigenproblemEnvironmentInterface(py_EigenproblemEnvironment_d);

    // Define the Python class for `EigenproblemEnvironment_cd`.
    py::class_<EigenproblemEnvironment<complex>> py_EigenproblemEnvironment_cd {module, "EigenproblemEnvironment_cd", "A complex-valued environment used to solve eigenvalue problems for self-adjoint matrices."};
    bindEigenproblemEnvironmentInterface(py_EigenproblemEnvironment_cd);
}


}  // namespace gqcpy
