<?php

namespace Korkz\Demirteknoloji\traits;
trait Lang
{
    use ENVConfig;

    public static function langCustomStatic(string $location, string $page, string $lang): array
    {

        $hostName = self::staticEnv()["HOST_NAMES_SERVER"];
        $host = $_SERVER["HTTP_HOST"];
        $dirPath = ($host === "localhost") ? "/$hostName" : "";
        $langPath = $_SERVER["DOCUMENT_ROOT"] . $dirPath . "/App/languages/$lang.json";
        $result = [];
        if (file_exists($langPath)) {
            $langData = json_decode(file_get_contents($langPath), true);
            if (isset($langData[$location][$page]["custom"])) {
                $result["custom"] = $langData[$location][$page]["custom"];
            }
        }
        return $result;
    }

    /**
     * @param string $location
     * @param string $page
     * @param string $html
     * @param string $lang
     * @return array|string
     */
    public function langViews(string $location, string $page, string $html, string $lang): array|string
    {
        if (!isset($_SESSION["lang"])) {
            $_SESSION["lang"] = $this->getLang();
        }
        $hostName = $this->env()["HOST_NAMES_SERVER"];
        $host = $_SERVER["HTTP_HOST"];
        $dirPath = ($host === "localhost") ? "/$hostName" : "";
        $langPath = $_SERVER["DOCUMENT_ROOT"] . $dirPath . "/App/languages/$lang.json";
        if (file_exists($langPath)) {
            $langData = json_decode(file_get_contents($langPath), true);
            $incoming = $outgoing = [];
            if ($page !== "login" && isset($langData[$location]["global"])) {
                $incoming = $langData[$location]["global"]["incoming"] ?? [];
                $outgoing = $langData[$location]["global"]["outgoing"] ?? [];
            }
            if (isset($langData[$location][$page])) {
                $incoming = array_merge($incoming, $langData[$location][$page]["incoming"] ?? []);
                $outgoing = array_merge($outgoing, $langData[$location][$page]["outgoing"] ?? []);
            }
            $result = str_replace($incoming, $outgoing, $html);

        } else {
            $result = str_replace([], [], $html);
        }
        return $result;
    }

    public function getLang(): string
    {
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && str_starts_with(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2), "tr")) {
            $lang = $_SESSION["lang"] ?? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        } else {
            $lang = $_SESSION["lang"] ?? 'en'; // varsayılan olarak İngilizce kullan
        }
        return $lang;
    }

    public function langReply(string $location, string $page, string $lang): array
    {
        $hostName = $this->env()["HOST_NAMES_SERVER"];
        $host = $_SERVER["HTTP_HOST"];
        $dirPath = ($host === "localhost") ? "/$hostName" : "";
        $langPath = $_SERVER["DOCUMENT_ROOT"] . $dirPath . "/App/languages/$lang.json";
        $result = [];
        if (file_exists($langPath)) {
            $langData = json_decode(file_get_contents($langPath), true);
            if (isset($langData[$location][$page]["custom"]["conclusion"])) {
                $result["success"] = $langData[$location][$page]["custom"]["conclusion"]["success"];
                $result["errors"] = $langData[$location][$page]["custom"]["conclusion"]["errors"];
                /*
                if (count($langData[$location][$page]["custom"]["conclusion"]) === 3) {
                    $result["special"] = $langData[$location][$page]["custom"]["conclusion"][2];
                }
                */
            }
        }
        return $result;
    }

    /**
     * @param string $location
     * @param string $page
     * @param string $lang
     * @return array|mixed
     */
    public function langValidation(string $location, string $page, string $lang = "tr"): mixed
    {
        $hostName = $this->env()["HOST_NAMES_SERVER"];
        $host = $_SERVER["HTTP_HOST"];
        $dirPath = ($host === "localhost") ? "/$hostName" : "";
        $langPath = $_SERVER["DOCUMENT_ROOT"] . $dirPath . "/App/languages/$lang.json";
        if (file_exists($langPath)) {
            $langData = json_decode(file_get_contents($langPath), true);
            if (isset($langData[$location][$page]["custom"])) {
                return $langData[$location][$page]["custom"]["labels"];
            }
        }
        return [];
    }
}
