<?php

/**
 * Class language
 *
 * 1xx Error codes
 */


class language
{
    public static $languages = array(
        'de_DE' => 'German',
        'en_EN' => 'English',

    );

    public const
        TITLE = 'TITLE',
        MAIN_TITLE = 'MAIN_TITLE',
        DESCRIPTION = 'DESCRIPTION',
        EXPLANATION_QUESTIONS = 'EXPLANATION_QUESTIONS',
        EXPLANATION_SCORE = 'EXPLANATION_SCORE',
        SCORE_FINISHED = 'SCORE_FINISHED',
        PROJECT_DESCRIPTION = 'PROJECT_DESCRIPTION',
        HELP_US = 'HELP_US',
        REQUEST_MORE = 'REQUEST_MORE',
        JOIN_DISCORD = 'JOIN_DISCORD',
        LANGUAGE_CHOOSE = 'LANGUAGE_CHOOSE',
        CALCULATE = 'CALCULATE'
    ;

    public static $lang_file;
    /**
     * @var string
     */
    public static $lang;


    public static function checkLanguage ( ): void
    {
        // Check cookie
        if( isset( $_GET['lang'] ) )
        {
            if( isset( self::$languages[$_GET['lang']] ) )
            {

                self::setLangauge( $_GET['lang'] );
            }
            else
            {
                self::setLangauge();
            }
        }
        else if ( isset( $_COOKIE['lang'], self::$languages[$_COOKIE['lang']] ) )
        {
                self::setLangauge( $_COOKIE['lang'] );
        }
        else
        {
            self::setLangauge();
        }
    }

    private static function setLangauge ( $a_language = 'en_EN' ): void
    {
        self::$lang = $a_language;
        if( !isset( $_COOKIE['lang'] ) || $_COOKIE['lang'] !== $a_language )
        {
            setcookie( 'lang', $a_language, 0 );
        }
        try
        {
            self::$lang_file = parse_ini_file( LANG_DIRECTORY . $a_language . '.ini', true );

        } catch( Exception $e )
        {
            throw new RuntimeException( 'Cannot load the language file!', 101 );
        }
    }

}