<?php

namespace App\Controller;

use App\Entity\Cas;
use App\Repository\CasRepository;
use App\Repository\UciteleRepository;
use App\Repository\UserRepository;
use App\Repository\ZakRepository;
use App\Repository\ZpravaRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class MainController extends AbstractController
{
    /**
     * @Route("/", name="home")
     * @param UserRepository $userRepository
     * @param ZakRepository $zakRepository
     * @param UciteleRepository $uciteleRepository
     * @param CasRepository $casRepository
     * @param ZpravaRepository $zpravaRepository
     * @return Response
     */
    public function index(UserRepository $userRepository, ZakRepository $zakRepository,
                          UciteleRepository $uciteleRepository, CasRepository $casRepository, ZpravaRepository $zpravaRepository)
    {
        $users = $userRepository->findAll();
        $result = null;
        $checkU = false;
        $vysledek = 0;
        if($this -> getUser() != null) {
            $user = $this->getUser()->getUsername();
            $rozvrh = null;
            foreach ($zakRepository -> findAll() as $z) {
                if ($z->getUser() != null && $z->getUser()->getUsername() == $user) {
                    $rozvrh = $z->getTrida()->getCas();
                }
            }
            foreach ($uciteleRepository -> findAll() as $u) {
                if ($u->getUser() != null && $u->getUser()->getUsername() == $user) {
                    foreach ($casRepository -> findAll() as $t)
                    {
                        if($t -> getUcitel() == $u)
                        {
                            $checkU = true;
                            $rozvrh = $t;
                        }}}}
            if($rozvrh != null)
            {
                if($checkU) $result = $this -> findTime($rozvrh);
                foreach ($rozvrh as $r)
                {
                    $r -> getId();
                    $result = $this -> findTime($r);
                }
            }
            $us = $this -> getUser();
            $zpravy = $zpravaRepository -> findAll();
            foreach ($zpravy as $zprava)
            {
                if($zprava -> getPrijemce() -> getId() == $us -> getId() && $zprava -> getPrecteno() == false)
                {
                   $vysledek++;
                }}}
        return $this->render('home/index.html.twig', ['users' => $users, 'rozvrh' => $result, 'zpravy' => $vysledek]);
    }

    public function findTime(Cas $rozvrh)
    {
        $result = null;
        $den = null;
        if (date("D") == "Mon") $den = "Pondělí";
        if (date("D") == "Tue") $den = "Úterý";
        if (date("D") == "Wed") $den = "Středa";
        if (date("D") == "Thu") $den = "Čtvrtek";
        if (date("D") == "Fri") $den = "Pátek";

        if ($rozvrh -> getDen() == $den) {
            if (800 <= date("Hi") + 100 && date("Hi") + 100 <= 2330) {
                if($rozvrh -> getDoba() -> getId() == 2) $result = $rozvrh;
            }
            if (855 <= date("Hi") + 100 && date("Hi") + 100 <= 959) {
                if($rozvrh -> getDoba() -> getId() == 3) $result = $rozvrh;
            }
            if (1000 <= date("Hi") + 100 && date("Hi") + 100 <= 1054) {
                if($rozvrh -> getDoba() -> getId() == 4) $result = $rozvrh;
            }
            if (1055 <= date("Hi") + 100 && date("Hi") + 100 <= 1149) {
                if($rozvrh -> getDoba() -> getId() == 5) $result = $rozvrh;
            }
            if (1150 <= date("Hi") + 100 && date("Hi") + 100 <= 1244) {
                if($rozvrh -> getDoba() -> getId() == 6) $result = $rozvrh;
            }
            if (1245 <= date("Hi") + 100 && date("Hi") + 100 <= 1334) {
                if($rozvrh -> getDoba() -> getId() == 7) $result = $rozvrh;
            }
            if (1335 <= date("Hi") + 100 && date("Hi") + 100 <= 1424) {
                if($rozvrh -> getDoba() -> getId() == 8) $result = $rozvrh;
            }
            if (1425 <= date("Hi") + 100 && date("Hi") + 100 <= 1514) {
                if($rozvrh -> getDoba() -> getId() == 9) $result = $rozvrh;
            }
            if (1515 <= date("Hi") + 100) {
                $result = null;
            }
        }

        return $result;
    }

}


