<?php

 
class DbOperation
{
    private $con;
 
 	// **** Connect to database ****
    function __construct()
    {
        require_once dirname(__FILE__) . '/DbConnect.php';
        $db = new DbConnect();
        $this->con = $db->connect();
    }

	// **** Create new user table in database ****
	public function createUser($userid,$password){
        $password = password_hash($password, PASSWORD_DEFAULT);
        $stmt = $this->con->prepare("INSERT INTO `users`(`id`,`userid`, `password`) VALUES (NULL,?,?)");
        $stmt->bind_param("ss", $userid, $password);
		if($stmt->execute()){
			if (!file_exists("images/".$userid)) {     
            mkdir("images/".$userid);
            } 
			return true; 
		}
		return false;
	}

	// **** Delete user in table users and delete user permission tables ****
	public function deleteUser($userid){
		$sql = "SELECT * FROM `users` WHERE `userid`= '$userid'";
		$result = $this->con->query($sql);
		$row = $result->fetch_assoc();
		foreach ($row as $key => $value) {
			if ($key == $value){
    		$tablename = $value;
    		$stmt = $this->con->prepare("DELETE FROM `$tablename` WHERE `userid`='$userid'");
    		$stmt->execute();
    		}

		}

		$stmt = $this->con->prepare("DELETE FROM `users` WHERE `userid`='$userid'");
		if($stmt->execute())
			$dir= "images/".$userid;
  			rmrf($dir);
			return true; 
		return false; 
	}

	// **** Delete user in table users and delete user permission tables ****
	public function deletePermissionType($userid,$permissionType){
		
		$stmt1 = $this->con->prepare("UPDATE `users` SET `$permissionType`='' WHERE `userid`='$userid'");
		if (!$stmt1->execute()){
    		return false;
    	}
    	$stmt = $this->con->prepare("DELETE FROM `$permissionType` WHERE `userid`='$userid'");
    	if ($stmt->execute()){
    		return true;
    	}	 
		return false; 
	}

	// **** Add permission type to user ****
	public function addPermissionType($userid,$permissionType){
		$sql = "SELECT * FROM `users` WHERE `userid`= '$userid'";
		$result = $this->con->query($sql);
		$row = $result->fetch_assoc();
		if (mysqli_num_rows($result) == 0)  return false;
		$tablename = $userid.$permissionType;
		$stmt1 = NULL;
		switch ($permissionType){
			case "sms":
				$stmt1 = $this->con->prepare("UPDATE `users` SET `sms`='sms' WHERE `userid`='$userid'");
				break;
			case "event":
				$stmt1 = $this->con->prepare("UPDATE `users` SET `event`='event' WHERE `userid`='$userid'");
				break;
			case "callLog":
				$stmt1 = $this->con->prepare("UPDATE `users` SET `callLog`='callLog' WHERE `userid`='$userid'");
				break;
			case "camera":
				$stmt1 = $this->con->prepare("UPDATE `users` SET `camera`='camera' WHERE `userid`='$userid'");
				break;
			case "contact":
				$stmt1 = $this->con->prepare("UPDATE `users` SET `contact`='contact' WHERE `userid`='$userid'");
				break;
			case "location":
				$stmt1 = $this->con->prepare("UPDATE `users` SET `location`='location' WHERE `userid`='$userid'");
				break;
			case "phoneState":
				$stmt1 = $this->con->prepare("UPDATE `users` SET `phoneState`='phoneState' WHERE `userid`='$userid'");
				break;
			case "storage":
				$stmt1 = $this->con->prepare("UPDATE `users` SET `storage`='storage' WHERE `userid`='$userid'");
				break;
			default: return false;

		}
		if($stmt1->execute())
			return true; 
		return false; 
	}


	// **** Add new SMS to table ****
	public function addSMS($userid,$date, $number,$text,$type){
		$stmt = $this->con->prepare("INSERT INTO `sms`(`id`,`userid`, `date`, `number`, `text`, `type`) VALUES (NULL, ?,?,?,?,?)");
		$stmt->bind_param("sssss",$userid,$date, $number,$text,$type);
		if($stmt->execute())
			return true; 
		return false; 
	}

	// **** Add new Event to table ****
	public function addEvent($userid,$title, $startdate,$enddate,$description){
		$stmt = $this->con->prepare("INSERT INTO `event`(`id`,`userid`, `title`, `startDate`, `endDate`, `description`) VALUES (NULL, ?, ?,?,?,?)");
		$stmt->bind_param("sssss", $userid,$title, $startdate,$enddate,$description);
		if($stmt->execute())
			return true; 
		return false; 
	}

	// **** Add new callLog to table ****
	public function addCallLog($userid,$phoneNumber, $date,$duration,$type){
		$stmt = $this->con->prepare("INSERT INTO `callLog`(`id`,`userid`, `phoneNumber`, `date`, `duration`, `type`) VALUES (NULL,?, ?, ?,?,?)");
		$stmt->bind_param("sssss", $userid,$phoneNumber, $date,$duration,$type);
		if($stmt->execute())
			return true; 
		return false; 
	}

	// **** Add new camera photo to table ****
	public function addCameraPhoto($userid,$photoname){
		$stmt = $this->con->prepare("INSERT INTO `camera`(`id`,`userid`, `photoName`) VALUES (NULL, ?,?)");
		$stmt->bind_param("ss", $userid,$photoname);
		if($stmt->execute())
			return true; 
		return false; 
	}

	// **** Add new contact to table ****
	public function addContact($userid,$name,$phoneNumber){
		$stmt = $this->con->prepare("INSERT INTO `contact`(`id`,`userid`, `name`,`phoneNumber`) VALUES (NULL,?,?,?)");
		$stmt->bind_param("sss",$userid,$name,$phoneNumber);
		if($stmt->execute())
			return true; 
		return false; 
	}

	// **** Add new location to table ****
	public function addLocation($userid,$latitude, $longtitude,$accuracy,$altitude){
		$stmt = $this->con->prepare("INSERT INTO `location`(`id`,`userid`, `latitude`, `longtitude`, `accuracy`, `altitude`) VALUES (NULL, ?,?,?,?,?)");
		$stmt->bind_param("sssss",$userid, $latitude, $longtitude,$accuracy,$altitude);
		if($stmt->execute())
			return true; 
		return false; 
	}

	// **** Add new phoneState to table ****
	public function addPhoneState($userid,$phoneNumber, $dataNetworkType,$operator){
		$stmt = $this->con->prepare("INSERT INTO `phoneState`(`id`,`userid`, `phoneNumber`, `dataNetworkType`, `operator`) VALUES (NULL, ?, ?,?,?)");
		$stmt->bind_param("ssss",$userid,$phoneNumber, $dataNetworkType,$operator);
		if($stmt->execute())
			return true; 
		return false; 
	}

	// **** Add new media photo to table ****
	public function addMediaPhoto($userid,$imagePath){
		$stmt = $this->con->prepare("INSERT INTO `storage`(`id`,`userid`, `imagePath`) VALUES (NULL,?,?)");
		$stmt->bind_param("ss", $userid,$imagePath);
		if($stmt->execute())
			return true; 
		return false; 
	}

}

