<?php

class Febe
{
    public static $fallbackLanguage = "en";
    private const availableLanguagesArray = array("de", "dk", "en", "es", "fi", "no");
    private const availableCountriesArray = array("DE", "DK", "EN", "ES", "FI", "NO");

    public function checkCountryLanguageAllegiance($country, $language)
    {
        $countryIso = $country != null ? strtolower($country) : $language;
        switch ($countryIso) {
            case "cn":
            case "hk":
            case "tw":
                return "cn";
            case "ar":
            case "bo":
            case "cl":
            case "co":
            case "cu":
            case "cr":
            case "do":
            case "ec":
            case "gt":
            case "gq":
            case "hn":
            case "mx":
            case "ni":
            case "pa":
            case "pe":
            case "pr":
            case "sv":
            case "uy":
            case "ve":
                return "es";
            case "be":
                return "fr";
            case "gb":
                return "en";
            case "at":
            case "ch":
            case "li":
            case "lu":
                return "de";
            case "ae":
            case "om":
            case "kw":
            case "qa":
            case "bh":
            case "sa":
            case "gcc":
                return "gcc";
            default:
                return $countryIso;
        }
    }

    public function storeCookie($name, $country, $language, $path)
    {
        setcookie($name, self::checkCountryLanguageAllegiance($country, $language), time() + (86400 * 30), $path);
    }

    /**
     * @return string
     */
    public function getHref()
    {
        return str_replace("\\", "/", substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], "/", 1) + 1));
    }

    public function redirectByLanguage($language): string
    {
        $language = self::checkCountryLanguageAllegiance(null, $language);
        if (in_array($language, self::availableLanguagesArray)) {
            return $language;
        } else {
            return self::$fallbackLanguage;
        }
    }

    public function redirectByCountry($country): string
    {
        $country = self::checkCountryLanguageAllegiance($country, null);
        if (in_array(strtoupper($country), self::availableCountriesArray)) {
            return strtolower($country);
        } else {
            return self::$fallbackLanguage;
        }
    }

    public function redirectByIpCountryIso($ipCountryIso): string
    {
        $ipCountryIso = self::checkCountryLanguageAllegiance($ipCountryIso, null);
        if (in_array(strtoupper($ipCountryIso), self::availableCountriesArray)) {
            return strtolower($ipCountryIso);
        } else {
            return self::$fallbackLanguage;
        }
    }


    public function setCurrentSubFolderLanguageCookie()
    {
        $langArray = array_filter(explode("/", $_SERVER['REQUEST_URI']), function ($item) {
            return in_array(self::checkCountryLanguageAllegiance(null, $item), self::availableLanguagesArray);
        });
        $langArray = array_values($langArray);
        if (!empty($langArray)) {
            self::storeCookie("intgrtn_language", null, $langArray[0], self::getHref());
        }
    }

    public function getFallbackLanguage(): string
    {
        return self::$fallbackLanguage;
    }
}