<?php

class tags
{

    public static function getAll(float $_idTag = NULL) {
        
        if($_idTag == NULL){
            db::query("SELECT "
                . "* "
                . "FROM " . DB_T_TAGS);
        } else {
            db::query("SELECT "
                . "* "
                . "FROM " . DB_T_TAGS 
                . " WHERE id_type = :idTag");
            db::bind(':idTag', $_idTag);
        }
        return db::resultset();
    }

    public static function cleanTags(){
        db::query("");

        try {
            
            return TRUE;
        } catch (Exception $ex) {
            return FALSE;
        }
    }

    private static function insert(string $_tag, float $_idTag = 2) {
        db::query("INSERT INTO " . DB_T_TAGS . " (id_type, label) VALUES (:id_type, :label)");
        db::bind(':id_type', $_idTag);
        db::bind(':label', $_tag);

        try {
            db::execute();
            return TRUE;
        } catch (Exception $ex) {
            return FALSE;
        }
    }

    public static function maj(float $_id, string $_tag) {
        db::query("UPDATE " . DB_T_TAGS . " SET label = :label WHERE id = :id; ");
        db::bind(':label', $_tag);
        db::bind(':id', $_id);

        try {
            db::execute();
            return TRUE;
        } catch (Exception $ex) {
            return FALSE;
        }
    }

    static public function findAndCreate(string $_tags, float $_idTag = 2){
        $find = [];
        $tmp = explode(",", $_tags);
        $allTags = self::getAll($_idTag);

        foreach ($allTags as $tags) {
            $find[] = $tags["label"];
        }

        $listCreate = array_udiff($tmp, $find, 'strcasecmp');

        foreach ($listCreate as $insert) {
            self::insert($insert, $_idTag);
        }
    }

    public static function getJquery(float $_idTag = NULL) {
        $tmp = "[";
        foreach (self::getAll($_idTag) as $tags) {
            $tmp .= "'".addslashes($tags["label"])."', ";
        }
        $tmp .= "]";
        return $tmp;
    }

    public static function textToId(string $_tags = NULL, float $_idTag = NULL) {
        $return = NULL;
        if($_tags != NULL) {

            ($_idTag != 1) ? self::findAndCreate($_tags) : NULL;

            $find = [];
            $tmp = explode(",", $_tags);

            $allTags = self::getAll($_idTag);

            foreach ($allTags as $f) {
                $find[$f["id"]] = $f["label"];
            }

            foreach ($tmp as $flag) { 
                $return .= array_search($flag, $find).",";
            }

            $return = substr($return, 0, -1);
        }
        return $return;
    }

    public static function findUsersTags(string $_tags = NULL){

        $tmp = explode(",", $_tags);

        $where = "WHERE ";

        for ($i=0; $i < count($tmp); $i++) { 
            if($i==0){
                $where .= "FIND_IN_SET(:tag".$i.", tags) ";
            } else {
                $where .= "OR FIND_IN_SET(:tag".$i.", tags) ";
            }
        }

        db::query("SELECT "
            . "" . DB_T_USER . ".id, "
            . "" . DB_T_USER . ".email, "
            . "" . DB_T_USER . ".prenom, "
            . "" . DB_T_USER . ".nom, "
            . "" . DB_T_USER . ".tags "
            . "FROM " . DB_T_USER . " "
            . $where);

            foreach ($tmp as $key => $tag) {
                db::bind(':tag'.$key, $tag);
            }
        return  db::resultset();
    }

    public static function compareUserDocument(string $_tag_user = NULL, string $_tag_document = NULL){
        if($_tag_user == NULL OR $_tag_document == NULL){
            return FALSE;
        }
        $user = explode(",", $_tag_user);
        $doc = explode(",", $_tag_document);

        foreach ($doc AS $temp) {
            if (in_array($temp, $user)){
                return TRUE;
            }
        }
        return FALSE;
    }

}