<?php
namespace apexl\Io\modules\display\components;

use apexl\Io\includes\Entity;
use apexl\Io\includes\Routes;
use apexl\Io\modules\component\entities\componentEntity;
use FastRoute\Route;

class EntityDisplayTile extends componentEntity {

    protected $entity;

    protected $componentWrapper;
    protected $footer;
    protected $table;

    protected $enableView = TRUE;
    protected $enableEdit = TRUE;
    protected $enableDelete = TRUE;


    public function __construct(Entity $entity)
    {
        parent::__construct();
        $this->setName("rowWrapper");
        $this->addClass('data-tile');
        $this->setColWidth('lg', 12);
        $this->setColWidth('xl', 6);

        $this->componentWrapper = (new RowWrapper())->resetClasses()->resetColumns()->addClass('content-wrapper');
        $this->addClass($entity->getEntityTrueName().'__tile-display');
        $this->entity = $entity;
        $this->addComponent($this->componentWrapper);
    }

    public function addContentTitle($title, $content = '', $header = 3){
        $contentTitle = (new ContentTitle())->addTitle($title)->addContent($content)->addHeader($header);
        $this->componentWrapper->addComponent($contentTitle);
        return $this;
    }

    public function addCustomTable(EntityDisplayTable $table){
        $this->table = $table;
        $this->componentWrapper->addComponent($this->table);
        return $this;
    }

    public function addTable($filters = null, $pagination = FALSE, $dataSrc = null, $viewButton=true, $editButton=true, $deleteButton=true){
        $this->table = (new EntityDisplayTable($this->entity))->addClass('w-100');
        $this->table->displayPagination($pagination);
        $this->table->displayShortTotals();
        if($dataSrc){
            $this->table->src(Routes::getRoutePattern($dataSrc));
        }
        if($filters){
            $this->table->setFilters($filters);
        }

        if ($viewButton) $this->table->addViewButton($this->entity->getEntityName() . '.display.get', $this->enableView ? FALSE : TRUE);
        if ($editButton) $this->table->addEditButton($this->entity->getEntityName() . '.display.put', $this->enableEdit ? FALSE : TRUE);
        if ($deleteButton) $this->table->addDeleteButton($this->entity->getEntityName() . '.display.delete', $this->enableDelete ? FALSE : TRUE);

        $this->componentWrapper->addComponent($this->table);
        return $this;
    }

    public function hideDisabledButtons($value = TRUE){
        $this->table->hideDisabled($value);
        return $this;
    }

    public function addButtonFooter(array $routes){
        $footer = (new RowWrapper())->addClass('tile-footer');
        foreach ($routes as $route => $title){
            $footer->addComponent((new SimpleButton())->addRoute(Routes::getRoutePattern($route))->addText($title));
        }
        $this->componentWrapper->addComponent($footer);
        return $this;
    }
}