<?php

namespace Application\Model\Manager;

use Ainias\Core\Model\Manager\StandardManager;
use Application\Controller\SyncController;
use Application\Model\Repository\WordRepository;
use Application\Model\Word;

class WordManager extends StandardManager
{
    const ACTION_CHECKED = 1;
    const ACTION_UNSURE = 2;
    const ACTION_DELETED = 3;

    /** @var WordRepository  */
    protected $repository;

    public function __construct(WordRepository $repository, Word $entity = null)
    {
        $this->repository = $repository;
        parent::__construct($repository, $entity);
    }

    /**
     * @param Word $word
     * @param $action
     * @return mixed
     */
    public static function doAction($word, $action)
    {
        switch($action)
        {
            case self::ACTION_UNSURE:
            {
                $word->setChecked(-1);
                break;
            }
            case self::ACTION_CHECKED:
            {
                $word->setChecked(1);
                $word->setDeleted(false);
                break;
            }
            case self::ACTION_DELETED:
            {
                $word->setChecked(1);
                $word->setDeleted(true);
                break;
            }
        }
        $word->setLastUpdated(new \DateTime());

        return $word;
    }

    /**
     * @param int $id
     * @return Word
     */
    public function getEntityById($id)
    {
        return parent::getEntityById($id);
    }

    /**
     * @param \DateTime $dateTime
     * @param int $offset
     * @return array
     */
    public function findNewerThan($dateTime, $offset = 0)
    {
        return $this->repository->findNewerThanDate($dateTime, $offset);
    }

    public function countNewerThan($dateTime)
    {
        return $this->repository->countNewerThanDate($dateTime);
    }

    static function wordToArray(Word $word)
    {
        return [
            "id" => $word->getId(),
            "word" => $word->getWord(),
//            "created" => $word->getCreated()->format(SyncController::DATETIME_SYNC_FORMAT),
//            "lastUpdated" => $word->getLastUpdated()->format(SyncController::DATETIME_SYNC_FORMAT),
            "language" => $word->getLang(),
            "deleted" => $word->isDeleted(),
        ];
    }

    public function wordsToArray($words)
    {
        $returnArray = [];
        /** @var Word $word */
        foreach ($words as $word)
        {
            $returnArray[] = $this->wordToArray($word);
        }
        return $returnArray;
    }

    public function getRandomWordNotChecked($minLength = 0, $maxLength = 100)
    {
        return $this->repository->getRandomWordNotChecked($minLength, $maxLength);
    }

    public function countNotChecked($minLength = 0, $maxLength = 100)
    {
        return $this->repository->countNotChecked($minLength, $maxLength);
    }

    public function countDeleted($minLength = 0, $maxLength = 100)
    {
        return $this->repository->countDeleted($minLength, $maxLength);
    }

    public function countChecked($minLength = 0, $maxLength = 100)
    {
        return $this->repository->countChecked($minLength, $maxLength);
    }

    public function countUnsure($minLength = 0, $maxLength = 100)
    {
        return $this->repository->countUnsure($minLength, $maxLength);
    }

    /**
     * @param $length
     * @return Word[]
     */
    public function findWordsForLength($length)
    {
       return $this->repository->findWordsForLength($length);
    }

    public function countNotUsed($minLength = 0, $maxLength = 100)
    {
        return $this->repository->countNotUsed($minLength, $maxLength);
    }
}