<?php

namespace Application\Controller;

use Ainias\Core\Controller\OnlineController;
use Application\Model\Manager\LevelManager;
use Application\Model\Manager\WordManager;
use Application\Model\Word;
use Zend\View\Model\ViewModel;

class IndexController extends OnlineController
{
    const LENGTH_WORDS_MIN = 8;
    const LENGTH_WORDS_MAX = 8;

    const NUMBER_WORDS_TO_CHECK_SIMULTANEOUSLY = 8;

    public function indexAction()
    {
        /** @var SelectWordsBot $bot */
//        $bot = $this->get(SelectWordsBot::class);
//        $bot->setWebhook("");
//        $bot->handleUpdates($bot->getUpdates());
//        $bot->sendWordToCheck();
        return new ViewModel();
    }

    public function selectWordsAction()
    {
        /** @var WordManager $wordManager */
        $wordManager = $this->get(WordManager::class);
        $wordsNotChecked = $wordManager->countNotChecked(self::LENGTH_WORDS_MIN, self::LENGTH_WORDS_MAX);
        $wordsDeleted = $wordManager->countDeleted(self::LENGTH_WORDS_MIN, self::LENGTH_WORDS_MAX);
        $wordsChecked = $wordManager->countChecked(self::LENGTH_WORDS_MIN, self::LENGTH_WORDS_MAX);
        $wordsUnsure = $wordManager->countUnsure(self::LENGTH_WORDS_MIN, self::LENGTH_WORDS_MAX);
        $wordsNotUsed = $wordManager->countNotUsed(self::LENGTH_WORDS_MIN, self::LENGTH_WORDS_MAX);

        $wordsToCheck = [];
        for ($i = 0; $i < self::NUMBER_WORDS_TO_CHECK_SIMULTANEOUSLY; $i++) {
            $word = $wordManager->getRandomWordNotChecked(self::LENGTH_WORDS_MIN, self::LENGTH_WORDS_MAX);
            if ($word != null) {
                $wordsToCheck[] = WordManager::wordToArray($word);
            }
        }

        return [
            "wordsNotChecked" => $wordsNotChecked,
            "wordsDeleted" => $wordsDeleted,
            "wordsChecked" => $wordsChecked,
            "wordsUnsure" => $wordsUnsure,
            "wordsToCheck" => $wordsToCheck,
            "wordsNotUsed" => $wordsNotUsed,
        ];
    }

    public function changeCheckedAction()
    {
        $request = $this->getRequest();

        if (!$request->isPost()) {
            return $this->triggerDispatchError(403);
        }

        $wordId = $request->getPost("wordId", "");
        $action = $request->getPost("action", "");

        /** @var WordManager $wordManager */
        $wordManager = $this->get(WordManager::class);
        $word = $wordManager->getEntityById($wordId);

        if ($word == null) {
            return $this->triggerDispatchError(404);
        }

        $word = WordManager::doAction($word, $action);
        $wordManager->save($word);

        $randomWordNotChecked = $wordManager->getRandomWordNotChecked(self::LENGTH_WORDS_MIN, self::LENGTH_WORDS_MAX);

        if ($randomWordNotChecked != null) {
            return [
                WordManager::wordToArray($randomWordNotChecked),
            ];
        }
        return [
            "id" => -1,
            "word" => "<i>Kein Wort mehr!</i>",
//            "created" => $word->getCreated()->format(SyncController::DATETIME_SYNC_FORMAT),
//            "lastUpdated" => $word->getLastUpdated()->format(SyncController::DATETIME_SYNC_FORMAT),
//            "language" => $word->getLang(),
//            "deleted" => $word->isDeleted(),
        ];
    }

    public function getDoubleUsedWordsAction()
    {
        /** @var LevelManager $levelManager */
        $levelManager = $this->get(LevelManager::class);
        return $levelManager->getDoubleUsedWords();
    }

    public function deleteLevelAction()
    {
        $request = $this->getRequest();

        if (!$request->isPost()) {
            return $this->triggerDispatchError(403);
        }

        $levelId = $request->getPost("levelId", "");

        /** @var LevelManager $levelManager */
        $levelManager = $this->get(LevelManager::class);
        $level = $levelManager->getEntityById($levelId);
        $level->setDeleted(true);
        $level->setLastUpdated(new \DateTime());

        $levelManager->save($level);

        return [];
    }

    public function generateLevelsAction()
    {
        /** @var LevelManager $levelManager */
        $levelManager = $this->get(LevelManager::class);
        return ["levelsGenerated" => $levelManager->generateLevels()];

    }
}
