<?php 

/**
 * 
 */
class review
{
	public $send_notice = NEVER;
	public $close_reviews = false;

	function __construct($obj)
	{
		$this->reviews = NULL;
		$this->post = new post;
		$this->user = new user;
		$this->session = Session::getInstance();
		$this->sorting = new sorting;
		$this->set_obj($obj);
		$this->people = array();
		$this->dictionary = new stdClass();
		$this->dictionary->label = "Review";
		$this->dictionary->label_plu = "Reviews";
		$this->dictionary->label_tag = "h2";
		$this->dictionary->placeholder = "Please say something about your rating...";
		$this->dictionary->submit_text = "Submit Review";
		$this->dictionary->rating = "Rate Us:";
		$this->dictionary->notice_subject = "You Have a New Response to Your Review";
		$this->appearance = new stdClass();
		$this->appearance->editor = "wyswyg-editor";
		$this->appearance->rating_size = 5;
	}

	function set_obj($obj) {
		$this->obj = $obj;
	}

	function get_obj() {
		return $this->obj;
	}

	function get_review_args() {
		return $this->args;
	}

	function set_review_args($options) {

		$user_id = isset($options['user_id']) ? $options['user_id'] : $this->session->id;

		$default = [
			'rating' => 0,
			'type' => 'review',
			'obj' => $this->obj,
			'status' => 'active',
			'user_id' => $user_id
		];

		$options = array_merge($default, $options);

		$this->args = apply_filters("after_review_args_".$this->key, $options, $this);
	}

	function new_review($args) {
		// $notice = get_fullname($args['user_id']) . " posted a new review in a conversation you are following";
		// $this->send_notice($notice, $args['user_id']);
		$res = $this->post->add_post($args);
		if($res && isset($this->key)) {
			do_action("after_review_".$this->key, $this);
		}
		return $res;
	}

	function set_reviews() {
		$this->reviews = $this->post->get_post_by_meta_data(['type' => 'review', 'obj' => $this->obj, 'status' => 'active'], 'LIKE', false, 0, 0, 'post_id', 'DESC');
	}

	function send_notice($notice, $sender_id) {
		if($this->send_notice === "NEVER") return false;

		// $this->set_people_in_conversation();

		// if($this->send_notice === "MODERATE") {
		// 	$list = $this->moderate_send_notice_list();
		// } else $list = $this->people;
		
		// unset($list[$sender_id]);
		// if(_count($list) && is_array($list)) {
		// 	inform_users($list, $notice, $this->dictionary->notice_subject, false, SMS, EMAIL, NOTIFICATION);
		// }
	}

	function average_rating() {
		if($this->reviews === NULL) {
			$this->set_reviews();
		}
		$total = $avg = 0;
		if($count = _count($this->reviews)) {
			foreach($this->reviews as $meta) {
				$total += $meta['rating'];
			}
			$avg = $total/$count;
			$avg = round($avg * 2) / 2;
		}
		return $avg;
	}

	function get_reviews() {
		return $this->reviews;
	}

	function sort_reviews() {
		$this->sorting->sort_by_meta_value($this->reviews, 'posted_on', "ASC");
	}

	function display_review() {
		$count = _count($this->reviews);
		$html = "<link rel=\"stylesheet\" href=\"app/assets/css/review.css?ver=".VERSION."\">";
		$html .= "<div class=\"review-box-heading\">";
		$html .= "<em class=\"pull-right\" id=\"review-count-".$this->obj."\">".$count." ";
		$html .= ($count > 1) ? $this->dictionary->label_plu : $this->dictionary->label;
		$html .= "</em><".$this->dictionary->label_tag." style=\"opacity: 0;\">".$this->dictionary->label_plu."</".$this->dictionary->label_tag."></div>";
		$html .= "<div id=\"review-box-".$this->obj."\" class=\"box-footer box-reviews\">";
		$html .= $this->review_rows();
		$html .= "</div>";
		return $html;
	}

	function review_rows() {
		ob_start();
		if(_count($this->reviews) && is_array($this->reviews)) {
			foreach ($this->reviews as $key => $review) {
				$diff = time() - $review['posted_on'];
				$user_meta = $this->user->get_user_meta($review['user_id']);
				?>
				<div id="review-<?php echo $review['id'] ?>" class="box-review">
					<img class="img-circle img-sm" src="<?php echo get_user_image($user_meta) ?>" alt="User Image">
					<div class="review-text">
						<span class="username"><?php echo get_fullname($user_meta, true) ?>
							<span class="text-muted pull-right">
							<?php 
								if($diff > 86400) { 
									echo date(_TIME_FORMAT_." "._DATE_FORMAT_, $review['posted_on']);
								} else {
									echo _time_elapsed_string($diff);
								}
							?>
							</span>
						</span>
					<?php echo $this->rating_display("rating-display-".$review['id'], $review['rating'])." ".$review['review'] ?>
					</div>
				</div>
				<?php
			}
		}
		$template = ob_get_contents();
		ob_get_clean();
		return $template;
	}

	function rating_display($item_id, $rate = 0) {
		$html = "<link rel=\"stylesheet\" href=\"app/assets/css/rating-style.css?ver=".VERSION."\">";
		$html .= "<div id=\"half-stars-display\"><div class=\"rating-group\">";
		$html .= "<input disabled=\"\" class=\"rating__input rating__input--none\" name=\"rating_".$item_id."\" id=\"".$item_id."-rating-0\" value=\"0\" type=\"radio\" ";
		$html .= $rate == 0 ? "checked=\"\"" : "";
		$html .= "><label aria-label=\"0 stars\" class=\"rating__label\" for=\"".$this->obj."-rating-0\">&nbsp;</label>
		        <label aria-label=\"0.5 stars\" class=\"rating__label rating__label--half\" for=\"".$item_id."-rating-5\"><i class=\"rating__icon rating__icon--star fa fa-star-half\"></i></label>
		        <input disabled=\"\" class=\"rating__input\" name=\"rating_".$item_id."\" id=\"".$item_id."-rating-5\" value=\"0.5\" type=\"radio\" ";
		$html .= $rate == 0.5 ? "checked=\"\"" : "";
		$html .= ">";
		for ($i=1; $i < ($this->appearance->rating_size + 1); $i++) {
			$h = $i + 0.5;
			$i10 = $i * 10;
			$h10 = $h * 10;
			$id = $item_id."-rating-".$i10;
			$idh = $item_id."-rating-".$h10;
			$html .= "<label aria-label=\"".$i." star\" class=\"rating__label\" for=\"".$id."\"><i class=\"rating__icon rating__icon--star fa fa-star\"></i></label>
	        <input disabled=\"\" class=\"rating__input\" name=\"rating_".$item_id."\" id=\"".$id."\" value=\"".$i."\" type=\"radio\" ";
	        $html .= $rate == $i ? "checked=\"\"" : "";
	        $html .= ">";
	        if($i < $this->appearance->rating_size) {
	        	$html .= "<label aria-label=\"".$h." stars\" class=\"rating__label rating__label--half\" for=\"".$idh."\"><i class=\"rating__icon rating__icon--star fa fa-star-half\"></i></label>
	        	<input disabled=\"\" class=\"rating__input\" name=\"rating_".$item_id."\" id=\"".$idh."\" value=\"".$h."\" type=\"radio\" ";
	        	$html .= $rate == $h ? "checked=\"\"" : "";
	        	$html .= ">";
	        }
	        
		}
		$html .= "</div></div>";
		return $html;
	}

	function rating_form() {
		ob_start();
		?>
		<link rel="stylesheet" href="app/assets/css/rating-style.css?ver=<?php echo VERSION ?>">
		<div id="half-stars-example">
			<div class="rating-group">
				<span class="pt-2"><?php echo $this->dictionary->rating ?></span>
				<input class="rating__input rating__input--none" name="rating" id="<?php echo $this->obj ?>-rating-0" value="0" type="radio" checked>
			    <label aria-label="0 stars" class="rating__label" for="<?php echo $this->obj ?>-rating-0">&nbsp;</label>
			    <label aria-label="0.5 stars" class="rating__label rating__label--half" for="<?php echo $this->obj ?>-rating-5"><i class="rating__icon rating__icon--star fa fa-star-half"></i></label>
			    <input class="rating__input" name="rating" id="<?php echo $this->obj ?>-rating-5" value="0.5" type="radio">
			<?php 
				for ($i=1; $i < ($this->appearance->rating_size + 1); $i++) {
					$h = $i + 0.5;
					$i10 = $i * 10;
					$h10 = $h * 10;
					$id = $this->obj."-rating-".$i10;
					$idh = $this->obj."-rating-".$h10;
				?>
					<label aria-label="<?php echo $i ?> star" class="rating__label" for="<?php echo $id ?>">
						<i class="rating__icon rating__icon--star fa fa-star"></i>
					</label>
			        <input class="rating__input" name="rating" id="<?php echo $id ?>" value="<?php echo $i ?>" type="radio">
			        <?php if($i < $this->appearance->rating_size) : ?>
			        	<label aria-label="<?php echo $h ?> stars" class="rating__label rating__label--half" for="<?php echo $idh ?>">
			        		<i class="rating__icon rating__icon--star fa fa-star-half"></i>
			        	</label>
			        	<input class="rating__input" name="rating" id="<?php echo $idh ?>" value="<?php echo $h ?>" type="radio">
			        <?php endif; ?>
				<?php } ?>
			</div>
		</div>
		<?php
		$template = ob_get_contents();
		ob_get_clean();
		return $template;
	}

	function review_form() {
		if($this->close_reviews) return "<em>reviews has been closed</em>";
		global $post_meta;
		add_page_javascript('app/assets/js/page-scripts/review.js');
		add_page_javascript('app/assets/js/page-scripts/submit.js');
		$html .= "<div class=\"box-footer\"><form action=\"app/includes/ajax.php\" method=\"post\" id=\"review-form-";
		$html .= $this->obj;
		$html .= "\" data-callback=\"refreshReview\" data-clearform=1>";
		$html .= apply_filters("review_rating_form", "rating", $this);
		$html .= "<img class=\"img-responsive img-circle img-sm\" src=\"";
		$html .= get_user_image($this->session->id);
		$html .= "\" alt=\"Alt Text\">";
		$html .= "<div class=\"img-push\">";
		$html .= "<textarea id=\"review-input-".$this->obj."\" name=\"review\" type=\"text\" placeholder=\"".$this->dictionary->placeholder."\" class=\"form-control input-sm ".$this->appearance->editor."\" required></textarea>";
		$html .= "<input type=\"hidden\" id=\"action-".$this->obj."\" name=\"action\" value=\"new_review\">";
		if(isset($this->key)) {
			$html .= "<input type=\"hidden\" id=\"key-".$this->obj."\" name=\"key\" value=\"".$this->key."\">";
		}
		$html .= "<input type=\"hidden\" id=\"obj-".$this->obj."\" name=\"obj\" value=\"".$this->obj."\">";
		$html .= "<input type=\"hidden\" id=\"label_plu-".$this->obj."\" name=\"label_plu\" value=\"".$this->dictionary->label_plu."\">";
		$html .= "<input type=\"hidden\" id=\"label-".$this->obj."\" name=\"label\" value=\"".$this->dictionary->label."\">";
		$html .= "<button data-id=\"".$this->obj."\" data-label=\"".$this->dictionary->label."\" data-label-plu=\"".$this->dictionary->label_plu."\" id=\"submit-review-btn-".$this->obj."\" class=\"submit-review-btn btn btn-primary submit btn-sm mt-2\">".$this->dictionary->submit_text."</button>";
		$html .= "</div></form></div>";
		return $html;
	}
}