<?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 = 6;
    const LENGTH_WORDS_MAX = 12;

    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);

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

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

    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);

        return [
            WordManager::wordToArray($randomWordNotChecked),
        ];
    }

    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()];

    }
}
