<?PHP
/**
 * Contains a class for loaded names in bulk.
 *
 * @author Joshua Ramon Enslin <joshua@museum-digital.de>
 */
declare(strict_types = 1);

/**
 * Class for the batch getting of noda name.
 */
final class NodaNameGetter {
    /**
     * Static function for getting tag names in bulk.
     *
     * @param MDMysqli       $mysqli_noda DB connection.
     * @param string         $lang        Language.
     * @param array<integer> $tag_ids     Tag IDs.
     *
     * @return array<int, string>
     */
    public static function getBatchTagNames(MDMysqli $mysqli_noda, string $lang, array $tag_ids):array {

        $output = [];

        // Get translations
        $result = $mysqli_noda->do_read_query("SELECT `tag_id`, `trans_name`
            FROM `tag_translation`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `tag_id` IN (" . implode(', ', $tag_ids) . ")");

        while ($cur = $result->fetch_row()) {
            $output[(int)$cur[0]] = (string)$cur[1];
        }
        $result->close();

        if (!empty($tag_ids_left = array_diff($tag_ids, array_keys($output)))) {

            $result = $mysqli_noda->do_read_query("SELECT `tag_id`, `tag_name`
                FROM `tag`
                WHERE `tag_id` IN (" . implode(', ', $tag_ids_left) . ")");

            while ($cur = $result->fetch_row()) {
                $output[(int)$cur[0]] = (string)$cur[1];
            }
            $result->close();

        }

        if (count($output) > 1) {
            asort($output);
        }

        return $output;

    }

    /**
     * Static function for getting place names in bulk.
     *
     * @param MDMysqli       $mysqli_noda DB connection.
     * @param string         $lang        Language.
     * @param array<integer> $place_ids   Place IDs.
     *
     * @return array<int, string>
     */
    public static function getBatchPlaceNames(MDMysqli $mysqli_noda, string $lang, array $place_ids):array {

        $output = [];

        // Get translations
        $result = $mysqli_noda->do_read_query("SELECT `ort_id`, `trans_name`
            FROM `ort_translation`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `ort_id` IN (" . implode(', ', $place_ids) . ")");

        while ($cur = $result->fetch_row()) {
            $output[(int)$cur[0]] = (string)$cur[1];
        }
        $result->close();

        if (!empty($place_ids_left = array_diff($place_ids, array_keys($output)))) {

            $result = $mysqli_noda->do_read_query("SELECT `ort_id`, `ort_name`
                FROM `orte`
                WHERE `ort_id` IN (" . implode(', ', $place_ids_left) . ")");

            while ($cur = $result->fetch_row()) {
                $output[(int)$cur[0]] = (string)$cur[1];
            }
            $result->close();

        }

        if (count($output) > 1) {
            asort($output);
        }

        return $output;

    }

    /**
     * Static function for getting persinst names in bulk.
     *
     * @param MDMysqli       $mysqli_noda  DB connection.
     * @param string         $lang         Language.
     * @param array<integer> $persinst_ids Persinst IDs.
     *
     * @return array<int, string>
     */
    public static function getBatchPersinstNames(MDMysqli $mysqli_noda, string $lang, array $persinst_ids):array {

        $output = [];

        // Get translations
        $result = $mysqli_noda->do_read_query("SELECT `persinst_id`, `trans_name`
            FROM `persinst_translation`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `persinst_id` IN (" . implode(', ', $persinst_ids) . ")");

        while ($cur = $result->fetch_row()) {
            $output[(int)$cur[0]] = (string)$cur[1];
        }
        $result->close();

        if (!empty($persinst_ids_left = array_diff($persinst_ids, array_keys($output)))) {
            $result = $mysqli_noda->do_read_query("SELECT `persinst_id`, `persinst_name_en`
                FROM `persinst`
                WHERE `persinst_id` IN (" . implode(', ', $persinst_ids_left) . ")");

            while ($cur = $result->fetch_row()) {
                $output[(int)$cur[0]] = (string)$cur[1];
            }
            $result->close();
        }

        if (count($output) > 1) {
            asort($output);
        }

        return $output;

    }

    /**
     * Static function for getting persinst names in bulk.
     *
     * @param MDMysqli       $mysqli_noda  DB connection.
     * @param string         $lang         Language.
     * @param array<integer> $persinst_ids Persinst IDs.
     *
     * @return array<int, string>
     */
    public static function getBatchPersinstNamesWithLifeDates(MDMysqli $mysqli_noda, string $lang, array $persinst_ids):array {

        $output = [];

        // Get translations
        $result = $mysqli_noda->do_read_query("SELECT `persinst`.`persinst_id`, `trans_name`, `persinst_geburtsjahr`, `persinst_sterbejahr`
            FROM `persinst_translation`, `persinst`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `persinst`.`persinst_id` = `persinst_translation`.`persinst_id`
                AND `persinst`.`persinst_id` IN (" . implode(', ', $persinst_ids) . ")");

        while ($cur = $result->fetch_row()) {
            $name = (string)$cur[1];
            if (!empty($cur[2]) || !empty($cur[3])) {
                $name .= ' (' . $cur[2] . '-' . $cur[3] . ')';
            }
            $output[(int)$cur[0]] = $name;
        }
        $result->close();

        if (!empty($persinst_ids_left = array_diff($persinst_ids, array_keys($output)))) {
            $result = $mysqli_noda->do_read_query("SELECT `persinst_id`, `persinst_anzeigename`
                FROM `persinst`
                WHERE `persinst_id` IN (" . implode(', ', $persinst_ids_left) . ")");

            while ($cur = $result->fetch_row()) {
                $output[(int)$cur[0]] = (string)$cur[1];
            }
            $result->close();
        }

        if (count($output) > 1) {
            asort($output);
        }

        return $output;

    }

    /**
     * Static function for getting time names in bulk.
     *
     * @param MDMysqli       $mysqli_noda DB connection.
     * @param string         $lang        Language.
     * @param array<integer> $time_ids    Time IDs.
     *
     * @return array<int, string>
     */
    public static function getBatchTimeNames(MDMysqli $mysqli_noda, string $lang, array $time_ids):array {

        $output = [];

        // Get translations
        $result = $mysqli_noda->do_read_query("SELECT `zeit_id`, `trans_name`
            FROM `zeit_translation`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `zeit_id` IN (" . implode(', ', $time_ids) . ")");

        while ($cur = $result->fetch_row()) {
            $output[(int)$cur[0]] = (string)$cur[1];
        }
        $result->close();

        if (!empty($time_ids_left = array_diff($time_ids, array_keys($output)))) {

            $result = $mysqli_noda->do_read_query("SELECT `zeit_id`, `zeit_name`
                FROM `zeiten`
                WHERE `zeit_id` IN (" . implode(', ', $time_ids_left) . ")");

            while ($cur = $result->fetch_row()) {
                $output[(int)$cur[0]] = (string)$cur[1];
            }
            $result->close();

        }

        if (count($output) > 1) {
            asort($output);
        }

        return $output;

    }

    /**
     * Static function for getting single tag names by ID.
     *
     * @param MDMysqli $mysqli_noda DB connection.
     * @param string   $lang        Language.
     * @param integer  $tag_id      Tag IDs.
     *
     * @return string
     */
    public static function getTagName(MDMysqli $mysqli_noda, string $lang, int $tag_id):string {

        // Get translations
        $result = $mysqli_noda->query_by_stmt("SELECT `trans_name`
            FROM `tag_translation`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `tag_id` = ?", "i", $tag_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        $result = $mysqli_noda->query_by_stmt("SELECT `tag_name`
            FROM `tag`
            WHERE `tag_id` = ?", "i", $tag_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        return "";

    }

    /**
     * Static function for getting single actor names by ID.
     *
     * @param MDMysqli $mysqli_noda DB connection.
     * @param string   $lang        Language.
     * @param integer  $persinst_id Actor IDs.
     *
     * @return string
     */
    public static function getPersinstName(MDMysqli $mysqli_noda, string $lang, int $persinst_id):string {

        // Get translations
        $result = $mysqli_noda->query_by_stmt("SELECT `trans_name`
            FROM `persinst_translation`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `persinst_id` = ?", "i", $persinst_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        $result = $mysqli_noda->query_by_stmt("SELECT `persinst_name_en`
            FROM `persinst`
            WHERE `persinst_id` = ?", "i", $persinst_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        return "";

    }

    /**
     * Static function for getting single place names by ID.
     *
     * @param MDMysqli $mysqli_noda DB connection.
     * @param string   $lang        Language.
     * @param integer  $place_id    Place IDs.
     *
     * @return string
     */
    public static function getPlaceName(MDMysqli $mysqli_noda, string $lang, int $place_id):string {

        // Get translations
        $result = $mysqli_noda->query_by_stmt("SELECT `trans_name`
            FROM `ort_translation`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `ort_id` = ?", "i", $place_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        $result = $mysqli_noda->query_by_stmt("SELECT `ort_name`
            FROM `orte`
            WHERE `ort_id` = ?", "i", $place_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        return "";

    }

    /**
     * Static function for getting single time names by ID.
     *
     * @param MDMysqli $mysqli_noda DB connection.
     * @param string   $lang        Language.
     * @param integer  $time_id     Time IDs.
     *
     * @return string
     */
    public static function getTimeName(MDMysqli $mysqli_noda, string $lang, int $time_id):string {

        // Get translations
        $result = $mysqli_noda->query_by_stmt("SELECT `trans_name`
            FROM `zeit_translation`
            WHERE `trans_language` = '" . $mysqli_noda->escape_string($lang) . "'
                AND `zeit_id` = ?", "i", $time_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        $result = $mysqli_noda->query_by_stmt("SELECT `zeit_name`
            FROM `zeiten`
            WHERE `zeit_id` = ?", "i", $time_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        return "";

    }

    /**
     * Static function for getting the name for a group.
     *
     * @param MDMysqli $mysqli_noda DB connection.
     * @param integer  $group_id    Group ID.
     *
     * @return string
     */
    public static function getGroupName(MDMysqli $mysqli_noda, int $group_id):string {

        $result = $mysqli_noda->query_by_stmt("SELECT `group_name`
            FROM `group`
            WHERE `group_id` = ?", "i", $group_id);

        if ($cur = $result->fetch_row()) {
            $result->close();
            return MD_STD_IN::sanitize_text($cur[0]);
        }
        $result->close();

        return "";

    }
}
