<?php


namespace App\Components\DataGrids;


use App\Model\Entities\Localization\GuiTranslation;
use App\Model\Entities\Localization\Language;
use App\Model\Enums\FlashMessage;
use App\Model\Facades\TranslatorFacade;
use App\Model\Translator;
use Kdyby\Doctrine\QueryBuilder;
use Nette\Forms\Container;
use Throwable;
use TypeError;
use Ublaboo\DataGrid\DataGrid;

class LanguagesDataGrid extends BaseDataGrid
{
    /** @var TranslatorFacade */
    protected $translatorFacade;

    public function __construct(Translator $translator, TranslatorFacade $translatorFacade)
    {
        parent::__construct($translator);
        $this->translatorFacade = $translatorFacade;
        $this->enableAdder();
    }

    protected function setupDataSource() : QueryBuilder
    {
        return $this->translatorFacade->getLanguageReader()->getGridDataSource();
    }

    protected function setupColumns(DataGrid $grid)
    {
        $this->columnIdPreset($grid);
        $grid->addColumnText('identifier', 'Identifier', 'identifier')
            ->setSortable()
            ->setSortableResetPagination();
        $this->columnNamePreset($grid, true);
        $grid->addColumnText('completion', 'Completion')
            ->setRenderer([$this, 'calcCompletion']);
        $grid->addColumnStatus('status', 'Status', 'state')
            ->setSortable()
            ->setSortableResetPagination()
            ->setCaret(true)
            ->addOption(Language::AVAILABLE, 'Available')
                ->setClass('btn-success')
                ->endOption()
            ->addOption(Language::IN_PROGRESS, 'In progress')
                ->setClass('btn-primary')
                ->endOption()
            ->onChange[] = [$this, 'statusChangeWrapper'];

        $grid->getColumn('identifier')->getElementPrototype('th')->class('col-xs-5');
        $grid->getColumn('name')->getElementPrototype('th')->class('col-xs-7');
        $grid->getColumn('status')->getElementPrototype('th')->class('fit-column no-break');
    }

    protected function setupInlineEdit(DataGrid $grid)
    {
        $grid->getColumn('identifier')->setEditableCallback([$this, 'editIdentifier']);
    }

    public function setupAdder(Container $container)
    {
        $container->addText('identifier', '');
        $container->addText('name', '');
    }

    protected function processAdder($values)
    {
        $this->translatorFacade->getLanguageWriter()->registerNewLanguage($values->identifier, $values->name);
    }

    // results are too large - this will not work => translated directly on translation request create
//    protected function setupActions(DataGrid $grid)
//    {
//        $grid->addAction('translate', 'translator', 'translator!')
//            ->setIcon('windows')
//            ->setClass(function(Language $item){
//                return $item->getIdentifier() === 'en' ? 'btn btn-xs btn-primary disabled' : 'btn btn-xs btn-primary';
//            });
//    }
//
//    public function handleTranslator($id)
//    {
//        try {
//            $this->translatorFacade->getTranslationWriter()->autoTranslate($id);
//        } catch (Throwable $e) {
//            throw $e;
//        }
//    }


    protected function statusChange(int $id, string $state)
    {
        $this->translatorFacade->getLanguageWriter()->changeLanguageState($id, $state);
    }

    protected function editName(int $id, string $name)
    {
        $this->translatorFacade->getLanguageWriter()->editLanguageName($id, $name);
    }

    public function editIdentifier($id, $identifier)
    {
        try {
            $this->translatorFacade->getLanguageWriter()->editLanguageIdentifier($id, $identifier);
        } catch (TypeError $e) {
            $this->presenter->flashMessage('Identifier edit failed.', FlashMessage::ERROR);
            $this->redrawFlashMessages();
        }
    }

    public function calcCompletion(Language $item)
    {
        $translated = $this->translatorFacade->getTranslationReader()->countGuiTranslations($item, GuiTranslation::TRANSLATED);
        $all = $this->translatorFacade->getTranslationReader()->countGuiTranslations($item);

        return "$translated / $all";
    }
}