<?php

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


class website
{

    public function showScore( ) : string
    {
        try {
            $website = file_get_contents( __DIR__ . '/website.html' );

            $website = $this->replaceDefaultThings( $website );

            $website = $this->setScore( $website );
            return $website;
        } catch( Exception $e )
        {
            throw new RuntimeException( 'Cannot load the score file', 201 );
        }
    }

    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::SCORE_FINISHED],
            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]', '[SCORE_FINISHED]', '[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::SCORE_FINISHED],
                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
    {
        $website = str_replace( '[EXPLANATION]', language::$lang_file[language::EXPLANATION_QUESTIONS], $website );

        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 style="font-size: 1.15em;">
        <input name="question_$number" type="checkbox" class="filled-in" />
        <span class="black-text text-lighten-3">$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="submit" value="checked">{$submit}
    <i class="material-icons right">send</i>
</button>
</form>
HTML;


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


        return $website;
    }

    private function calculateScore() : int
    {

        $score = count( language::$lang_file['questions'] );
        foreach( language::$lang_file['questions'] as $number => $question )
        {
            if( isset( $_POST["question_$number"] ) && $_POST["question_$number"] === 'on' )
            {
                $score--;
            }
        }
        return $score;
    }

    private function setScore ( string $a_website ): string
    {
        $a_website = str_replace( '[EXPLANATION]', language::$lang_file[language::EXPLANATION_SCORE], $a_website );
        $score = $this->calculateScore();

        $max = count( language::$lang_file['questions'] );
        $back = language::$lang_file['BACK'];
        $html = <<<HTML
<div class="row">
    <div class="col s12">
        <div class="card blue-grey darken-1">
            <div class="card-content white-text">
                <span class="card-title">[SCORE_TITLE]</span>
                <div class="row">
                    <div class="col s5">
                        <p>[SCORE_DESCRIPTION]</p>  
                        
                    </div>
                   
                        
                    <div class="col s2 l2">
                        <span id="score" class="deep-orange-text" style="font-size: 8.8em; font-family: PressStart2P, serif">$max</span>
                    </div>
                    
                    <div class="col s4"></div>
                </div>
            </div>
            <div class="card-action right-align">
                <a href="#">[SCORE_SHARE] <i class="material-icons" style="font-size: 1.2em;">share</i></a>
                <a href="#">[SCORE_SAVE] <i class="material-icons" style="font-size: 1.2em;">save</i></a>
            </div>
        </div>
    </div>
   
      
</div>
<div style="padding-left: 1em;">
    <button class="btn waves-effect btn-large waves-light" type="submit" name="submit" value="checked" onclick="location.href = '?';"">{$back}
    <i class="material-icons right">arrow_back</i>
</button>
</div>
HTML;

        $html = str_replace( ['[SCORE_TITLE]', '[SCORE_DESCRIPTION]', '[SCORE_SHARE]', '[SCORE_SAVE]'], [language::$lang_file['SCORE_TITLE'], language::$lang_file['SCORE_DESCRIPTION'], language::$lang_file['SCORE_SHARE'], language::$lang_file['SCORE_SAVE']], $html );

        $script = <<<JS
if( {$score} !== {$max} )
{
        animateValue("score", {$max}, {$score}, 600);   
}
JS;

        $a_website = str_replace( array( '[SCRIPT_CONTENT]', '[CONTENT]' ), array( $script, $html ), $a_website );
        return $a_website;
    }
}