<?php
use \Psr\Http\Message\ResponseInterface as Response;
use \Psr\Http\Message\ServerRequestInterface as Request;

$app->post('/{username}/getstudentclassesshort', function (Request $request, Response $response, $args) {

    require __DIR__ . '/../../bootstrap.php';

    $body = $request->getParsedBody();

    // check the time
    $post4pm = false;
    if (date('H') >= 17 && date('H') <= 23) {
        $post4pm = true;
    }

    if (date('H') >= 1 && date('H') <= 7) {
        $post4pm = true;
    }

    $post4pm = true; // delete this line and school hour window will be deleted
    if ($post4pm == true) {
        $data = $entityManager->getRepository('Routine')->getStudentClassesShort($body);

        return $response->withJson($data, 200);
    } else {
        $data = null;
        return $response->withJson($data, 200);
    }

});


$app->post('/{username}/{username1}/{username2}/getstudentclassesshort', function (Request $request, Response $response, $args) {

    require __DIR__ . '/../../bootstrap.php';

    $body = $request->getParsedBody();

    // check the time
    $post4pm = false;
    if (date('H') >= 17 && date('H') <= 23) {
        $post4pm = true;
    }

    if (date('H') >= 1 && date('H') <= 7) {
        $post4pm = true;
    }

    $post4pm = true; // delete this line and school hour window will be deleted
    if ($post4pm == true) {
        $data = $entityManager->getRepository('Routine')->getStudentClassesShort($body);

        return $response->withJson($data, 200);
    } else {
        $data = null;
        return $response->withJson($data, 200);
    }

});




$app->post('/{username}/getstudentclasses', function (Request $request, Response $response, $args) {

    require __DIR__ . '/../../bootstrap.php';

    $body = $request->getParsedBody();

    $email = addslashes(strip_tags(trim($args['username'])));

    $data = $entityManager->getRepository('Routine')->getStudentClasses($body);

    return $response->withJson($data, 200);

});

$app->post('/{username}/{username1}/{username2}/getstudentclasses', function (Request $request, Response $response, $args) {

    require __DIR__ . '/../../bootstrap.php';

    $body = $request->getParsedBody();

    $email = addslashes(strip_tags(trim($args['username'])));

    $data = $entityManager->getRepository('Routine')->getStudentClasses($body);

    return $response->withJson($data, 200);

});


$app->post('/{username}/getstudentclassestomorrow', function (Request $request, Response $response, $args) {

    require __DIR__ . '/../../bootstrap.php';

    $body = $request->getParsedBody();

    $email = addslashes(strip_tags(trim($args['username'])));

    $data = $entityManager->getRepository('Routine')->getStudentClassesTomorrow($body);

    return $response->withJson($data, 200);

});

$app->post('/{username}/{username1}/{username2}/getstudentclassestomorrow', function (Request $request, Response $response, $args) {

    require __DIR__ . '/../../bootstrap.php';

    $body = $request->getParsedBody();

    $email = addslashes(strip_tags(trim($args['username'])));

    $data = $entityManager->getRepository('Routine')->getStudentClassesTomorrow($body);

    return $response->withJson($data, 200);

});

$app->get('/gettodaysday', function (Request $request, Response $response, $args) {

    require __DIR__ . '/../../bootstrap.php';
    
    $dayNames = array(
        'Sunday'=>'Sun',
        'Monday'=>'Mon', 
        'Tuesday'=>'Tue', 
        'Wednesday'=>'Wed', 
        'Thursday'=>'Thu', 
        'Friday'=>'Fri', 
        'Saturday'=>'Sat', 
     );

    $data = date('l');

    return $response->withJson($dayNames[$data], 200);

});

$app->post('/fetchtodaysdata', function (Request $request, Response $response, $args) {

    require __DIR__ . '/../../bootstrap.php';

    $body = $request->getParsedBody();

    // check the time
    $post4pm = true;
    if (date('H') >= 17 && date('H') <= 23) {
        $post4pm = true;
    }

    if (date('H') >= 1 && date('H') <= 7) {
        $post4pm = true;
    }

    $post4pm = true; // delete this line and school hour window will be deleted

    if($post4pm == true){
        $data = $entityManager->getRepository('Routine')->getOneClassDetails($body);
    }else{
        $data = null;
    }

    

    return $response->withJson($data, 200);

});
