<?php

class EdittrackController extends Controller
{

	function __construct()
	{
		if (!App::$user->id) {
			App::redirect('/');
		}
	}

	function actionTrip()
	{
		$id = intval(App::getParam('id'));
		$trip = Trips::model()->getByPK($id);

		if (empty($_POST['name']) || $trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$trip->name = trim($_POST['name']);
		$trip->description = trim($_POST['description']);
		$trip->status = ($_POST['isPublic'] == 'true') ? Trips::STATUS_PUBLIC : Trips::STATUS_PRIVATE;
		$trip->json = '';
		$trip->save();
	}

	function actionDeletetrip()
	{
		$id = intval(App::getParam('id'));
		$trip = Trips::model()->getByPK($id);

		if ($trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$trip->delete($id);

		App::redirect('/tracks/');
	}

	function actionTrack()
	{
		$id = intval(App::getParam('id'));

		$track = Tracks::model()->getByPK($id);
		if (empty($_POST['name']) || $track->isEmpty()) {
			App::error404();
		}

		$trip = Trips::model()->getByPK($track->tripId);
		if ($trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$track->name = trim($_POST['name']);
		$track->description = trim($_POST['description']);
		$track->save();

		$trip->json = '';
		$trip->save();
	}

	function actionDeletetrack()
	{
		$id = intval(App::getParam('id'));

		$track = Tracks::model()->getByPK($id);
		if ($track->isEmpty()) {
			App::error404();
		}

		$trip = Trips::model()->getByPK($track->tripId);
		if ($trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$track->delete($id);

		$trip->json = '';
		$trip->save();

		App::redirect('/tracks/edit/id/' . $trip->id);
	}

	function actionMergetrack()
	{
		$id = intval(App::getParam('id'));

		$track = Tracks::model()->getByPK($id);
		if ($track->isEmpty()) {
			App::error404();
		}

		$trip = Trips::model()->getByPK($track->tripId);
		if ($trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$takeIt = $mergetId = $merget = FALSE;
		$tracks = Tracks::model()->getAll(array('where' => '`tripId` = ?'), array($trip->id));
		foreach ($tracks as $key => $value) {
			if ($takeIt) {
				$mergetId = $key;
				$merget = $value;
				break;
			}

			if ($key == $id) {
				$takeIt = TRUE;
			}
		}

		if (!$mergetId) {
			App::error404();
		}

		$track->name = $track->name . '; ' . $merget->name;
		$track->distance = $track->distance + $merget->distance;
		if ($merget->description) {
			$track->description = $track->description . '; ' . $merget->description;
		}
		$track->save();

		App::DB()->query("UPDATE `trackPoints` SET `trackId` = $id WHERE `trackId` = $mergetId");
		$track->delete($mergetId);

		$trip->json = '';
		$trip->save();

		App::redirect('/tracks/edit/id/' . $trip->id);
	}

	function actionWaypoint()
	{
		$id = intval(App::getParam('id'));

		$waypoint = WayPoints::model()->getByPK($id);
		if (empty($_POST['name']) || $waypoint->isEmpty()) {
			App::error404();
		}

		$trip = Trips::model()->getByPK($waypoint->tripId);
		if ($trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$waypoint->name = trim($_POST['name']);
		$waypoint->description = trim($_POST['description']);
		$waypoint->save();
	}

	function actionDeletewaypoint()
	{
		$id = intval(App::getParam('id'));

		$waypoint = WayPoints::model()->getByPK($id);
		if ($waypoint->isEmpty()) {
			App::error404();
		}

		$trip = Trips::model()->getByPK($waypoint->tripId);
		if ($trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$waypoint->delete($id);

		$trip->json = '';
		$trip->save();

		App::redirect('/tracks/edit/id/' . $trip->id);
	}

	function actionGettrackdetails()
	{
		$id = intval(App::getParam('id'));

		$track = Tracks::model()->getByPK($id);
		if ($track->isEmpty()) {
			App::error404();
		}

		$trip = Trips::model()->getByPK($track->tripId);
		if ($trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$trackpoints = TrackPoints::model()->getAll(array('where' => '`trackId` = ' . $track->id, 'asArray' => TRUE, 'noKeys' => TRUE, 'order' => '`id`'));

		self::renderPartial('json.php', $trackpoints);
	}

	function actionCut()
	{
		$id = intval(App::getParam('id'));

		$trackPoint = TrackPoints::model()->getByPK($id);
		if ($trackPoint->isEmpty()) {
			App::error404();
		}

		$track = Tracks::model()->getByPK($trackPoint->trackId);
		if ($track->isEmpty()) {
			App::error404();
		}

		$trip = Trips::model()->getByPK($track->tripId);
		if ($trip->isEmpty() || $trip->owner != App::$user->id) {
			App::error404();
		}

		$tracks = Tracks::model()->getAll(array('where' => '`tripId` = ?'), array($trip->id));
		foreach ($tracks as $trk) {
			$newTrack = Tracks::model()->add(
					array(
						'tripId' => $trip->id,
						'name' => $trk->name,
						'distance' => $trk->distance,
						'description' => $trk->description,
					)
			);


			if ($trk->id == $track->id) {
				$trackPoints = TrackPoints::model()->getAll(array('where'=>'`trackId` = ? AND `id` <= ?', 'order' => 'id'), array($trk->id, $trackPoint->id));
				foreach ($trackPoints as $tpt) {
					TrackPoints::model()->add(
							array(
								'trackId' => $newTrack->id,
								'lat' => floatval($tpt->lat),
								'lng' => floatval($tpt->lng),
								'alt' => $tpt->alt,
								'createdAt' => $tpt->createdAt,
								'speed' => $tpt->speed,
							)
						);

				}

				$newTrack = Tracks::model()->add(
					array(
						'tripId' => $trip->id,
						'name' => $trk->name . ' / #2',
						'distance' => $trk->distance,
						'description' => $trk->description,
					)
				);

				$trackPoints = TrackPoints::model()->getAll(array('where'=>'`trackId` = ? AND `id` >= ?', 'order' => 'id'), array($trk->id, $trackPoint->id));
				foreach ($trackPoints as $tpt) {
					TrackPoints::model()->add(
							array(
								'trackId' => $newTrack->id,
								'lat' => floatval($tpt->lat),
								'lng' => floatval($tpt->lng),
								'alt' => $tpt->alt,
								'createdAt' => $tpt->createdAt,
								'speed' => $tpt->speed,
							)
						);
				}
			} else {
				$trackPoints = TrackPoints::model()->getAll(array('where'=>'`trackId` = ?', 'order' => 'id'), array($trk->id));
				foreach ($trackPoints as $tpt) {
					TrackPoints::model()->add(
							array(
								'trackId' => $newTrack->id,
								'lat' => floatval($tpt->lat),
								'lng' => floatval($tpt->lng),
								'alt' => $tpt->alt,
								'createdAt' => $tpt->createdAt,
								'speed' => $tpt->speed,
							)
						);
				}
			}

			App::DB()->query("DELETE FROM `trackPoints` WHERE `trackId` = ".$trk->id);
			$trk->delete($trk->id);
		}

		$trip->json = '';
		$trip->calculateDist();
		$trip->save();

		App::redirect('/tracks/edit/id/' . $trip->id);
	}

}
