<?php

header('Content-Type: text/json');
//require_once("config.php");

$action = $_POST['action'];

/* conterra' la stringa di query al database */
$query_string = "";



class Actions {
    private static $initialized = false;
	private static $mysqli;
	const DB_HOST = 'localhost';
	const DB_USER = 'to-do-list';
	const DB_PASSWORD = 'to-do-list';
	const DB_DATABASE = 'to-do-list';

    public static function initialize() {
    	if (self::$initialized)
    		return;
    	self::$initialized = true;
    	
		self::$mysqli = new mysqli(Actions::DB_HOST, Actions::DB_USER, Actions::DB_PASSWORD, Actions::DB_DATABASE); 
		//print_r(self::$mysqli);
    }
	

    public static function loadData() {
    	//self::initialize();
       	$query_string = 'SELECT * FROM to_do ORDER BY date'; 
	
    	// esegui la query
		$result = self::$mysqli->query($query_string); 
	
    	$todos = array();	
    
    	// cicla sul risultato
		while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
		
			$todo_id = $row['id'];
			$todo_text = $row['text'];
			$to_do_completed = $row['completed'];
			$todo_date = $row['date'];
  
			$todo = array('id' => $todo_id,'text' =>$todo_text, 'completed' => $to_do_completed, 'date' => $todo_date);
			array_push($todos, $todo);
		}
	
    	$response = array('todos' => $todos, 'type' => 'load');

		// encodo l'array in JSON
		echo json_encode($response);	
    }
	
	public static function insertData($to_do_text){
		//self::initialize();	
		$query_string = "INSERT INTO to_do (text) values ('". htmlspecialchars($to_do_text) . "')";
		//echo $query_string;
    	// esegui la query per inserire il to do nel db
		$result = self::$mysqli->query($query_string); 
    	$query_string = 'SELECT * FROM to_do WHERE ID=' . self::$mysqli->insert_id; 
    	// esegui la query per rileggere il record inserito
		$result = self::$mysqli->query($query_string); 
    	$todos = array();	
    
    	// cicla sul risultato
		while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
		
			$todo_id = $row['id'];
  			$todo_text = $row['text'];
			$to_do_completed = $row['completed'];
  			$todo_date = $row['date'];
  
			$todo = array('id' => $todo_id,'text' =>$todo_text, 'completed' => $to_do_completed, 'date' => $todo_date);
			array_push($todos, $todo);
		}
	
    	$response = array('todos' => $todos, 'type' => 'insert');

		// encodo l'array in JSON
		echo json_encode($response);	
	}
	
	public static function updateData($id, $status){
		//self::initialize();
		$pieces = explode("_", $id);
		$query_string = 'UPDATE to_do SET completed=' . $status . ' WHERE ID=' . $pieces[1]; 
    	// esegui la query
		self::$mysqli->query($query_string); 
	
		//echo $query_string;
	
    	if(self::$mysqli->affected_rows > 0) {
		// encodo l'array in JSON

	  		$response = array('updated' => true, 'id' => $id, 'type' => 'update');
		
		} else {
	  		$response = array('updated' => false, 'id' => $id, 'type' => 'update');	
		}
	
		echo json_encode($response);
	}
	
	public static function deleteData($id){
		
			//self::initialize();
			$pieces = explode("_", $id);
			$query_string = 'DELETE FROM to_do WHERE ID=' . $pieces[1]; 
			$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); 
	
    		// esegui la query
			$result = self::$mysqli->query($query_string); 
	
    		if(self::$mysqli->affected_rows > 0) {

				// encodo l'array in JSON
	  			$response = array('deleted' => true, 'id' => $id, 'type' => 'delete');
			} else {
	  			$response = array('deleted' => false, 'id' => $id, 'type' => 'delete');
	  		}
	
			echo json_encode($response);
	
	}
}

Actions::initialize();

/* smista secondo il tipo di richiesta */
switch($action) {

	case "load" :
		Actions::loadData();
	break;
	case "insert" :
		if (isset($_POST['text'])) {
			$to_do_text = $_POST['text'];
			
			Actions::insertData($to_do_text);
		} else {
			echo "you didn't specify a text";
			return;
		}
	break;
	case "update" :
		if (isset($_POST['id'])) $id = $_POST['id'];
		if (isset($_POST['status'])) $status = $_POST['status'];
		Actions::updateData($id, $status);
	break;
	case "delete" :
		if (isset($_POST['id'])) {
			$id = $_POST['id'];
			Actions::deleteData($id);
		} else {
			return;
		}
		
	break;
}

	



?>