<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Chat extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->library(['ci_pusher','session']);
        $this->load->model('chat_model');
		error_reporting(0);
		//session_destroy();
    }
	public function index()
	{
		$data['active_members'] = $this->chat_model->get_all_online('idle');
		$data['myprofile'] = $this->chat_model->get_by_id($this->session->userdata('chatter_id'));
		$this->load->view('chat',['data'=>$data]);
	}
	public function fetch_message_on_reload(){
		$userid = $_POST['userid'];
		//echo 
		$chatdata = $this->chat_model->fetch_message($userid);
		//echo "<pre>";echo $userid;print_r($chatdata);
		//die;
			$html = '';
		foreach($chatdata as $chat){
		
			if($userid!=$chat['userid']){
				$html .= '<div id="cm-msg" class="chat-msg user">'; 
				$html .= ' <span class="msg-avatar">'; 
				$html .= '<img src="'.base_url('assets/web/char-icons/icon2.png').'">'; 
				$html .= '</span>'; 
				$html .= '<div class="chat-body1 clearfix">'; 
				$html .= '<p class="cm-msg-text">'.$chat['message'].'</p>';
				$html .= '<div class="chat_time">'.$chat['time'].'</div>'; 
				$html .= '</div>'; 
				$html .= '</div>'; 			
			}else{
				$html .= '<div id="cm-msg" class="chat-msg user">'; 
				$html .= ' <span class="msg-avatar">'; 
				$html .= '<img src="'.base_url('assets/web/char-icons/f125_11-512.png').'" class="chat_img_right">'; 
				$html .= '</span>'; 
				$html .= '<div class="chat-body1 chat_right clearfix">'; 
				$html .= '<p class="cm-msg-text cm-msg-text-me">'.$chat['message'].'</p>'; 
				$html .= '<div class="chat_time">'.$chat['time'].'</div>'; 
				$html .= '</div>'; 
				$html .= '</div>'; 
			}
			
		}
		echo $html;
		
		//echo json_encode($chatdata);
		//die;
	}
	public function logout(){
		$sess_id = $_POST['ses_id'];
		if($sess_id!=""){
			$this->session->unset_userdata('chatter_id');
			$this->session->unset_userdata('user_name');
		}else{
			$this->session->unset_userdata('chatter_id');
			$this->session->unset_userdata('user_name');
		}
		//echo "logout";die;
	
	}
	public function chatsend(){
		$pusher = $this->ci_pusher->get_pusher();
		$data['message'] = $_POST['message'];
		$data['date'] = date('H:i A');
		$data['id'] = $this->session->userdata('chatter_id');
		$data['username'] = $this->session->userdata('user_name');
		///echo "<pre>";print_r($data);die;
		$data1['message'] = $_POST['message'];
		$data1['time'] = date('H:i A');
		$data1['userid'] =   $this->session->userdata('chatter_id');
		$data1['username'] =  $this->session->userdata('user_name');
		//$this->db->query("insert into user_message set meassage=$data['message'],userid=$data['id'],time=$data['date'],username=$data['username']");
		$this->chat_model->insert_message($data1);
		$event = $pusher->trigger('chatglobal', 'my_event', $data);
		///session_destroy();
	}

	public function update_user(){
		$username = $_POST['username'];
		$email    = $_POST['email'];
		$password = $_POST['password'];
		if($password == ""){
			$data = array(
				'username' => $username,
				'email'    => $email,
			);
		}else{
			$data = array(
				'username' => $username,
				'email'    => $email,
				'password' => $password,
			);
		}
		$this->chat_model->update($this->session->userdata('chatter_id'),$data);
	}
	public function chatregister(){
		//$this->session->unset_userdata('chatter_id');
		//$this->session->unset_userdata('user_name');
		$data['email'] 			= $_POST['emailer'];
		$data['username'] 		= 'ananomous';
		$data['status'] 		= 'idle';
		if($data['email']!=""){
			$count = $this->chat_model->check_duplicate($data['email']);
			if($count==0){
				$userid = $this->chat_model->add_user($data);
				$getuserdata = $this->chat_model->get_by_id($userid);
				$this->session->set_userdata('chatter_id',$userid);
				$this->session->set_userdata('user_name',$getuserdata->email);
			}else{
				$get = $this->chat_model->check_email_exist($data['email']);
				$this->session->set_userdata('chatter_id',$get->id);
				$this->session->set_userdata('user_name',$get->email);
			}
			echo $this->session->userdata('chatter_id');
			die;
		}else{
			echo "error";
			die;
		}
		
	}
}
