<?php
// namespace core;
// use core\controller as Controller,
// core\view as View;

/**
* Users Controller
* Controls the users processes and procedures
**/
class Users extends Controller{
	/** Property to use other object models... **/
	protected $u_model;
	protected $l_model;
	/**
	* Construct this object by extending the basic Controller class
	**/
	public function __construct(){
		parent::__construct();
		// $this->loadModel('users');
		Language::load();
		$this->u_model = new Users_Model();
		$this->l_model = new Login_Model();
	}

	/**
	* Index, default display lately added users using id's
	* Displaying only activated users :)
	**/
	public function index(){
		$data = $this->u_model->get_NewlyCreatedUsers(0,DISPLAY_USERS);
		if($data == NULL)
			Url::redirect('');
		$this->_view->rendercusttmpl('orn','header');
		if(Auth::handleCookedLogin()){
			$this->_view->render('listed/top');
			$srch = ['ugend' => Session::get("user_gender")];
			$this->_view->render('listed/srch',$srch);
			$this->_view->render('users/index',$data);
			$this->_view->render('listed/bottom');
		}else{
			$this->_view->render('guest/top');
			$this->_view->render('guest/srch');
			// $this->_view->render('login/ind');
			$this->_view->render('users/index',$data);
			$this->_view->render('guest/bottom');
		}
		$this->_view->rendercusttmpl('orn','footer');
	}

	/**
	* Get user properties using user id and if user is login enables chat,
	* if just receiving an POST for chat request updates it and returns chat's contents
	*/
	public function id($userId){
		/* check for conversation request always should be sent with text itself */
		$p_user_con = filter_var(@$_POST['user_con'], FILTER_SANITIZE_NUMBER_INT);
		$userId = filter_var(@$userId, FILTER_SANITIZE_NUMBER_INT);
		//$usedData = new stdClass();
		/* if i get just the POST no need to render just check... */
		if(!empty($p_user_con)){
			/* check for self posting prevention */
			if(Auth::handleCookedLogin() && (Session::get("user_id") != $userId)){
				$p_user_msg = @filter_var($_POST['user_msg'], FILTER_SANITIZE_STRING);
				/*  if new message delivered by user update db */
				if(!empty($p_user_msg)){
					$this->u_model->set_ConvMsg(Session::get("user_id"),$userId,$p_user_msg);
				}
				/* fetch any conversations */
				$sucsess = $this->u_model->get_Conv(Session::get("user_id"),$userId);
				/* check if we have any conversations and return the content */
				if($sucsess){
					$msg_construct = "";
					foreach($sucsess as $dataa){
						$msg_construct.= '<umsg>';
						foreach($dataa as $key => $value){
							$msg_construct .= '<ucon>'.$value.'</ucon>';
						}
						$msg_construct.= '</umsg>';
					}
					echo $msg_construct;
				}
				/* exit here because it was only a conversation request no rendering */
				return;
			}
		}
		$usedData = $this->u_model->get_User($userId);
		/* redirect if no users exists */
		if($usedData == NULL)
			Url::redirect('users');
		/* This is not just for logged in users to show conversation but prevent conversation of the user with himself */
		$usedData->logged_in = 1;
		/* user images as one string with combination "|(image_path)|(imagepath) and etc..." or false if empty or not exists */
		$u_images = $this->u_model->get_UserImages($userId);
		if($u_images){
			foreach($u_images as $holder){
				/* This check is for user if he set image private and the images are confirmed by administrator */
				if(!$holder->user_img_private && $holder->user_img_active)
					$usedData->user_img_paths .= "|".$holder->user_img_path;
			}
		}
		$this->_view->rendercusttmpl('orn','header');
		if(Auth::handleCookedLogin()){
			/* user should not conversation with himself ! */
			if(Session::get("user_id") != $userId){
				$sucsess = $this->u_model->get_Conv(Session::get("user_id"),$userId);
				/* check if we have any conversations and return the content */
				if($sucsess){
					$conv_construct = "";
					foreach($sucsess as $message){
						$conv_construct.= '<div class="inner-text">';
						if($message->user_one == $userId)
							$conv_construct.= '<span class="chat_left">';
						else
							$conv_construct.= '<span class="chat_right">';
						$conv_construct.= str_replace("\n",'</br>',$message->msg).'</span><span class="chat_time">'.$message->time.'</span>';
						$conv_construct.= '</div>';
					}
					/* check for conversation request always should be sent with text itself */
					$usedData->conversation = $conv_construct;
				}
			}else{
				$usedData->logged_in = 0;
			}
			$this->_view->render('listed/top');
			$srch = ['ugend' => Session::get("user_gender")];
			$this->_view->render('listed/srch',$srch);
			$this->_view->render('users/id',$usedData);
			$this->_view->render('listed/bottom');
		}else{
			$usedData->logged_in = 0;
			$this->_view->render('guest/top');
			$this->_view->render('guest/srch');
			$this->_view->render('users/id',$usedData);
			$this->_view->render('guest/bottom');
		}
		$this->_view->rendercusttmpl('orn','footer');
	}

	/**
	* Search, makes an user search with requests
	* renders the 'nothing' view event when no users meets criteria
	*/
	public function search($pageId = 0){
		$pageId = (int)filter_var($pageId, FILTER_SANITIZE_NUMBER_INT);
		/** arguments patter to filter are set here **/
		$filterArgs = array(
			'user_gender' => array('filter' => FILTER_VALIDATE_INT,
									'flags' => FILTER_REQUIRE_SCALAR, 
									'options' => array('min_range' => 1,'max_range' => 2)),
			'user_age_from' => array('filter' => FILTER_VALIDATE_INT,
									'flags' => FILTER_REQUIRE_SCALAR, 
									'options' => array('min_range' => 19,'max_range' => 98)),
			'user_age_to' => array('filter' => FILTER_VALIDATE_INT,
									'flags' => FILTER_REQUIRE_SCALAR, 
									'options' => array('min_range' => 19,'max_range' => 98)),
			'srch_dstrct' => array('filter' => FILTER_VALIDATE_INT,
									'flags' => FILTER_REQUIRE_SCALAR, 
									'options' => array('min_range' => 1,'max_range' => 7))
		);
		/** filter GET request against array with NULL flag enabled **/
		$getValues = filter_input_array(INPUT_GET, $filterArgs,false);
		if(!empty($getValues['user_gender']))
			$setValues['user_gender']=$getValues['user_gender'];
		if(!empty($getValues['user_age_from']))
			$setValues['user_age_from']=date((date("Y")-$getValues['user_age_from'])."-m-d");
		if(!empty($getValues['user_age_to']))
			$setValues['user_age_to']=date((date("Y")-$getValues['user_age_to'])."-m-d");
		if(!empty($getValues['srch_dstrct']))
			$setValues['user_district']=$getValues['srch_dstrct'];
		$is_cooked = Auth::handleCookedLogin();
		if(!empty($setValues))
			$data = $this->u_model->get_Search($pageId,DISPLAY_USERS,$setValues);
		else
			$data = $this->u_model->get_Users($pageId,DISPLAY_USERS);
		$data['add']['pid'] = $pageId;
		$this->_view->rendercusttmpl('orn','header');
		if($is_cooked){
			$srch = ['ugend' => Session::get("user_gender")];
			$this->_view->render('listed/top');
			$this->_view->render('listed/srch',$srch);
		}else{
			$this->_view->render('guest/top');
			$this->_view->render('guest/srch');
		}
		/** If no results found meet the requested criteria show nothing found page **/
		if($data['usr'] == NULL)
			$this->_view->render('users/nothing');
		else
			$this->_view->render('users/search',$data);
		if($is_cooked)
			$this->_view->render('listed/bottom');
		else
			$this->_view->render('guest/bottom');
		$this->_view->rendercusttmpl('orn','footer');
	}

	/**
	* Search, makes an user search with requests
	* renders the 'nothing' view event when no users meets criteria
	*/
	// public function search($pageId = 0){
		// $pageId = filter_var($pageId, FILTER_SANITIZE_NUMBER_INT);
		// // check if user is login and get his gender for search
		// $p_user_gender = filter_var(@$_GET['user_gender'], FILTER_SANITIZE_NUMBER_INT);
		// $p_user_age_from = filter_var(@$_GET['user_age_from'], FILTER_SANITIZE_NUMBER_FLOAT);
		// $p_user_age_to = filter_var(@$_GET['user_age_to'], FILTER_SANITIZE_NUMBER_FLOAT);
		// $is_cooked = Auth::handleCookedLogin();
		// $data = $this->u_model->get_Users($pageId,DISPLAY_USERS,$p_user_gender,$p_user_age_from,$p_user_age_to);
		// $data['add']['pid'] = $pageId;
		// $this->_view->rendercusttmpl('orn','header');
		// if($is_cooked){
			// $srch = ['ugend' => Session::get("user_gender")];
			// $this->_view->render('listed/top');
			// $this->_view->render('listed/srch',$srch);
		// }else{
			// $this->_view->render('guest/top');
			// $this->_view->render('guest/srch');
		// }
		// /** If no results found meet the requested criteria show nothing found page **/
		// if($data['usr'] == NULL)
			// $this->_view->render('users/nothing');
		// else
			// $this->_view->render('users/search',$data);
		// if($is_cooked)
			// $this->_view->render('listed/bottom');
		// else
			// $this->_view->render('guest/bottom');
		// $this->_view->rendercusttmpl('orn','footer');
	// }

}