<?php

namespace BancaDati;

use PDO;
use PDOException;

class BancaDati {
    private $dbName = "BancaDati";
    private $linkName = "localhost";
    private $user = "root";
    private $pw = "root";

    public $pdo;
    public function __construct() {
        $this->linkDB();
    }
    private function linkDB() {
        try {
            $this->pdo = new PDO("mysql:dbname=$this->dbName;host=$this->linkName"
                , $this->user
                , $this->pw
                , array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
        } catch (PDOException $e) {
            var_dump($e);
            die;
        }
    }
    public function createUUID()
    {
        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }

    /**
     * Einheitliche Insert Funktion
     * @param string $table
     * @param array $values
     * @return void
     * author Simon Bock
     */
    public function insert(string $table, array $values){
        $value = "";
        $column = "";
        foreach ($values as $col => $v){
            $value .=  "'" . $v . "',";
            $column .= $col . ",";
        }
        $value = trim($value, ",");
        $column = trim($column, ",");

        $sql = "INSERT INTO $table($column) VALUES ($value);";
        try {
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
            return $this->pdo->lastInsertId();
        }catch (PDOException $e){
            return false;
            //die;
        }
    }

    /**
     * Einheitliche Update Funktion
     * @param string $table
     * @param string $id
     * @param array $values
     * @return void
     * @author Malte Schulze Hobeling
     */
    public function update(string $table, string $id, array $values){
        $value = "";
        foreach ($values as $col => $v){
            $value .= $col . "='" . $v . "',";
        }
        $value = trim($value, ",");

        $sql = "UPDATE " . $table . " SET " . $value . " WHERE `id` = " . $id . ";";
        try {
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
        }catch (PDOException $e){
            die;
        }
    }

    /**
     * Einheitliche Delete Funktion
     * @param string $table
     * @param string $id
     * @return void
     * @author Malte Schulze Hobeling
     */
    public function delete(string $table, string $id){
        $sql = "DELETE FROM " . $table . " WHERE `id` = '" . $id . "';";
        try {
            $sth = $this->pdo->prepare($sql);
            $sth->execute();
        }catch (PDOException $e){
            die;
        }
    }

    /**
     * einheitliche Select Funktion
     * kann sortiert werden durch ["by"]=>"col" und ["order"]=>"ASC|DESC"
     * @param string $table
     * @param array $where ["column"]=>"value" es wird mit LIKE verglichen und mit AND verbunden
     * @return void
     * @author Malte Schulze Hobeling
     */
    public function select(string $table, array $where = []){
        $whereString = "";
        $orderString = "";
        if(isset($where["by"])){
            $orderString = " ORDER BY " . $where["by"];
            unset($where["by"]);
            if(isset($where["order"])){
                $orderString .= " " . $where["order"];
                unset($where["order"]);
            }
        }
        foreach ($where as $col => $v) {
            if($whereString != ""){
                $whereString .= " AND ";
            }
            $whereString .= "`" . $col . "` LIKE '" . $v . "'";
        }
        $sql = "SELECT * FROM " . $table . ((count($where) > 0) ? " WHERE ".$whereString.$orderString : "") .";";
        try {
            return $this->pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
        }catch (PDOException $e){
            die;
        }
    }
}