<?php 

/**
 * 
 */
class visitor
{
	
	function __construct($app)
	{
		$this->app = $app;
		$this->query = new query;
		$this->columns = [
			'visitor_name',
			'visitor_id',
			'visitor_ip',
			'visit_page',
			'page_link',
			'request_time',
			'load_time',
			'user_agent',
			'referer',
			'status',
			'request_method'
		];
		$this->home_url = get_option("home_url");
	}

	function entry() {
		$this->visitor_name = ($this->app->user_meta) ? get_fullname($this->app->user_meta, true) : "Not Profiled";
		$this->visitor_id = ($this->app->user_meta) ? $this->app->user_meta['id'] : "";
		$this->visitor_ip = $_SERVER['REMOTE_ADDR'];
		// $this->visitor_ip = (connection_status() === 0 && !on_local_computer()) ? file_get_contents("http://ipinfo.io/ip/") : $_SERVER['REMOTE_ADDR'];
		$this->visit_page = $this->app->post_meta['title'];
		$this->page_link = $this->app->post_meta['permalink'];
		if(!empty($_GET)) {
			if(isset($_GET['action']) || isset($_GET['media']) || isset($_GET['ispopup'])) {
				return;
			}
			$this->page_link .= "?".http_build_query($_GET);
		}
		$this->request_time = $_SERVER['REQUEST_TIME'];
		$this->load_time = "";
		$this->user_agent = $_SERVER['HTTP_USER_AGENT'];
		$this->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Direct';
		$this->request_method = $_SERVER['REQUEST_METHOD'];
		$this->status = "Broken";
		$this->save();
	}

	function loaded() {
		$this->set_load_time();
		$this->status = "Success";
		$table = ($this->is_external()) ? 'externals' : 'visitors';
		if(isset($this->id)) {
			if($this->id) {
				$this->query->update( $table,
					array('status' => $this->status, 'load_time' => $this->load_time),
					array('id' => $this->id)
				);
			}
		}
	}

	function save() {
		$values = [
			$this->visitor_name,
			$this->visitor_id,
			$this->visitor_ip,
			$this->visit_page,
			$this->page_link,
			$this->request_time,
			$this->load_time,
			$this->user_agent,
			$this->referer,
			$this->status,
			$this->request_method
		];
		$table = ($this->is_external()) ? 'externals' : 'visitors';
		$this->id = $this->query->insert($table, $this->columns, $values);
		$sql = "DELETE FROM ".$table." WHERE id NOT IN (SELECT id FROM ( SELECT id FROM ".$table." ORDER BY id DESC LIMIT 2000) foo)";
		$this->query->query($sql);
	}

	function is_external() {
		if(($this->referer ?? "") == "") {
			$this->referer = "Direct access";
			return false;
		}
		if(strpos($this->referer, $this->home_url) !== false) {
			return false;
		} else return true;
	}

	function set_load_time() 
	{
		$this->load_time = time() - ($this->request_time ?? time());
	}

	function get_visits($from = false, $to = false, $user_id = false, $output = ARRAY_A, $table = 'visitors', $limit = 0, $offset = 0, $order_by = 'id', $order = 'DESC') 
	{
		$sql = "SELECT * FROM `".$table."`";
		if($from || $to || $user_id) {
			$sql .= " WHERE";
			if($from) {
				$sql .= " `request_time` > '".$from."' AND";
			}
			if($to) {
				$sql .= " `request_time` < '".$to."' AND";
			}
			if($user_id) {
				$sql .= " `visitor_id` = '".$user_id."' AND";
			}
			$sql = substr($sql, 0, -4);
		}
		$sql .= " ORDER BY `".$order_by."` ".$order;
		$sql .= ($limit && $limit > 0) ? " LIMIT ".$limit : "";
		$sql .= ($offset && $offset > 0) ? " OFFSET ".$offset : "";

		return $this->query->get_results( $sql, $output );
	}

	function last_activity($user_id) {
		$res = $this->get_visits(false, false, $user_id, OBJECT_K, 'visitors', 1, 0, 'request_time', 'DESC');
		if($res !== null) {
			return array_shift($res);
		} else return null;
	}
}