<?php defined('SYSPATH') or die('No direct script access.');

class Model_Message extends ORM
{
        // No Problem
        protected $_belongs_to = array('user' => array('model' => 'user', 'foreign_key' => 'send_helper'),
                                       'sale' => array('model' => 'sale', 'foreign_key' => 'sale_id'));
        

        public function __construct(){
            parent::__construct();
        }
        
        public function getInboxMessages(){
            
            $me = Auth::instance()->get_user()->id;
            $messages = ORM::factory("message") ->where('getter_id','=',$me)
                                                ->where('last_message','=',true)
                                                ->order_by('read_getter', 'asc')
                                                ->find_all()->as_array();
            
            return $messages;
        }
        
        public function getSentMessages(){
            
            $me = Auth::instance()->get_user()->id;
            $messages = ORM::factory("message") ->where('sender_id','=',$me)
                                                ->where('last_message','=',true)
                                                ->find_all()->as_array();
            
            return $messages;
        }
        
        public function getInboxGroupMessages($group_id){
            
            $me = Auth::instance()->get_user()->id;
            $reading =  ORM::factory("message") ->where('getter_id','=',$me)
                                                ->where('group_id','=',$group_id)
                                                ->where('last_message','=',true)
                                                ->order_by('time', 'desc')
                                                ->find();
            
           
            $messages = ORM::factory("message") ->where('group_id','=',$group_id)
                                                ->find_all();
                                                
            $message  = ORM::factory("message") ->where('getter_id','=',$me)
                                                ->where('group_id','=',$group_id)
                                                ->where('last_message','=',true)
                                                ->find();
            if($message->id == 0){
                return $message;
            }
            else{
                $reading->read_getter = true;
                $reading->save();
                return $messages;
            }
        }
        
        public function getSentGroupMessages($group_id){
            
            $me = Auth::instance()->get_user()->id;
            
            $messages = ORM::factory("message") ->where('group_id','=',$group_id)
                                                ->order_by('time', 'desc')
                                                ->find_all();
                                                
            $message  = ORM::factory("message") ->where('sender_id','=',$me)
                                                ->where('group_id','=',$group_id)
                                                ->where('last_message','=',true)
                                                ->find();
            if($message->id == 0){
                return $message;
            }
            else
                return $messages;
        }
        
        public function sendANewMessage($sale_id, $group_id, $getter_id, $messageset){
            
            $me = Auth::instance()->get_user()->id;
	    $last_message = ORM::factory("message") ->where('group_id','=',$group_id)
                                                    ->where('last_message','=',true)
                                                    ->where('send_helper','=',$me)
                                                    ->find();
            
            if($last_message->id){
		
                $last_message->last_message = false;
                $last_message->save();
            }
            $me                         = Auth::instance()->get_user()->id;
	    $message 			= ORM::factory('message');                                                                 
            $message->message 		= $messageset;
            $message->time 		= gen::now();
            $message->sender_id 	= $me;
            $message->read_getter	= false;
            $message->send_helper	= $me;
            $message->sale_id		= $sale_id;
            $message->getter_id		= $getter_id;
            $message->get_helper	= $getter_id;
	    $message->group_id 		= $group_id;
	    $message->last_message	= true;
            $message->save();
        }
        
        public function getNumberOfNewMessages(){
            
            $me = Auth::instance()->get_user()->id;
            $numOfMsg = ORM::factory("message") ->where('getter_id','=',$me)
                                                ->where('read_getter','=',false)
                                                ->where('last_message','=',true)
                                                ->count_all();
                                                
            Session::instance()->set("newMsg", $numOfMsg);
            return $numOfMsg; 
        }
        
        public function getIdToSendMessage($group_id){
            
            $me = Auth::instance()->get_user()->id;
            $message  = ORM::factory("message") ->where('group_id','=',$group_id)
                                                ->where('last_message','=',true)
                                                ->find();
            $getter = '';
            if($message->send_helper == $me."")
                $getter = $message->get_helper;
            else
                $getter = $message->send_helper;
            
            return $getter; 
        }
        
        public function deleteInboxMessage($group_id){
            
            $me = Auth::instance()->get_user()->id;
            $messages   = ORM::factory("message")   ->where('getter_id','=',$me)
                                                    ->where('group_id','=',$group_id)
                                                    ->find_all();
                                                    
            $message  = ORM::factory("message") ->where('getter_id','=',$me)
                                                ->where('group_id','=',$group_id)
                                                ->where('last_message','=',true)
                                                ->find();
            
            foreach($messages as $message){
                $message->getter_id = 0;
                $message->save();
            }
            
            
            if($message->id != 0)
                $this->deleteSuccess($group_id);
        }
        
        public function deleteSentMessage($group_id){
            
            $me = Auth::instance()->get_user()->id;
            $messages   = ORM::factory("message")   ->where('sender_id','=',$me)
                                                    ->where('group_id','=',$group_id)
                                                    ->find_all();
            
            $message  = ORM::factory("message") ->where('sender_id','=',$me)
                                                ->where('group_id','=',$group_id)
                                                ->where('last_message','=',true)
                                                ->find();
            
            foreach($messages as $message){
                $message->sender_id = 0;
                $message->save();
            }
            
            
            if($message->id != 0)
                $this->deleteSuccess($group_id);
        }
        
        public function deleteSuccess($group_id){
            
            $delete_success = 1;
            
            $messages   = ORM::factory("message")   ->where('group_id','=',$group_id)
                                                    ->find_all();
            
            foreach($messages as $message){
                
                if(($message->sender_id != 0) || ($message->getter_id != 0)){
                    $delete_success = 0;
                    break;
                }
            }
            
            if($delete_success == 1)
                ORM::factory("message") ->where('group_id','=',$group_id) ->delete_all();
        }
}