<?PHP
/**
 * Contains a class of blacklists for unwanted terms.
 *
 * @author Joshua Ramon Enslin <joshua@museum-digital.de>
 */
declare(strict_types = 1);

/**
 * Contains lists of disallowed terms by language.
 */
final class NodaBlacklistedTerms {
    /**
     * A blacklist of disallowed tags. All entries are listed in full lowercase.
     */
    public const TAG_BLACKLIST = [
        'de' => [
            'Allgemein',
            'andere',
            'anderes',
            'In general',
            'sonst',
            'sonst.',
            'sonstige',
            'sonstiges',
            'versch.',
            'verschiedenes',
            'unbekannt',
            '_',
            'weitere',
            'weiteres',

            'objekt',
            'objekte',
            'noch nicht bestimmte objekte',
            'ding',
            'dinge',
            'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
            'Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
            'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
            'nichtmünzliches',
            'unbestimmt',
            'AA',
            'BB',
            'CC',
            'F',
            'K',
            'V',
            'Kl',
            'En',
            'DD',
            'FF',
            'Ma',
            'PP',
            'HH',
            'WE',
            'QQ',
            'EE',
            'BB',
            'PO',
            'LL',
            'IV',
            'MM',
            'The',
            'KK',
            'K.A',
            'S',
            'N) (generell',
            'DD',
            'EE',
            'FF',
            'II',
            'GG',
            'HH',
            'T',
            'D',
            'Generell) (+ Variante',
            'Unbeschreibliche',
            'LL',
            'Von unbestimmbarer Gattung',
            'Ohne andere Personen',
            'Andere Darstellungen, denen der NAME einer historischen Person beigegeben werden kann',
            'Andere Konzepte',
            'Andere Systeme oder Kanons',
            '-',
            '?',
        ],
        'en' => [
            'AA',
            'other',
            'others',
            'unknown',
            'various',
            '-',
            '?',
        ],
        'hu' => [
            'AA',
            'ism.',
            'ismeretlen',
            '-',
            '?',
        ],
    ];

    public const ACTOR_GIVEN_NAMES_BLACKLIST = [
        'Doktor',
        'Dr.',
        'PhD.',
        'Dr',
        'Frau',
        'Herr',
        'Amtsmann',
        'Prof.',
        'Prof. Dr.',
        'Hofdruckerei',
        'Dt',
        'Gr',
        'Senatsbaudirektor',
    ];


    /**
     * Checks if an tag name is blacklisted.
     *
     * @param string $lang Input language.
     * @param string $name Input name to check.
     *
     * @return boolean
     */
    public static function tagIsInBlacklist(string $lang, string $name):bool {

        $toCheck = \strtolower($name);

        if (!isset(self::TAG_BLACKLIST[$lang])) {
            return false;
        }

        if (\in_array($toCheck, self::TAG_BLACKLIST[$lang], true) || \in_array($name, self::TAG_BLACKLIST[$lang], true)) {
            return true;
        }

        return false;

    }

    /**
     * Checks if a place name is blacklisted in the DB.
     *
     * @param MDMysqli $mysqli DB connection.
     * @param string   $lang   The user's currently used language.
     * @param string   $name   The name entered by the user.
     *
     * @return boolean
     */
    public static function checkPlaceBlacklistedInDb(MDMysqli $mysqli, string $lang, string $name):bool {

        $result = $mysqli->query_by_stmt("SELECT 1
            FROM `" . DATABASENAME_NODA . "`.`orte_blacklist`
            WHERE `language` = ?
                AND `ort_name` = ?
            LIMIT 1", "ss", $lang, $name);

        if ($result->num_rows === 0) {
            $result->close();
            return false;
        }

        $result->close();
        return true;

    }

    /**
     * Checks if an actor name is blacklisted in the DB.
     *
     * @param MDMysqli $mysqli DB connection.
     * @param string   $lang   The user's currently used language.
     * @param string   $name   The name entered by the user.
     *
     * @return boolean
     */
    public static function checkPersinstBlacklistedInDb(MDMysqli $mysqli, string $lang, string $name):bool {

        $result = $mysqli->query_by_stmt("SELECT 1
            FROM `" . DATABASENAME_NODA . "`.`persinst_blacklist`
            WHERE `language` = ?
                AND `persinst_name` = ?
            LIMIT 1", "ss", $lang, $name);

        if ($result->num_rows === 0) {
            $result->close();
            return false;
        }

        $result->close();
        return true;

    }

    /**
     * Checks if a time name is blacklisted in the DB.
     *
     * @param MDMysqli $mysqli DB connection.
     * @param string   $lang   The user's currently used language.
     * @param string   $name   The name entered by the user.
     *
     * @return boolean
     */
    public static function checkTimeBlacklistedInDb(MDMysqli $mysqli, string $lang, string $name):bool {

        $result = $mysqli->query_by_stmt("SELECT 1
            FROM `" . DATABASENAME_NODA . "`.`zeiten_blacklist`
            WHERE `language` = ?
                AND `zeit_name` = ?
            LIMIT 1", "ss", $lang, $name);

        if ($result->num_rows === 0) {
            $result->close();
            return false;
        }

        $result->close();
        return true;

    }

    /**
     * Checks if an actor's given name is in the specific blacklist for those.
     * Returns true if the name is blacklisted.
     *
     * @param string $given_name Given name.
     *
     * @return boolean
     */
    public static function checkActorGivenNameIsBlacklisted(string $given_name):bool {

        if (in_array(trim($given_name, ' ()[]?'), self::ACTOR_GIVEN_NAMES_BLACKLIST, true)) return true;
        else return false;

    }
}
