/*
 * Mirai. Copyright (C) 2024 Vyn
 * This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
 * The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
 */

#include "Mirai.h"
#include "Config.h"
#include "DataProvider.h"
#include "Source.h"
#include "cpp-utils/debug.h"
#include "utils.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <optional>
#include <ostream>
#include <vector>

namespace mirai
{

void Mirai::loadSource(std::unique_ptr<DataProvider> &&resource)
{
	DataProvider *sourceDataProvider = resource.release();
	sourceDataProvider->load();
	sources_.push_back(
		std::make_unique<Source>(SourceConstructor{.sourceDataProvider = sourceDataProvider})
	);
};

void Mirai::unloadAllSources()
{
	sources_.clear();
}

void Mirai::save()
{
	for (auto &source : sources_) {
		if (source->isDirty()) {
			source->save();
		}
	}
}

std::vector<std::unique_ptr<Source>> &Mirai::getSources()
{
	return sources_;
}

Source *Mirai::getSourceById(int id)
{
	if (id >= sources_.size()) {
		return nullptr;
	}
	return sources_.at(id).get();
}

} // namespace mirai
