<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Message_archive extends CI_Model
{

    var $id;
    var $message_id;
    var $title = '';
    var $media_file_url = '';
    var $preacher = '';
    var $service_date = '';
    var $uploaded_by = '';
    var $updated_by = '';
    var $img_url = '';
    var $service = '';

    public function __construct()
    {
        parent::__construct();
    }

    public function count_all()
    {
        return $this->db->count_all('message_archive');
    }

    public function count_row_for_query($query)
    {
        return $query->num_rows();
    }


    public function get_all_msgs()
    {
        $query = $this->db->get('message_archive');
        return $query->result();
    }

    public function get_msgs_reecent()
    {
        $query = $this->db->get('message_archive');
        return $query->result();
    }

    public function get_user_msgs()
    {
        $query = $this->db->get('message_archive');
        return $query->result();
    }

    public function get_msgs_recent()
    {
        $this->db->order_by('updated_date', 'desc');
        $this->db->limit(10);
        $query = $this->db->get('message_archive');
        return $query->result();
    }

    public function get_user_msg()
    {
        $query = $this->db->get('message_archive');
        return $query->result();
    }

    public function get_messages_by_service($id)
    {
        $query = $this->db->get_where('message_archive', array('service' => $id));
        $messages = $query->result();
        $sortedmessages =array();
        $message = [];
        $i=0;
        $count = count($messages);
        if ($count > 0) {
            foreach ($messages as $row) {
                // $row->id = $i;
                $message = $row;
                $sortedmessages[$i] = $message;
                $i++;
            }
        }
        return $messages;
    }

    public function get_messages_by_id($id)
    {
        $query = $this->db->get_where('message_archive', array('id' => $id));
        $messages = $query->result();
        $message = [];
        if (count($messages) > 0) {
            foreach ($messages as $row) {
                $message = $row;
            }
        }
        return $message;
    }

    public function save_new_msg()
    {
        $stream_clean = $this->security->xss_clean($this->input->raw_input_stream);
        $request = json_decode($stream_clean);
        $this->title = $request->title;
        $this->media_file_url = $request->media_file_url;
        $this->preacher = $request->preacher;
        $this->service_date = $request->service_date;
        $this->uploaded_by = $request->uploaded_by;
        $this->updated_by = $request->updated_by;
        $this->img_url = $request->img_url;
        $this->service = $request->service;

        if ($this->db->insert('message_archive', $this)) {
            $productArray = ['code' => '00', 'message' => 'Message Uploaded Successfully!', 'data' => true];
            return $productArray;
        } else {
            $productArray = ['code' => '99', 'message' => 'Error in creating user!', 'data' => false];
            return $productArray;
        }
    }

    public function save_msg($title, $media_file_url, $preacher, $service_date, $uploaded_by, $updated_by, $img_url, $service)
    {
        $this->title = $title;
        $this->media_file_url = $media_file_url;
        $this->preacher = $preacher;
        $this->service_date = $service_date;
        $this->uploaded_by = $uploaded_by;
        $this->updated_by = $updated_by;
        $this->img_url = $img_url;
        $this->service = $service;
        
        $query = $this->db->get_where('message_id_seq', array('service_id' => $service));
                $ms = $query->result();
        $me = [];
        if (count($ms) > 0) {
            foreach ($ms as $row) {
                $me = $row;
            }
        }
        $mid = $me->current_id;
        $msg_id = $mid+1;
        $this->message_id = $msg_id;

        if ($this->db->insert('message_archive', $this)) {
            
            $this->db->set('current_id', $msg_id)->where('service_id', $service)->update('message_id_seq');
            
            $productArray = ['code' => '00', 'message' => 'Message Uploaded Successfully!', 'data' => true];
            return $productArray;
        } else {
            $productArray = ['code' => '99', 'message' => 'Error in saving message!', 'data' => false];
            return $productArray;
        }
    }

    public function update_msg($id, $title, $media_file_url, $preacher, $service_date, $uploaded_by, $updated_by, $img_url, $service)
    {
        $message = $this->get_messages_by_id($id);
        
        if(empty($media_file_url)){
            $this->db->set('media_file_url', $media_file_url);
        }
        
        if(empty($img_url)){
            $this->db->set('img_url', $img_url);
        }
        
        if ($this->db
        ->set('title', $title)
        ->set('preacher', $preacher)
        ->set('service_date', $service_date)
        ->set('uploaded_by', $uploaded_by)
        ->set('updated_by', $updated_by)
        ->set('service', $service)
        ->where('id', $id)->update('message_archive')) {
            $productArray = ['code' => '00', 'message' => 'Message Updated Successfully!', 'data' => true];
            return $productArray;
        } else {
            $productArray = ['code' => '99', 'message' => 'Error in Updating Message!', 'data' => false];
            return $productArray;
        }
    }
    
    public function delete_message($id)
    {
        $query = $this->db->get_where('message_archive', array('id' => $id));
        $mge = $query->result();
        $message = [];
                if (count($mge) > 0) {
            foreach ($mge as $row) {
                $message = $row;
            }
        }
        $msg = ['title' => $message->title, 'media_file_url' => $message->media_file_url, 
        'preacher' => $message->preacher, 
        'service_date' => $message->service_date, 
        'uploaded_by' => $message->uploaded_by, 
        'updated_by' => $message->updated_by, 
        'img_url' => $message->img_url,
        'service' => $message->service,
        'deleted' => true];
        
        $this -> db -> where('id', $message->id);
        $this -> db -> delete('message_archive');
        $query = $this->db->get_where('message_id_seq', array('service_id' => $service));
        $ms = $query->result();
        $me = [];
        if (count($ms) > 0) {
            foreach ($ms as $row) {
                $me = $row;
            }
        }
        $mid = $me->current_id;
        $msg_id = $mid-1;
        $this->message_id = $msg_id;
        $this->db->set('current_id', $msg_id)->where('service_id', $service)->update('message_id_seq');
        
        if ($this->db->insert('archived_deleted_messages', $msg)) {
            return true;
            }
        else{
            return false;
            }
    }
    
        public function archive_message($id)
    {
        $query = $this->db->get_where('message_archive', array('id' => $id));
        $message = $query->result();
        $msg = array();
        $msg->title = $message->title;
        $msg->media_file_url = $message->media_file_url;
        $msg->preacher = $message->preacher;
        $msg->service_date = $message->service_date;
        $msg->uploaded_by = $message->uploaded_by;
        $msg->updated_by = $message->updated_by;
        $msg->img_url = $message->img_url;
        $msg->service = $message->service;
        $msg->archived = true;
        
        $this -> db -> where('id', $message->id);
        $this -> db -> delete('message_archive');
        if ($this->db->insert('archived_deleted_messages', $msg)) {
            return true;
            }
        else{
            return false;
            }
    }
    
}
