<?php
/**
 * class slide_form
 *
 * Description for class slide_form
 *
 * @author:
*/
namespace form;

use core\form;

class slide_form extends form {
	public $existsUsers = false;
	public $existsReactions = false;

	public function __construct($data = array()) {
		parent::__construct();
		
		if(isset($data["users"]) && (int)$data["users"] > 0) {
			$this->existsUsers = true;
		}

		if(isset($data["usersReactions"]) && (int)$data["usersReactions"] > 0) {
			$this->existsReactions = true;
		}

		$this->setData($data);
	}

	public function setData($data) {
		if(isset($data["config"])) {
			$config = $data["config"];
			unset($data["config"]);

			//$config = json_decode($config, true);

			if(!is_array($config)) {
				$config = array();
			}

			$data = array_merge($data, $config);
		}

		if(!isset($data["options"]) || count($data["options"]) == 0) {
			$data["options"] = array(
				0 => array("id" => "", "text" => "", "correct" => "0", "image" => ""),
				1 => array("id" => "", "text" => "", "correct" => "0", "image" => ""),
				2 => array("id" => "", "text" => "", "correct" => "0", "image" => "")
				);
		}

		$this->setInitData($data);
	}

	protected function __save($board_id, $name, $image, $video, $config, $reactions, $options = null, $optionsCorrect = null, $optionsImages = null) {
		global $__database;

		$config_json = json_encode($config);
		$reactions_json = json_encode($reactions);

		try {
			$resultsExists = false;

			$query = $__database->prepare("SELECT COUNT(`result_id`) AS `numResults` FROM `results` WHERE `board_id`=:board_id");
			$query->bindParam(":board_id", $board_id);
			$query->execute();
			if($record = $query->fetch()) {
				if($record["numResults"] > 0) {
					$resultsExists = true;
				}
			}

			$query = $__database->prepare("UPDATE `boards` SET `date_updated`=NOW(), `name`=:name, `image`=:image, `video`=:video, `config`=:config, `reactions`=:reactions WHERE `board_id`=:board_id");
			$query->bindParam(":name", $name);
			$query->bindParam(":image", $image);
			$query->bindParam(":video", $video);
			$query->bindParam(":config", $config_json);
			$query->bindParam(":reactions", $reactions_json);
			$query->bindParam(":board_id", $board_id);
			$query->execute();

			if(!$resultsExists) {
				$query = $__database->prepare("DELETE FROM `boards_options` WHERE `board_id`=:board_id");
				$query->bindParam(":board_id", $board_id);
				$query->execute();

				if(!empty($options) && is_array($options)) {
					if(count($options) > 0) {
						$order = 1;

						foreach($options as $key => $option) {
							if($key < 0 || empty($option)) {
								continue;
							}

							$image = "";
							if(isset($optionsImages[$key]) && !empty($optionsImages[$key])) {
								$image = $optionsImages[$key];
							}

							$correct = "0";
							if(isset($optionsCorrect[$key]) && $optionsCorrect[$key] == "1") {
								$correct = "1";
							}

							$query = $__database->prepare("INSERT INTO `boards_options` (`option_id`, `board_id`, `text`, `correct`, `image`, `order`) VALUES (UUID(), :board_id, :text, :correct, :image, :order)");
							$query->bindParam(":board_id", $board_id);
							$query->bindParam(":text", $option);
							$query->bindParam(":correct", $correct);
							$query->bindParam(":image", $image);
							$query->bindParam(":order", $order);
							$query->execute();

							$order++;
						}
					}
				}
			}

			return true;
		} catch(PDOException $error) {
			
		}

		return false;
	}
}
?>