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

class Favorites_model extends CI_Model
{

    var $id = '';
    var $user_id = '';
    var $song_id = '';
    var $genre_id = '';

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

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

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

    public function get_all_favs()
    {
		//TO-DO Set all for Admin Dashboard
        $query = $this->db->get('favorites');
        return $query->result();
    }

    public function get_user_favs()
    {
		$stream_clean = $this->security->xss_clean($this->input->raw_input_stream);
        $request = json_decode($stream_clean);
        $id = $request->user_id;
        $query = $this->db->get_where('favorites', array('user_id' => $id));	
        $favs = $query->result();
		$sortedmessages =array();		
        $i=0;
        $message = [];
		$count = count($favs);
        if ($count > 0) {
            foreach ($favs as $row) {
                // $row->id = $i;
                $message = $row;
				$query2 = $this -> db -> get_where('message_archive', array('message_id'=>$message->id,'service' => $message->genre_id));
				$msgs = $query->result();
				$msg = [];
				if (count($msgs) > 0) {
				    foreach ($$msgs as $row) {
				        $msg = $row;
				    }
				}
				$sortedmessages[$i] = $msg;
                $i++;
            }
        }
        return $sortedmessages;
    }
    
        public function get_user_favs_id($id)
    {
        $query = $this->db->get_where('favorites', array('user_id' => $id));	
        $favs = $query->result();
		$sortedmessages =array();		
        $i=0;
        $message = [];
		$count = count($favs);
        if ($count > 0) {
            foreach ($favs as $row) {
                // $row->id = $i;
                $message = $row;
				$query2 = $this -> db -> get_where('message_archive', array('message_id'=>$message->song_id,'service' => $message->genre_id));
				$msgs = $query2->result();
				$msg = [];
				if (count($msgs) > 0) {
				    foreach ($msgs as $row) {
				        $msg = $row;
				    }
				}
				$sortedmessages[$i] = $msg;
                $i++;
            }
        }
        return $sortedmessages;
    }

    public function get_favs_by_genre($id)
    {
        $query = $this->db->get_where('favorites', array('genre_id' => $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_favs_by_id($id)
    {
        $query = $this->db->get_where('favorites', array('id' => $id));
        $messages = $query->result();
        $message = [];
        if (count($messages) > 0) {
            foreach ($messages as $row) {
                $message = $row;
            }
        }
        return $message;
    }

    public function save_new_fav()
    {
        $stream_clean = $this->security->xss_clean($this->input->raw_input_stream);
        $request = json_decode($stream_clean);
        $this->song_id = $request->song_id;
        $this->user_id = $request->user_id;
        $this->genre_id = $request->genre_id;
        
        $search = array('user_id' => $this->user_id,'song_id' => $this->song_id,'genre_id' => $this->genre_id );
        $this->db->where($search);
        $query = $this->db->get('favorites');
        $mge = $query->result();
        $message = [];
        if (count($mge) > 0) {
            $productArray = ['code' => '00', 'message' => 'Favorite Uploaded Previously!', 'data' => true];
            return $productArray;
        } 

        if ($this->db->insert('favorites', $this)) {
            $productArray = ['code' => '00', 'message' => 'Favorite Uploaded!', 'data' => true];
            return $productArray;
        } else {
            $productArray = ['code' => '99', 'message' => 'Error!', 'data' => false];
            return $productArray;
        }
    }

    public function save_fav($song_id, $user_id, $genre_id)
    {
        $this->song_id = $song_id;
        $this->user_id = $user_id;
        $this->genre_id = $genre_id;
        
        if ($this->db->insert('favorites', $this)) {
            $productArray = ['code' => '00', 'message' => 'Favorite Uploaded Successfully!', 'data' => true];
            return $productArray;
        } else {
            $productArray = ['code' => '99', 'message' => 'Error!', 'data' => false];
            return $productArray;
        }
    }

    public function delete_fav()
    {
        		$stream_clean = $this->security->xss_clean($this->input->raw_input_stream);
        $request = json_decode($stream_clean);
        $user_id = $request->user_id;
        $song_id = $request->song_id;
        $genre_id = $request->genre_id;
        $query = $this->db->get_where('favorites', array('user_id' => $user_id,'song_id' => $song_id,'genre_id' => $genre_id ));
        $mge = $query->result();
        $message = [];
                if (count($mge) > 0) {
            foreach ($mge as $row) {
                $message = $row;
            }
        }        
        $this -> db -> where('id', $message->id);
        if ($this -> db -> delete('favorites')) {
            return true;
            }
        else{
            return false;
            }
    } 
    
}
