<?php 
	/**
	* 
	*/
	class Model_notification extends CI_Model
	{
        private $from = '';
        private $to = '';
        private $type = '';
        private $heading = '';
        private $message = '';
        private $icon = '';
        private $image = '';
        private $insert_id = null;
        private $is_email_sent = false;

        /**
         * setter and getter
         */
    
        public function setFrom($from)
        {
           $this->from = $from;
           return $this;
        }

        public function setTo($to)
        {
            $this->to = $to;
            return $this;
        }

        public function setType($type)
        {
            $this->type = $type;
            return $this;
        }

        public function setHeading($heading)
        {
            $this->heading = $heading;
            return $this;
        }

        public function setMessage($message)
        {
            $this->message = $message;
            return $this;
        }

        public function setIcon($icon)
        {
            $this->icon = $icon;
            return $this;
        }

        public function setImage($image)
        {
            $this->image = $image;
            return $this;
        }

        public function setLink($link)
        {
            $this->link  = $link;
            return $this;
        }

        public function getInsertId()
        {
            return $this->insert_id;
        }

        public function isEmailSent()
        {
            return $this->is_email_sent;
        }

        public function notify($from = '', $to = '', $message = '', $icon = '',$image = '',$link = '')
        {

            (!empty($from))? $this->from = $from : null;
            (!empty($to))? $this->link = $link : null;
            (!empty($message))? $this->msessage = $message : null;
            (!empty($icon))? $this->$icon = $icon : null;
            (!empty($image))? $this->image = $image : null;
            (!empty($link))? $this->link = $link : null;

            $data = array
            (
                'user_id'   => $this->to,
                'icon'      => $this->icon,
                'type'      => $this->type,
                'heading'   => $this->heading,
                'message'   => $this->message,
                'image'     => $this->image,
                'link'      => $this->link,
                'created_by'=> $this->from,
            );

            $this->db->insert('application__notification', $data);
            $this->insert_id = $this->db->insert_id();
            return $this;
        }

        public function read($id,$user_id)
        {
            $data = array(
                'is_read'   => 1,
                'read_at'   => date('Y-m-d h:i:s')
            );
            $this->db->where('id',$id);
            $this->db->where('user_id',$user_id);
            $this->db->update('application__notification',$data);
        }

        public function fetchAll($id)
        {
            $where = array(
                'id'        => $id,
                'statue'    => 0,
                'is_read'   => 0,
            );
            $query = $this->db->get('application__notification');
            return ($query->num_rows() > 0)?$query->result:null;
        }

        public function getFirst($id)
        {
            $all = $this->fetchAll($id);
            return (!empty($all))?$all[0]:null;
        }

        public function getUserNotification($user_id)
        {
            $this->db->select('a.*,b.name');
            $this->db->where('a.user_id',$user_id);
            $this->db->where('a.is_read',0);
            $this->db->join('user b','b.id = a.created_by','left');
            $query = $this->db->get('application__notification a');
            return ($query->result())?$query->result():null;
        }

        public function getNotificationLink($id)
        {
           $this->db->select('a.link');
           $this->db->where('a.id',$id);
           $query = $this->db->get('application__notification a');
           return ($query->result())?$query->result()[0]->link:null;
        }
        
        /**
         * unfinished features
         */
        public function sendEmail()
        {
            /**
             * check email sent or not and then sent email
             */
            $this->is_email_sent = true;
            
            return $this->is_email_sent;
        }

	}
?>