<?php

namespace AutoAdmin\Bundle\Core\Util;

/**
 * https://github.com/TakahikoKawasaki/nv-i18n/blob/master/src/main/java/com/neovisionaries/i18n/LocaleCode.java
 */
class LocaleCode
{
    public function __construct(
        /**
         * format is http://en.wikipedia.org/wiki/ISO_639-1
         */
        protected string $language,
        /**
         * format is http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
         */
        protected ?string $country = null,
    ) {
    }

    public function getLanguage(): string
    {
        return $this->language;
    }

    public function getCountry(): ?string
    {
        return $this->country;
    }

    public function __toString(): string
    {
        return $this->country
            ? sprintf("%s_%s", $this->language, $this->country)
            : sprintf("%s", $this->language);
    }

    public static function getFirstCountryFromCode(string $languageCode, bool $fallbackIfNull = false): ?string
    {
        /** @var LocaleCode $localeCode */
        foreach (self::values() as $localeCode) {
            if ($localeCode->getLanguage() === $languageCode && $localeCode->getCountry() !== null) {
                return $localeCode;
            }
        }

        return $fallbackIfNull ? strtolower($languageCode)."_".strtoupper($languageCode) : null;
    }

    public static function values(): array
    {
        return [
            new self('af'),
            new self('af', 'ZA'),
            new self('ar'),
            new self('ar', 'SA'),
            new self('ar', 'EG'),
            new self('az'),
            new self('az', 'AZ'),
            new self('bg'),
            new self('bg', 'BG'),
            new self('bn'),
            new self('bn', 'BD'),
            new self('ca'),
            new self('ca', 'ES'),
            new self('cs'),
            new self('cs', 'CZ'),
            new self('da'),
            new self('da', 'DK'),
            new self('de'),
            new self('de', 'DE'),
            new self('el'),
            new self('el', 'GR'),
            new self('en'),
            new self('en', 'GB'),
            new self('en', 'US'),
            new self('en', 'AU'),
            new self('es'),
            new self('es', 'ES'),
            new self('es', 'MX'),
            new self('et'),
            new self('et', 'EE'),
            new self('eu'),
            new self('eu', 'ES'),
            new self('fa'),
            new self('fa', 'IR'),
            new self('fi'),
            new self('fi', 'FI'),
            new self('fr'),
            new self('fr', 'FR'),
            new self('fr', 'CA'),
            new self('he'),
            new self('he', 'IL'),
            new self('hi'),
            new self('hi', 'IN'),
            new self('hr'),
            new self('hr', 'HR'),
            new self('hu'),
            new self('hu', 'HU'),
            new self('id'),
            new self('id', 'ID'),
            new self('is'),
            new self('is', 'IS'),
            new self('it'),
            new self('it', 'IT'),
            new self('ja'),
            new self('ja', 'JP'),
            new self('ka'),
            new self('ka', 'GE'),
            new self('kk'),
            new self('kk', 'KZ'),
            new self('ko'),
            new self('ko', 'KR'),
            new self('lt'),
            new self('lt', 'LT'),
            new self('lv'),
            new self('lv', 'LV'),
            new self('mk'),
            new self('mk', 'MK'),
            new self('mn'),
            new self('mn', 'MN'),
            new self('ms'),
            new self('ms', 'MY'),
            new self('nl'),
            new self('nl', 'NL'),
            new self('no'),
            new self('no', 'NO'),
            new self('pl'),
            new self('pl', 'PL'),
            new self('pt'),
            new self('pt', 'PT'),
            new self('pt', 'BR'),
            new self('ro'),
            new self('ro', 'RO'),
            new self('ru'),
            new self('ru', 'RU'),
            new self('sk'),
            new self('sk', 'SK'),
            new self('sl'),
            new self('sl', 'SI'),
            new self('sq'),
            new self('sq', 'AL'),
            new self('sr'),
            new self('sr', 'RS'),
            new self('sv'),
            new self('sv', 'SE'),
            new self('ta'),
            new self('ta', 'IN'),
            new self('th'),
            new self('th', 'TH'),
            new self('tr'),
            new self('tr', 'TR'),
            new self('uk'),
            new self('uk', 'UA'),
            new self('uz'),
            new self('uz', 'UZ'),
            new self('vi'),
            new self('vi', 'VN'),
            new self('zh'),
            new self('zh', 'CN'),
            new self('zh', 'TW'),
        ];
    }
}
