<?php

namespace App\DataBase;

use Exception;
use PDO;

if (!function_exists('n_print')) {
    function n_print($data, $name = '') {
        print_r($data, 1);
    }
}

class DataBase {

    private $dataBase = __DIR__ . '/../../datas/soshot.sqlite';
    private $db;
    private $params = [
        'id' => '',
        'url' => '',
        'complete' => '',
        'full' => '',
        'hd' => '',
        'thumb' => '',
        'fav' => '',
        'og' => '',
        'pdf' => '',
        'created' => ''
    ];

    function __construct($params = null) {
        try {
            if (!file_exists($this->dataBase)) {
                $this->db = new PDO('sqlite:' . $this->dataBase);
                $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // ERRMODE_WARNING | ERRMODE_EXCEPTION | ERRMODE_SILENT

                $this->db->query("CREATE TABLE IF NOT EXISTS soshot ( 
                    id string PRIMARY KEY NOT NULL,
                    url text,
                    complete tinyint,
                    full tinyint,
                    hd tinyint,
                    thumb tinyint,
                    fav tinyint,
                    og tinyint,
                    pdf tinyint,
                    created DATETIME
                );");
            } else {
                $this->db = new PDO('sqlite:' . $this->dataBase);
                $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // ERRMODE_WARNING | ERRMODE_EXCEPTION | ERRMODE_SILENT
            }
        } catch (Exception $e) {
            echo $e->getMessage();
            die();
        }

        if (!empty($params)) {
            $this->setParams($params);
        }
        return $this;
    }

    public function setParams($params) {
        $this->params = (object)$this->params;
        $this->params->id = $params->hmac;
        $this->params->url = $params->url;
        $this->params->type = $params->type;
        $this->params->created = date("Y-m-d H:i:s");
        $this->params = $this->params;
    }

    public function addUpdate($update, $type = '') {
        if (!empty($type)) {
            $this->params->type = $type;
        }
        if ($this->testExit($this->params->id)) {
            $this->update($update);
        } else {
            $this->insert($update);
        }
    }

    public function insert($update) {
        $stmt = $this->db->prepare("INSERT INTO soshot (id, url, " . $this->params->type . ", created) VALUES 
        (:id, :url, :" . $this->params->type . ", :created)");
        //$stmt->debugDumpParams();

        $result = $stmt->execute(array(
            'id' => $this->params->id,
            'url' => $this->params->url,
            $this->params->type => $update,
            'created'        => $this->params->created
        ));
    }

    private function update($update) {
        $stmt = $this->db->prepare("UPDATE soshot
        SET " . $this->params->type . "=:" . $this->params->type . "
        WHERE id=:id;");
        
        $result = $stmt->execute([
            ':' . $this->params->type => $update,
            ':id' => $this->params->id
        ]);
        //$stmt->debugDumpParams();
    }

    private function testExit($id) {
        $stmt = $this->db->prepare("SELECT id FROM soshot WHERE id=:id LIMIT 1;");
        $stmt->execute(array(':id' => $id));
        $result = $stmt->fetchAll(PDO::FETCH_OBJ);
        if (!empty($result)) {
            return true;
        }
        return false;
    }

    public function getTotal() {
        $stmt = $this->db->prepare("SELECT COUNT(id) AS nb FROM soshot ");
        $stmt->execute();
        $result = $stmt->fetch();
        return $result['nb'];
    }

    public function getList($start, $end) {
        $stmt = $this->db->prepare("SELECT * FROM soshot ORDER BY created DESC limit :start, :end;");
        $stmt->execute(array(':start' => $start, ':end' => $end));
        $result = $stmt->fetchAll(PDO::FETCH_OBJ);
        return $result;
    }
}
