<?php
namespace Avision;

class Avision_I18n {
    private $current_language = 'en';
    private $supported_languages = ['en', 'fr', 'es'];
    private $translations = [];

    public function init() {
        $this->detect_language();
        $this->load_translations();
    }

    private function detect_language() {
        if (isset($_GET['lang']) && in_array($_GET['lang'], $this->supported_languages, true)) {
            $this->current_language = $_GET['lang'];
            $exp = time() + (86400 * 365);
            setcookie('avision_lang', $this->current_language, $exp, '/');
            setcookie('avi_lang', $this->current_language, $exp, '/');
        } elseif (isset($_COOKIE['avi_lang']) && in_array($_COOKIE['avi_lang'], $this->supported_languages, true)) {
            $this->current_language = $_COOKIE['avi_lang'];
        } elseif (isset($_COOKIE['avision_lang']) && in_array($_COOKIE['avision_lang'], $this->supported_languages, true)) {
            $this->current_language = $_COOKIE['avision_lang'];
        }
    }

    private function load_translations() {
        $lang_file = AVISION_PLUGIN_DIR . 'languages/' . $this->current_language . '.json';
        if (file_exists($lang_file)) {
            $content = file_get_contents($lang_file);
            $this->translations = json_decode($content, true) ?? [];
        }
    }

    public function get($key, $default = '') {
        return $this->translations[$key] ?? $default;
    }

    public function get_current_language() {
        return $this->current_language;
    }

    public function get_supported_languages() {
        return $this->supported_languages;
    }

    public function get_all_translations() {
        return $this->translations;
    }

    public function get_currency() {
        return $this->current_language === 'en' ? 'USD' : 'EUR';
    }

    public function get_currency_symbol() {
        return $this->current_language === 'en' ? '$' : '€';
    }

    public function get_price($type = 'dashboard') {
        $symbol = $this->get_currency_symbol();
        if ($type === 'dashboard') {
            return $symbol . '99';
        } elseif ($type === 'automation') {
            return $symbol . '29/month';
        }
        return $symbol . '0';
    }
}
