/*
	PIP - Platform Independent Primitives
	State machine
	Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

	This program 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.

	This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pistatemachine_transition.h"

#include "pistatemachine_state.h"


PITransitionBase::PITransitionBase(PIStateBase * source, PIStateBase * target, int event_id) {
	target_state = target;
	source_state = source;
	eventID      = event_id;
}


PITransitionBase::~PITransitionBase() {
	piDeleteSafety(guard);
}


PITransitionBase * PITransitionBase::addAction(std::function<void()> a) {
	action = std::move(a);
	return this;
}


void PITransitionBase::makeAction() {
	if (action) action();
}


void PITransitionBase::trigger() {
	if (!target_state || (source_state == target_state)) {
		makeAction();
		return;
	}
	//	source_state->setActiveRecursive(false);
	auto source_path        = source_state->pathToMachine();
	auto target_path        = target_state->pathToMachine();
	auto source_target_path = target_path;
	source_target_path.reverse();
	// piCout << "source_path" << source_path;
	// piCout << "target_path" << target_path;
	ssize_t common_count = 0;
	for (common_count = 0; common_count < piMini(source_path.size_s(), target_path.size_s()); ++common_count) {
		// piCout << "check" << source_path[source_path.size_s() - common_count - 1] << target_path[target_path.size_s() - common_count -
		// 1];
		if (source_path[source_path.size_s() - common_count - 1] != target_path[target_path.size_s() - common_count - 1]) {
			// piCout << "diff" << common_count;
			break;
		}
	}
	// piCout << "common" << common_count;
	source_path.remove(source_path.size_s() - common_count, common_count);
	target_path.remove(target_path.size_s() - common_count, common_count);
	// piCout << "source_target_path" << source_target_path;
	// piCout << "source_path" << source_path;
	// piCout << "target_path" << target_path;
	// piCout << "source_state before" << source_state;
	for (int i = target_path.size_s() - 1; i >= 0; --i) {
		// piCout << "check" << source_state->activeChild() << target_path[i];
		if (source_state->activeChild() == target_path[i])
			source_state = target_path[i];
		else
			break;
	}
	// piCout << "source_state  after" << source_state;
	source_state->setChildrenActiveRecursive(false);
	for (auto * s: source_path)
		s->setActive(false);
	makeAction();
	for (int i = 0; i < source_target_path.size_s(); ++i) {
		if (i == source_target_path.size_s() - 1)
			source_target_path[i]->start();
		else
			source_target_path[i]->activeChild(source_target_path[i + 1]);
	}
	if (target_state->isCompound()) target_state->start(true);
}


// PITransitionTimeout

PITransitionTimeout::PITransitionTimeout(PIStateBase * source, PIStateBase * target, PISystemTime timeout)
	: PITransitionBase(source, target, 0) {
	timer.setInterval(timeout);
	timer.setSlot([this] {
		trigger();
		timer.stop();
	});
}


PITransitionTimeout::~PITransitionTimeout() {
	timer.stopAndWait();
}


void PITransitionTimeout::enabled() {
	timer.start();
}


void PITransitionTimeout::disabled() {
	timer.stop();
}
