<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Users_inbox_model extends CI_Model
{

    private $private_message_id;
    private $recipients;

    public function __construct()
    {
        parent::__construct();
    }

    public function update_status_auto_message($status_auto_messages)
    {
        $this->db->set('status_auto_messages', json_encode($status_auto_messages));
        $this->db->where('id', $this->session->id);
        $this->db->update('users');
    }

    /*** Gestion messages auto ***/
    public function get_auto_message($messageId, $dataselect='messages_auto.*')
    {
        $this->db->where('messages_auto.id', $messageId)->select($dataselect);

        return $this->db->get('messages_auto')->row();
    }

    public function get_auto_messages()
    {
        $this->db->select('messages_auto.id, messages_auto.type, messages_auto.instant_publish, messages_auto.periodicity, messages_auto.name, messages_auto.publish_date as last_publish_date');

        return $this->db->get('messages_auto')->result();
    }
    
    /*** Gestion messages utilisateur ***/

    public function get_inbox_messages($limit = 15, $offset = 0)
    {
        $this->db->select('messages.id,messages.message_object, messages_users.is_read as is_read, messages.last_publisher_id, UNIX_TIMESTAMP(messages.last_publish_date) as last_publish_date, messages.recipients, users.id as publisher_id, users.nickname as publisher_nickname');
        $this->db->where('messages_users.user_id', $this->session->id);
        $this->db->where('messages_users.state', 0);
        $this->db->from('messages_users');
        $this->db->join('messages', 'messages.id = messages_users.parent_message_id');
        $this->db->join('users', 'users.id = messages.last_publisher_id');
        $this->db->limit($limit, $offset);
        $this->db->order_by('last_publish_date', 'desc');
        return $this->db->get()->result();
    }

    public function get_recipient_nickname($userID)
    {
        $this->db->select('users.nickname');
        $this->db->where('users.id', $userID);

        return $this->db->get('users')->row()->nickname;
    }

    public function get_pm_count($userID) 
    {
		$this->db->where('user_id', $userID);
		$this->db->where('state', 0);
		
		return $this->db->get('messages_users')->num_rows();
    }
	
    public function get_recipients_list($usersID)
    {
        $this->db->select('users.id, users.nickname, users.settings, users.ignored');
        $this->db->where_in('users.id', $usersID);

        return $this->db->get('users')->result();
    }

    public function get_private_message($pmID)
    {
        $this->db->where('id', $pmID);

        return $this->db->get('messages')->row();
    }

    public function get_private_message_recipients($pmID)
    {
        $this->db->select('recipients');
        $this->db->where('id', $pmID);

        return $this->db->get('messages')->row()->recipients;
    }

    public function get_count_replies($pmID)
    {
        $this->db->select('id');
        $this->db->where('parent_message_id', $pmID);
        $this->db->where('state', 1);

        return $this->db->get('messages_replies')->num_rows();
    }

	public function getMessageDataFromReply($data, $replyID) 
	{
		$this->db->select($data);
		$this->db->where('id', $replyID);
		
		return $this->db->get('messages_replies')->row(); 
	}


    public function get_replies($pmID, $limit = 15, $offset = 0)
    {
        $this->db->select('messages_replies.id, messages_replies.message_body, UNIX_TIMESTAMP(messages_replies.publish_date) as publish_date , users.nickname AS publisher , users.last_activity_date AS publisher_activity , users.uploaded AS up , users.downloaded AS down , users.id AS publisher_id  , users.rank AS publisher_rank , users.avatar AS avatar');
        $this->db->where('messages_replies.parent_message_id', $pmID);
        $this->db->where('messages_replies.state', 1);
        $this->db->join('users', 'messages_replies.publisher_id = users.id');
        $this->db->order_by('messages_replies.id', 'desc');
        $this->db->limit($limit, $offset);

        return $this->db->get('messages_replies')->result();
    }

    public function get_reply_message($messageID)
    {
        $this->db->select('messages_replies.id, messages_replies.parent_message_id, messages_replies.message_body, messages_replies.publisher_id, UNIX_TIMESTAMP(messages_replies.publish_date)');
        $this->db->where('messages_replies.id', $messageID);

        return $this->db->get('messages_replies')->row();
    }

    public function mark_as_read($pmID) {
		$this->db->where('parent_message_id', $pmID);
		$this->db->where('user_id', $this->session->id);
		$this->db->where('is_read', 0);
		$this->db->set('is_read', 1);
		$this->db->update('messages_users');
	}

    public function new_private_message($object, $recipients)
    {
        $message = array(
            'starter_publisher_id' => $this->session->id,
            'message_object'       => $object,
            'recipients'           => json_encode($recipients),
        );

        $this->db->insert('messages', $message);
        $this->private_message_id = $this->db->insert_id();
        $this->recipients         = $recipients;

        $this->set_recipients();

        return $this->private_message_id;
    }

    public function new_reply($private_message_id, $body)
    {
        $reply = array(
            'parent_message_id' => $private_message_id,
            'publisher_id'      => $this->session->id,
            'message_body'      => $body,
        );

        $this->db->insert('messages_replies', $reply);

        return $this->db->insert_id();
    }

    private function set_recipients()
    {
        $query = 'INSERT INTO messages_users (user_id, parent_message_id) VALUES ';
        $i     = 0;
        foreach ($this->recipients as $recipient) {
            $i++;
            if ($i == count($this->recipients)) {
                $query .= '(' . $recipient->id . ', ' . $this->private_message_id . ');';
            } else {
                $query .= '(' . $recipient->id . ', ' . $this->private_message_id . '),';
            }
        }
        $this->db->query($query);
    }

    public function set_status_deleted($ids) 
    {
		$this->db->set('state', 1);
		$this->db->where('user_id', $this->session->id);
		$this->db->where_in('parent_message_id', $ids);
		$this->db->update('messages_users');
    }

    public function set_status_unreaded($ids) 
    {
		$this->db->set('is_read', 0);
		$this->db->where('user_id', $this->session->id);
		$this->db->where_in('parent_message_id', $ids);
		$this->db->update('messages_users');
    }


    public function set_status_readed($ids) 
    {
		$this->db->set('is_read', 1);
		$this->db->where('user_id', $this->session->id);
		$this->db->where_in('parent_message_id', $ids);
		$this->db->update('messages_users');
    }


    /*** START CHECK AUTHORIZATIONS ***/

    public function is_owner_reply($replyID)
    {
        $this->db->select('publisher_id');
        $this->db->where('id', $replyID);

        return $this->session->id == $this->db->get('messages_replies')->row()->publisher_id;
    }

    /*** START "DELETE" QUERIES ***/

    public function delete_reply($replyID)
    {
        $this->db->where('id', $replyID);
        $this->db->set('state', 0);
        $this->db->update('messages_replies');
    }


    /*** AUTOMATIC MESSAGES ***/
    public function update_status_auto_messages($status)
    {
        $this->db->set('status_auto_messages', json_encode($status));
        $this->db->where('id', $this->session->id);
        $this->db->update('users');
    }

    public function viewed_auto_message($message_id)
    {
        $data = array(
            'guid' => $message_id,
            'user_id' => $this->session->id,
            'date' => now()
        );

        $this->db->insert('messages_auto_views', $data);
    }

    public function increment_views_message_auto($message_id)
    {
        $this->db->where('id', $message_id);
        $this->db->set('views', 'views+1', false);
        $this->db->update('messages_auto');
    }



}
