<?php

declare(strict_types=1);

namespace Kylin\Payment\Services;

use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\App;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Contracts\Container\Container;

final class LanguageService
{
    protected readonly Container $app;
    protected readonly Filesystem $files;
    protected string $prefix;

    public function __construct(Container $app, Filesystem $files)
    {
        $this->app = $app;
        $this->files = $files;
        $this->prefix = 'kylin-payment::payment.';
    }

    public function trans(string $key, array $replace = [], ?string $locale = null): string
    {
        return Lang::get($this->prefix . $key, $replace, $locale);
    }

    public function transPaymentType(string $type, ?string $locale = null): string
    {
        return $this->trans('payment_types.' . $type, [], $locale) ?? $type;
    }

    public function transPaymentStatus(string $status, ?string $locale = null): string
    {
        return $this->trans('payment_statuses.' . $status, [], $locale) ?? $status;
    }

    public function transPaymentAction(string $action, ?string $locale = null): string
    {
        return $this->trans('payment_actions.' . $action, [], $locale) ?? $action;
    }

    public function transError(string $key, array $replace = [], ?string $locale = null): string
    {
        return $this->trans('errors.' . $key, $replace, $locale) ?? $key;
    }

    public function transSuccess(string $key, array $replace = [], ?string $locale = null): string
    {
        return $this->trans('success.' . $key, $replace, $locale) ?? $key;
    }

    public function transMessage(string $key, array $replace = [], ?string $locale = null): string
    {
        return $this->trans('messages.' . $key, $replace, $locale) ?? $key;
    }

    public function transStatistics(string $key, array $replace = [], ?string $locale = null): string
    {
        return $this->trans('statistics.' . $key, $replace, $locale) ?? $key;
    }

    public function getCurrencySymbol(string $currency, ?string $locale = null): string
    {
        $symbols = $this->trans('currencies', [], $locale) ?? [];
        return $symbols[$currency] ?? $currency;
    }

    public function formatCurrency(float $amount, string $currency, ?string $locale = null): string
    {
        $symbol = $this->getCurrencySymbol($currency, $locale);
        return "{$symbol}" . number_format($amount, 2, '.', ',');
    }

    public function getCurrentLocale(): string
    {
        return App::currentLocale();
    }

    public function setLocale(string $locale): void
    {
        App::setLocale($locale);
    }

    public function getSupportedLocales(): array
    {
        $langPath = __DIR__ . '/../../resources/lang';
        $locales = [];

        if ($this->files->exists($langPath)) {
            $directories = $this->files->directories($langPath);
            foreach ($directories as $directory) {
                $locale = basename($directory);
                $locales[$locale] = $this->getLocaleName($locale);
            }
        }

        return $locales;
    }

    protected function getLocaleName(string $locale): string
    {
        $localeNames = [
            'zh-CN' => '简体中文',
            'en' => 'English',
            'ja' => '日本語',
            'ko' => '한국어',
            'de' => 'Deutsch',
            'fr' => 'Français',
        ];

        return $localeNames[$locale] ?? $locale;
    }

    public function getSupportedCurrencies(): array
    {
        return [
            'CNY' => '人民币',
            'USD' => '美元',
            'EUR' => '欧元',
            'JPY' => '日元',
            'KRW' => '韩元',
            'GBP' => '英镑',
            'HKD' => '港币',
            'TWD' => '新台币',
            'AUD' => '澳元',
            'CAD' => '加元',
            'SGD' => '新加坡元',
            'MYR' => '马来西亚林吉特',
            'THB' => '泰铢',
            'VND' => '越南盾',
            'IDR' => '印尼盾',
            'PHP' => '菲律宾比索',
            'INR' => '印度卢比',
            'RUB' => '俄罗斯卢布',
            'BRL' => '巴西雷亚尔',
            'ARS' => '阿根廷比索',
            'MXN' => '墨西哥比索',
        ];
    }

    public function isLocaleSupported(string $locale): bool
    {
        return array_key_exists($locale, $this->getSupportedLocales());
    }

    public function isCurrencySupported(string $currency): bool
    {
        return array_key_exists($currency, $this->getSupportedCurrencies());
    }
}
