<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

//Vendor Autoloader
include(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/vendor/autoload.php');
include(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/mysyde/DcAutoloader.php');

$dcAutoloader = new DcAutoloader();
$dcAutoloader->register();

require_once("../../mysyde/common/common_functions.inc.php");
$envDir = rtrim(dirname(dirname(__DIR__)), '/') . '/config';
if (is_dir($envDir)) {
    $dotenv = new \Dotenv\Dotenv($envDir);
    $dotenv->load();
}

require_once((local_environment()) ? "../../mysyde/dc.config.php" : "../../mysyde/dc-server.config.php");
db_connect();

function blockReloadTime($seconds){
    $modiefied_date = new DateTime();
    $modiefied_date->modify('+ '.$seconds.' seconds');
    updateReloadTime($modiefied_date->format('d.m.Y H:i:s'));
}

function updateReloadTime($reload_time){
    $update = $GLOBALS['pdo_conn']->prepare("UPDATE main_infopoint_screens SET block_reload_active = :block_reload_active, block_reload_time = :block_reload_time WHERE hash = :hash");
    $update->execute(array(':block_reload_active' => '1', ':block_reload_time' => $reload_time, ':hash' => $_GET['hash']));
}

function updateReload(){
    $update = $GLOBALS['pdo_conn']->prepare("UPDATE main_infopoint_screens SET reload = :reload  WHERE hash = :hash");
    $update->execute(array(':reload' => '0', ':hash' => $_GET['hash']));
}

function getCurrentPreset(){
    $select_reload = $GLOBALS['pdo_conn']->prepare("SELECT main_infopoint_preset_id FROM main_infopoint_screens WHERE hash = :hash");
    $select_reload->execute(array(':hash' => $_GET['hash']));
    $res_reload = $select_reload->fetch(PDO::FETCH_ASSOC);
    return $res_reload['main_infopoint_preset_id'];
}

function getCurrentPage(){
    $select_reload = $GLOBALS['pdo_conn']->prepare("SELECT current_page FROM main_infopoint_screens WHERE hash = :hash");
    $select_reload->execute(array(':hash' => $_GET['hash']));
    $res_reload = $select_reload->fetch(PDO::FETCH_ASSOC);
    return $res_reload['current_page'];
}

function updateCurrentPage($current_page, $transition_id){
    $update_reload = $GLOBALS['pdo_conn']->prepare("UPDATE main_infopoint_screens SET current_page = :current_page, main_infopoint_transition_id = :main_infopoint_transition_id WHERE hash = :hash");
    $update_reload->execute(array(':current_page' => $current_page, ':main_infopoint_transition_id' => $transition_id, ':hash' => $_GET['hash']));
}

$GLOBALS['main_infopoint_preset_id'] = getCurrentPreset();

$select_presets = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_infopoint_preset_link LEFT JOIN main_infopoint_pages ON main_infopoint_pages.id = main_infopoint_preset_link.main_infopoint_page_id WHERE main_infopoint_preset_id = :main_infopoint_preset_id AND active = 1 ORDER BY sorting");
$select_presets->execute(array(':main_infopoint_preset_id' => $GLOBALS['main_infopoint_preset_id']));
$res_presets = $select_presets->fetchALL(PDO::FETCH_ASSOC);

function selectCurrentPage($sort){
    $select_plan = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_infopoint_pages LEFT JOIN main_infopoint_preset_link ON main_infopoint_pages.id = main_infopoint_preset_link.main_infopoint_page_id WHERE sorting = :sorting AND main_infopoint_preset_id = :main_infopoint_preset_id");
    $select_plan->execute(array(':sorting' => $sort, ':main_infopoint_preset_id' => $GLOBALS['main_infopoint_preset_id']));
    $res_plan = $select_plan->fetch(PDO::FETCH_ASSOC);
    return $res_plan;
}

function selectPageBySorting($sorting){
    $select_presets = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_infopoint_preset_link LEFT JOIN main_infopoint_pages ON main_infopoint_pages.id = main_infopoint_preset_link.main_infopoint_page_id WHERE main_infopoint_preset_id = :main_infopoint_preset_id AND active = 1 ORDER BY sorting");
    $select_presets->execute(array(':main_infopoint_preset_id' => $GLOBALS['main_infopoint_preset_id']));
    $res_presets = $select_presets->fetchALL(PDO::FETCH_ASSOC);
    return $res_presets[$sorting-1]['id'];
}

$count = count($res_presets);
$current_page_sort = getCurrentPage();
$current_page = selectCurrentPage($current_page_sort);
blockReloadTime($current_page['time']);

$next_page = $current_page_sort + 1;
if($current_page_sort >= $count){
    $next_page = 1;
}
$next_page_id = selectPageBySorting($next_page);
// echo $next_page_id;
updateReload();

function getTransitionID(){
    $select = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_infopoint_screens WHERE hash = :hash");
    $select->execute(array(':hash' => $_GET['hash']));
    $res_select = $select->fetch(PDO::FETCH_ASSOC);
    return $res_select;
}

function randomTransition(){
    $select = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_infopoint_transition WHERE type = :type AND active = 1 ORDER BY RAND() LIMIT 1");
    $select->execute(array(':type' => 'out'));
    $res_select = $select->fetch(PDO::FETCH_ASSOC);
    return $res_select;
}

function TransitionFromBefore($transition_id){
    $select = $GLOBALS['pdo_conn']->prepare("SELECT * FROM main_infopoint_transition WHERE parent = :parent");
    $select->execute(array(':parent' => $transition_id));
    $res_select = $select->fetch(PDO::FETCH_ASSOC);
    return $res_select;
}

$last_transition = getTransitionID();
$current_transition = TransitionFromBefore($last_transition['main_infopoint_transition_id']);
$next_transition = randomTransition();

updateCurrentPage($next_page, $next_transition['id']);
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <link href="../../layout/frontend/b2c/dist/css/infopoint.css?t=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/layout/frontend/b2c/dist/css/infopoint.css')?>" rel="stylesheet" type="text/css" />
</head>
<body class='<?php echo $current_transition['class'] ?>'>
    <?php 
        require_once('content_infopoint.php');
        loadContent($current_page)
    ?>
    <div class='ajax_timer'></div>
</body>
</html>
<script>
    function ajaxCall() {
        $.ajax({
            url: "reload_infopoint.php",
            type: "POST",
            data: {
                option: "reload",
                hash: "<?php echo $_GET['hash']?>",
                
            },
            success: function (data) {
                if(data == 1){
                    location.reload();
                }else if(data == 2){
                    $('body').addClass("<?php echo $next_transition['class'] ?>");
                }
            }
        });
        setTimeout(function () {
            ajaxCall();
        }, 1000);
    }
    ajaxCall();
</script>