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

class Playlist extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('model_music');
		$this->load->library('form_validation');
	}
	public function index(){
		if ($recherche=filter_input(INPUT_GET,'recherche') == false or $recherche=filter_input(INPUT_GET,'recherche') == null){
			$playlists = $this->model_music->getPlaylist();
		}else{
			$recherche=filter_input(INPUT_GET,'recherche');
			$playlists = $this->model_music->getSearchPlaylist($recherche);
		}
		$this->load->view('layout/header');
		if ($playlists == false){
			$page = preg_split('/[\/]/',$_SERVER['REQUEST_URI']);
			$this->load->view('error',['page'=>$page[count($page)-1]]);
			$playlists = $this->model_music->getPlaylist();
		}
		$this->load->view('playlist_list',['playlists'=>$playlists]);
		$this->load->view('layout/footer');
	}

    public function addplaylist(){
        

		$this->form_validation->set_rules('name', 'name', 'required');
				
        if ($this->form_validation->run() == FALSE){
			$this->load->view('layout/header');
        	$this->load->view('create_playlist');
		    $this->load->view('layout/footer');
		}else{
        	$nom = $this->input->post('name');
        	$description = $this->input->post('Description');
			$userId = $this->session->userdata('userId');
        	$playlist = array(
        	    'name'=>$nom,
        	    'description'=>$description,
        	    'userId'=>$userId,
        	);
        	$this->model_music->addPlayliste($playlist);
			redirect('playlist');
    	}
	}

	public function duplicatePlaylist($id){
		$this->form_validation->set_rules('name', 'name', 'required');
				
        if ($this->form_validation->run() == FALSE){
			$this->load->view('layout/header');
        	$this->load->view('create_playlist');
		    $this->load->view('layout/footer');
		}else{
        	$nom = $this->input->post('name');
        	$description = $this->input->post('Description');
			$userId = $this->session->userdata('userId');
        	$playlist = array(
        	    'name'=>$nom,
        	    'description'=>$description,
        	    'userId'=>$userId,
        	);
        	$this->model_music->addPlayliste($playlist);
			$this->model_music->DuplicatePlaylist($id, $nom);
			redirect('playlist');
		}
	}

	public function deletePlaylist($id){
		$this->model_music->DeletePlaylist($id);
		redirect('playlist');
	}

	public function create_random_playlist() {
		$this->form_validation->set_rules('num_tracks', 'num_tracks', 'required');
		$this->form_validation->set_rules('name', 'name', 'required');

		$data['genres'] = $this->model_music->get_all_genres();
        $data['artists'] = $this->model_music->get_all_artists();
        $data['years'] = $this->model_music->get_all_years();
				
        if ($this->form_validation->run() == FALSE){
			$this->load->view('layout/header');
        	$this->load->view('random_Playlist',$data);
		    $this->load->view('layout/footer');
		}else{
        	$genres = $this->input->post('genre');
			$artists = $this->input->post('artist');
			$years = $this->input->post('year');
			$nom = $this->input->post('name');
			$description = $this->input->post('Description');
			$userId = $this->session->userdata('userId');
			$num_tracks = (int) $this->input->post('num_tracks');
        	$playlist = array(
        	    'name'=>$nom,
        	    'description'=>$description,
        	    'userId'=>$userId,
        	);
        	$this->model_music->addPlayliste($playlist);
			$idplaylist = $this->model_music->IdPLaylistByName($nom);
			$tracks = $this->model_music->get_random_tracks($genres, $artists, $years, $num_tracks);
			foreach ($tracks as $musique){
				$this->model_music->AddSongtoPlaylist($idplaylist, $musique->trackId);
			}
			redirect('playlist');
    	}
    }
}