<?php 

/**
 * 
 */
class comment
{
	public $send_notice = NEVER;
	public $close_comments = false;

	function __construct($obj)
	{
		$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 = "Comment";
		$this->dictionary->label_plu = "Comments";
		$this->dictionary->label_tag = "h2";
		$this->dictionary->submit_text = "Submit Comment";
		$this->dictionary->notice_subject = "You Have a New Response to Your Comment";
		$this->appearance = new stdClass();
		$this->appearance->editor = "wyswyg-editor";
		$this->notice_action = false;
	}

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

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

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

	function set_comment_args($options) {

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

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

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

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

	function new_comment($args) {
		$notice = get_fullname($args['user_id']) . " posted a new comment 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_comment_".$this->key, $this);
		}
		return $res;
	}

	function set_comments() {
		$this->comments = $this->post->get_post_by_meta_data(['type' => 'comment', 'obj' => $this->obj, 'status' => 'active']);
	}

	function set_people_in_conversation() {
		if(_count($this->comments) && is_array(_count($this->comments))) {
			foreach ($this->comments as $comment_meta) {
				if(!isset($this->people[$comment_meta['user_id']])) {
					$this->people[$comment_meta['user_id']] = $this->user->get_user_meta($comment_meta['user_id']);
				}
			}
		}
	}

	function get_people_in_conversation() {
		$this->set_people_in_conversation();
		return $this->people;
	}

	function moderate_send_notice_list($interval = 300) {
		$this->mod = $this->people;
		if(_count($this->people) && is_array($this->people)) {
			foreach ($this->people as $user_id => $user_meta) {
				set_time_limit(200);
				if(isset($user_meta['login_meta'])) {
					$login_meta = unserialize($user_meta['login_meta']);
					if($login_meta['state'] === 'online') {
						if(time() - $login_meta['last_login'] < $interval) {
							unset($this->mod[$user_id]);
						}
					}
				}
			}
		}
		return $this->mod;
	}

	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, $this->notice_action, SMS, EMAIL, NOTIFICATION);
		}
	}

	function get_comments() {
		return $this->comments;
	}

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

	function display_comment() {
		$count = _count($this->comments);
		$html = "<link rel=\"stylesheet\" href=\"".get_option("home_url")."app/assets/css/comment.css?ver=".VERSION."\">";
		$html .= "<div class=\"comment-box-heading\">";
		$html .= "<em class=\"pull-right\" id=\"comment-count-".$this->obj."\">".$count." ";
		$html .= ($count > 1) ? $this->dictionary->label_plu : $this->dictionary->label;
		$html .= "</em><".$this->dictionary->label_tag.">".$this->dictionary->label_plu."</".$this->dictionary->label_tag."></div>";
		$html .= "<div id=\"comment-box-".$this->obj."\" class=\"box-footer box-comments\">";
		if(_count($this->comments) && is_array($this->comments)) {
			foreach ($this->comments as $key => $comment) {
				$diff = time() - $comment['posted_on'];
				$user_meta = $this->user->get_user_meta($comment['user_id']);
				$html .= "<div";
				$html .= " id=\"comment-".$comment['id']."\"";
				$html .= " class=\"box-comment\"><img class=\"img-circle img-sm\" src=\"";
				$html .= get_user_image($user_meta);
				$html .= "\" alt=\"User Image\"><div class=\"comment-text\"><span class=\"username\">";
				$html .= get_fullname($user_meta, true);
				$html .= "<span class=\"text-muted pull-right\">";
				if($diff > 86400) {
					$html .= date(_TIME_FORMAT_." "._DATE_FORMAT_, $comment['posted_on']);
				} else {
					$html .= _time_elapsed_string($diff);
				}
				
				$html .= "</span></span>";
				$html .= $comment['comment'];
				$html .= "</div></div>";
			}
		}
		$html .= "</div>";
		return $html;
	}

	function comment_form() {
		if($this->close_comments) return "<em>Comments has been closed</em>";
		if(!isset($this->session->id)) return;
		global $post_meta;
		add_page_javascript('app/assets/Trumbowyg-master/dist/trumbowyg.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/colors/trumbowyg.colors.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/giphy/trumbowyg.giphy.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/base64/trumbowyg.base64.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/noembed/trumbowyg.noembed.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/pasteimage/trumbowyg.pasteimage.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/preformatted/trumbowyg.preformatted.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/plugins/upload/trumbowyg.upload.min.js');
		add_page_javascript('app/assets/Trumbowyg-master/dist/trumbowyg.init.js');
		$html = "<link rel=\"stylesheet\" type=\"text/css\" href=\"app/assets/Trumbowyg-master/dist/ui/trumbowyg.min.css\">";
		$html .= "<div class=\"box-footer\"><form action=\"".get_option("home_url")."app/includes/ajax.php\" method=\"post\" id=\"comment-form-";
		$html .= $this->obj;
		$html .= "\" data-callback=\"refreshComment\" data-clearform=1><img class=\"img-responsive img-circle img-sm\" src=\"";
		$html .= get_user_image($this->session->id);
		$html .= "\" alt=\"Alt Text\"><div class=\"img-push\">";
		$html .= "<textarea id=\"comment-input-".$this->obj."\" name=\"comment\" type=\"text\" class=\"form-control input-sm ".$this->appearance->editor."\"></textarea>";
		$html .= "<input type=\"hidden\" id=\"action-".$this->obj."\" name=\"action\" value=\"new_comment\">";
		if(isset($this->key)) {
			$html .= "<input type=\"hidden\" id=\"key-".$this->obj."\" name=\"key\" value=\"".$this->key."\">";
		}
		$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-comment-btn-".$this->obj."\" class=\"submit-comment-btn btn btn-primary btn-sm mt-2\">".$this->dictionary->submit_text."</button>";
		$html .= "</div></form></div>";
		return $html;
	}
}