/*
 * 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 "Event.h"
#include "BaseSource.h"
#include "TaskItem.h"
#include "utils.h"

namespace mirai
{

Event::Event(BaseSource *source, Day *parent, const EventData &data)
	: source_(source), day_(parent), data(data)
{
}

void Event::setText(const std::string &text)
{
	this->data.description = text;
	onChange();
}

const std::string &Event::getText() const
{
	return data.description;
}

const Date &Event::getDate() const
{
	return data.date;
}

const Time &Event::getStartTime() const
{
	return data.startTime;
}

const Time &Event::getEndTime() const
{
	return data.endTime;
}

const Tags &Event::getTags() const
{
	return data.tags;
}

bool Event::hasTag(const std::string &tag) const
{
	return vectorUtils::contains(data.tags, tag);
}

TaskItem *Event::createTask(const TaskItemData &taskData)
{
	auto task = std::make_unique<TaskItem>(source_, day_, taskData);
	mirai::TaskItem *taskPtr = task.get();
	tasks_.push_back(std::move(task));
	onChange();
	return taskPtr;
}

void Event::deleteTask(const TaskItem &taskToDelete)
{
	tasks_.erase(std::remove_if(
		tasks_.begin(), tasks_.end(),
		[&](const std::unique_ptr<TaskItem> &task) {
			return task->id() == taskToDelete.id();
		}
	));
}

std::vector<std::unique_ptr<TaskItem>> *Event::tasks()
{
	return &tasks_;
}

void Event::setStartTime(const Time &time)
{
	data.startTime = time;
	onChange();
}

void Event::setEndTime(const Time &time)
{
	data.endTime = time;
	onChange();
}

void Event::onChange()
{
	source_->setDirty(true);
}

Event &Event::operator=(const EventData &newData)
{
	data = newData;
	onChange();
	return *this;
};

int Event::id() const
{
	return id_;
}

Day *Event::day()
{
	return day_;
}

int Event::nextId = 0;

} // namespace mirai
