<?php

/**
 * Class website
 * 2xx error codes
 */


class website
{

    public function buildWebsite(): string
    {
        try
        {
            $website = file_get_contents( __DIR__ . '/website.html' );
            $website = $this->replaceDefaultThings( $website );

            $website = $this->setQuestions( $website );
            return $website;

        } catch( Exception $e )
        {
            if( ( $code = $e->getCode() ) === 202 || $code === 203 )
            {
                throw new RuntimeException( $e->getMessage(), $code );
            }
            throw new RuntimeException( 'Cannot load the website.html file', 201 );
        }
    }

    private function replaceDefaultThings( $website )
    {
        if( !isset( language::$lang_file[language::TITLE],
            language::$lang_file[language::MAIN_TITLE],
            language::$lang_file[language::DESCRIPTION],
            language::$lang_file[language::EXPLANATION],
            language::$lang_file[language::PROJECT_DESCRIPTION],
            language::$lang_file[language::HELP_US],
            language::$lang_file[language::REQUEST_MORE],
            language::$lang_file[language::LANGUAGE_CHOOSE],
            language::$lang_file[language::JOIN_DISCORD] ) )
        {
            throw new RuntimeException( 'Cannot load default langauge replacements', 202 );
        }
        $website = str_replace(
            array(
                '[LANG]', '[TITLE]', '[MAIN_TITLE]', '[DESCRIPTION]', '[EXPLANATION]', '[PROJECT_DESCRIPTION]', '[HELP_US]', '[REQUEST_MORE]','[LANGUAGE_CHOOSE]', '[JOIN_DISCORD]' ),
            array(
                substr( language::$lang, 0, 2 ),
                language::$lang_file[language::TITLE],
                language::$lang_file[language::MAIN_TITLE],
                language::$lang_file[language::DESCRIPTION],
                language::$lang_file[language::EXPLANATION],
                language::$lang_file[language::PROJECT_DESCRIPTION],
                language::$lang_file[language::HELP_US],
                language::$lang_file[language::REQUEST_MORE],
                language::$lang_file[language::LANGUAGE_CHOOSE],
                language::$lang_file[language::JOIN_DISCORD]
            ), $website );
        $languages = '';
        foreach( language::$languages as $code => $language )
        {
            $languages .= <<<HTML
<li><a class="blue-text" href="?lang=$code">$language</a></li>
HTML;

}
        $website = str_replace( '[LANGUAGES]', $languages, $website );
        return $website;
    }

    private function setQuestions( $website ) : string
    {
        if( !isset( language::$lang_file['questions'] ) )
        {
            throw new RuntimeException( 'Error while reading the questions in langauge file', 203 );
        }
        $questions = '';
        foreach( language::$lang_file['questions'] as $number => $question )
        {
            $questions .= <<<CHECKBOX
<label>
        <input name="question_$number" type="checkbox" class="filled-in z-depth-4" />
        <span>$question</span>
</label><br>
CHECKBOX;

        }
        $form_start = <<<HTML
<form action="" method="post">
HTML;
        $submit = language::$lang_file[language::CALCULATE];
        $form_end = <<<HTML
<br>
<button class="btn waves-effect btn-large waves-light " type="submit" name="action">{$submit}
    <i class="material-icons right">send</i>
</button>
</form>
HTML;


        $questions = $form_start . $questions . $form_end;
        $website = str_replace( '[QUESTIONS]', $questions, $website );


        return $website;
    }
}