<?php
    $section_page_titles = [
        0 => '',
        1 => 'Home',
        2 => 'About myself',
        3 => 'What I\'ve done so far',
        4 => 'Who I would like to be',
        5 => 'Web Form contact'
    ];
    if ( ! isset ( $_POST ['section_id'] ) ) {
        $requested_section_title = 'Home';
    }
    else {
        if ( ! in_array ( $_POST ['section_id'], array_keys( $section_page_titles ) ) ) {
            $page_content_array = [
                'status' => 400,
                'errorCode' => 'Bad Request',
                'errorText' => 'Requested section \'' . $_POST [ 'section_id' ] . '\' does not exist: do not attempt to compromise the website.',
            ];
            $page_content_json = json_encode ( $page_content_array );
            header('Content-Type: application/json');
            echo $page_content_json;
            exit;
        }
        $requested_section = $_POST ['section_id'];
        if ( $requested_section == 0 ) {
            if ( isset ( $_SESSION [ 'section_id' ] ) ) {
                $requested_section = $_SESSION [ 'section_id' ];
            }
            else {
                $requested_section = 1;
            }
        }
        $_SESSION [ 'section_id' ] = $requested_section;
        $requested_section_title = $section_page_titles [ $requested_section ];
        $section_templates = [
            1 => file_get_contents ( 'lib/php/templates/home.html' ),
            2 => file_get_contents ( 'lib/php/templates/description.html' ),
            3 => file_get_contents ( 'lib/php/templates/projects.html' ),
            4 => file_get_contents ( 'lib/php/templates/ideas.html' ),
            5 => file_get_contents ( 'lib/php/templates/contact-form.html' ),
        ];
        $page_content_array = [
            'status' => 200,
            'pageTitle' => $requested_section_title.' | Bryan Pedini',
            'html' => $section_templates [ $requested_section ]
        ];
        $page_content_json = json_encode($page_content_array);
        header('Content-Type: application/json');
        echo $page_content_json;
        exit;
    }