/************************************************************************
 *
 * Copyright (C) 2022-2026 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight 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.
 *
 * Sight 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 Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include <core/runtime/runtime.hpp>

#include <data/camera.hpp>
#include <data/camera_set.hpp>
#include <data/exception.hpp>

#include <service/base.hpp>
#include <service/op.hpp>

#include <boost/config.hpp>

#include <doctest/doctest.h>

TEST_SUITE("sight::module::data::get_camera")
{
//------------------------------------------------------------------------------

    TEST_CASE("extracts_camera_from_camera_set")
    {
        // Set up context before running a test.
        sight::core::runtime::init();
        auto module = sight::core::runtime::load_module(std::string("sight::module::data"));

        // Create a camera series.
        sight::data::camera_set::sptr camera_set = std::make_shared<sight::data::camera_set>();
        // create service
        sight::service::base::sptr get_camera_srv = sight::service::add("sight::module::data::get_camera");
        CHECK(get_camera_srv->is_a("sight::module::data::get_camera"));

        // ------------------ create a matrix ----------------------
        sight::data::matrix4::sptr matrix = std::make_shared<sight::data::matrix4>();
        // -- Camera 1 --
        sight::data::camera::sptr camera1 = std::make_shared<sight::data::camera>();
        // -- Camera 2 --
        sight::data::camera::sptr camera2 = std::make_shared<sight::data::camera>();

        camera_set->add_camera(camera1);
        camera_set->add_camera(camera2);
        CHECK_EQ(camera_set->size(), std::size_t(2));
        // -------------Set extrinsic Matrix-----------------
        camera_set->set_extrinsic_matrix(0, matrix);

        const std::string config =
            "<out group='camera'>"
            "<key index='0' uid='DCamera'/>"
            "<key index='1' uid='RGBCamera'/>"
            "</out>"
            "<out group='extrinsic'>"
            "<key index='0' uid='extrinsic'/>"
            "</out>";

        get_camera_srv->set_config(config);
        get_camera_srv->set_inout(camera_set, "camera_set");
        get_camera_srv->configure();
        get_camera_srv->start().wait();

        CHECK_NOTHROW(get_camera_srv->update().get());
        CHECK_EQ(get_camera_srv->output("camera", 0).lock()->get_id(), camera1->get_id());
        CHECK_EQ(get_camera_srv->output("camera", 1).lock()->get_id(), camera2->get_id());
        CHECK_EQ(get_camera_srv->output("extrinsic", 0).lock()->get_id(), matrix->get_id());

        get_camera_srv->stop().wait();
        sight::service::remove(get_camera_srv);
    }

//------------------------------------------------------------------------------

    TEST_CASE("extracts_invalid_camera_set")
    {
        // create service
        sight::service::base::sptr get_camera_srv = sight::service::add("sight::module::data::get_camera");
        CHECK(get_camera_srv->is_a("sight::module::data::get_camera"));
        const std::string config =
            "<out group='camera'>"
            "<key index='0' uid='DCamera'/>"
            "<key index='1' uid='RGBCamera'/>"
            "</out>"
            "<out group='extrinsic'>"
            "<key index='0' uid='extrinsic'/>"
            "</out>";

        get_camera_srv->set_config(config);
        get_camera_srv->set_inout(nullptr, "camera_set");
        get_camera_srv->configure();
        get_camera_srv->start().wait();

        CHECK_THROWS_AS(get_camera_srv->update().get(), sight::data::exception);
        get_camera_srv->stop().wait();
        sight::service::remove(get_camera_srv);
    }

//------------------------------------------------------------------------------

    TEST_CASE("extracts_valid_extrinsic")
    {
        // // Create a camera series.
        sight::data::camera_set::sptr camera_set = std::make_shared<sight::data::camera_set>();
        // create service
        sight::service::base::sptr get_camera_srv = sight::service::add("sight::module::data::get_camera");
        CHECK(get_camera_srv->is_a("sight::module::data::get_camera"));
        // -- Camera 1 --
        sight::data::camera::sptr camera1 = std::make_shared<sight::data::camera>();
        // -- Camera 2 --
        sight::data::camera::sptr camera2 = std::make_shared<sight::data::camera>();
        camera_set->add_camera(camera1);
        camera_set->add_camera(camera2);
        CHECK_EQ(camera_set->size(), std::size_t(2));

        const std::string config =
            "<out group='camera'>"
            "<key index='0' uid='DCamera'/>"
            "<key index='1' uid='RGBCamera'/>"
            "</out>";

        get_camera_srv->set_config(config);
        get_camera_srv->set_inout(camera_set, "camera_set");
        get_camera_srv->configure();
        get_camera_srv->start().wait();

        CHECK_NOTHROW(get_camera_srv->update().get());
        CHECK_EQ(get_camera_srv->output("camera", 0).lock()->get_id(), camera1->get_id());
        CHECK_EQ(get_camera_srv->output("camera", 1).lock()->get_id(), camera2->get_id());
        get_camera_srv->stop().wait();
        sight::service::remove(get_camera_srv);
    }

//------------------------------------------------------------------------------

    TEST_CASE("extracts_valid_extrinsic1")
    {
        // // Create a camera series.
        sight::data::camera_set::sptr camera_set = std::make_shared<sight::data::camera_set>();
        // create service
        sight::service::base::sptr get_camera_srv = sight::service::add("sight::module::data::get_camera");
        CHECK(get_camera_srv->is_a("sight::module::data::get_camera"));
        // -- Camera 1 --
        sight::data::camera::sptr camera1 = std::make_shared<sight::data::camera>();
        // -- Camera 2 --
        sight::data::camera::sptr camera2 = std::make_shared<sight::data::camera>();
        // ------------------ create a matrix ----------------------
        sight::data::matrix4::sptr matrix = std::make_shared<sight::data::matrix4>();

        camera_set->add_camera(camera1);
        camera_set->add_camera(camera2);
        CHECK_EQ(camera_set->size(), std::size_t(2));
        camera_set->set_extrinsic_matrix(0, matrix);
        const std::string config =
            "<out group='camera'>"
            "<key index='0' uid='DCamera'/>"
            "<key index='1' uid='RGBCamera'/>"
            "</out>";

        get_camera_srv->set_config(config);
        get_camera_srv->set_inout(camera_set, "camera_set");
        get_camera_srv->configure();
        get_camera_srv->start().wait();

        CHECK_NOTHROW(get_camera_srv->update().get());
        CHECK_EQ(get_camera_srv->output("camera", 0).lock()->get_id(), camera1->get_id());
        CHECK_EQ(get_camera_srv->output("camera", 1).lock()->get_id(), camera2->get_id());
        get_camera_srv->stop().wait();
        sight::service::remove(get_camera_srv);
    }

//------------------------------------------------------------------------------

    TEST_CASE("extracts_camera_out_of_bound_index")
    {
        // Create a camera series.
        auto camera_set = std::make_shared<sight::data::camera_set>();
        // create service
        auto get_camera_srv = sight::service::add("sight::module::data::get_camera");
        CHECK(get_camera_srv->is_a("sight::module::data::get_camera"));
        // -- Camera 1 --
        auto camera1 = std::make_shared<sight::data::camera>();
        // -- Camera 2 --
        auto camera2 = std::make_shared<sight::data::camera>();

        camera_set->add_camera(camera1);
        camera_set->add_camera(camera2);
        CHECK_EQ(camera_set->size(), std::size_t(2));

        const std::string config =
            "<out group='camera'>"
            "<key index='0' uid='DCamera'/>"
            "<key index='5' uid='RGBCamera'/>"
            "</out>"
            "<out group='extrinsic'>"
            "<key index='1' uid='extrinsic'/>"
            "</out>";

        get_camera_srv->set_config(config);
        get_camera_srv->set_inout(camera_set, "camera_set");
        get_camera_srv->configure();
        get_camera_srv->start().wait();

        CHECK_THROWS_AS(get_camera_srv->update().get(), std::out_of_range);
        get_camera_srv->stop().wait();
        sight::service::remove(get_camera_srv);
    }

//------------------------------------------------------------------------------

    TEST_CASE("extracts_extrinsic_out_of_bound_index")
    {
        // Create a camera series.
        sight::data::camera_set::sptr camera_set = std::make_shared<sight::data::camera_set>();
        // create service
        sight::service::base::sptr get_camera_srv = sight::service::add("sight::module::data::get_camera");
        CHECK(get_camera_srv->is_a("sight::module::data::get_camera"));

        // -- Camera 1 --
        sight::data::camera::sptr camera1 = std::make_shared<sight::data::camera>();
        // -- Camera 2 --
        sight::data::camera::sptr camera2 = std::make_shared<sight::data::camera>();
        camera_set->add_camera(camera1);
        camera_set->add_camera(camera2);
        CHECK_EQ(camera_set->size(), std::size_t(2));

        const std::string config =
            "<out group='camera'>"
            "<key index='0' uid='DCamera'/>"
            "<key index='1' uid='RGBCamera'/>"
            "</out>"
            "<out group='extrinsic'>"
            "<key index='5' uid='extrinsic'/>"
            "</out>";

        get_camera_srv->set_config(config);
        get_camera_srv->set_inout(camera_set, "camera_set");
        get_camera_srv->configure();
        get_camera_srv->start().wait();

        CHECK_THROWS_AS(get_camera_srv->update().get(), std::out_of_range);
        get_camera_srv->stop().wait();
        sight::service::remove(get_camera_srv);
    }

//------------------------------------------------------------------------------

    TEST_CASE("extracts_invalid_extrinsic")
    {
        // Create a camera series.
        sight::data::camera_set::sptr camera_set = std::make_shared<sight::data::camera_set>();
        // create service
        sight::service::base::sptr get_camera_srv = sight::service::add("sight::module::data::get_camera");
        CHECK(get_camera_srv->is_a("sight::module::data::get_camera"));
        // -- Camera 1 --
        sight::data::camera::sptr camera1 = std::make_shared<sight::data::camera>();
        // -- Camera 2 --
        sight::data::camera::sptr camera2 = std::make_shared<sight::data::camera>();

        camera_set->add_camera(camera1);
        camera_set->add_camera(camera2);
        CHECK_EQ(camera_set->size(), std::size_t(2));

        const std::string config =
            "<out group='camera'>"
            "<key index='0' uid='DCamera'/>"
            "<key index='1' uid='RGBCamera'/>"
            "</out>"
            "<out group='extrinsic'>"
            "<key index='1' uid='extrinsic'/>"
            "</out>"
        ;

        get_camera_srv->set_config(config);
        get_camera_srv->set_inout(camera_set, "camera_set");
        get_camera_srv->configure();
        get_camera_srv->start().wait();

        CHECK_THROWS_AS(get_camera_srv->update().get(), sight::core::exception);
        CHECK_EQ(get_camera_srv->output("camera", 0).lock()->get_id(), camera1->get_id());
        CHECK_EQ(get_camera_srv->output("camera", 1).lock()->get_id(), camera2->get_id());

        get_camera_srv->stop().wait();
        sight::service::remove(get_camera_srv);
    }

//------------------------------------------------------------------------------
} // TEST_SUITE("sight::module::data::get_camera")
