<?php

namespace Apexl\Io\Admin\Display\Components;

use Apexl\Io\Component\Collections\ComponentCollection;
use Apexl\Io\Component\Collections\ComponentPropertyCollection;
use Apexl\Io\Component\Components\Component;
use Apexl\Io\Component\Factories\ComponentPropertyFactory;
use Apexl\Io\Component\Factories\GenericComponentFactory;
use Apexl\Io\Display\Components\ContentTitle;
use Apexl\Io\Display\Components\LinkTile;
use Apexl\Io\Display\Components\RowWrapper;
use Arrayy\Type\StringCollection;

readonly class MenuTile extends Component
{
  protected RowWrapper $componentWrapper;

  public function __construct(
      private GenericComponentFactory $componentFactory,
      ComponentPropertyFactory $propertyFactory,
      ComponentPropertyCollection $props,
      ComponentCollection $components,
      StringCollection $classes,
      ?string $componentName = null,
      ?string $componentId = null
  ) {
    parent::__construct(
        $propertyFactory,
        $props,
        $components,
        $classes,
        $componentName,
        $componentId
    );

    $this->componentWrapper = $this->componentFactory->make(RowWrapper::class)
        ->resetClasses()
        ->addClass('content-wrapper');
  }

  public function init(?string $title = null): void
  {
    $this->addClass('menu-tile-wrapper col-md-6 col-sm-6');

    $this->addComponent($this->componentWrapper);
    if ($title) {
      $this->addTitle($title);
    }
  }

  public function addTitle($title): static
  {
    $titleComponent = $this->componentFactory->make(ContentTitle::class)
        ->addTitle($title)->
        addClass('tileTitle')
        ->addHeader(5);

    $this->componentWrapper->addComponent($titleComponent);

    return $this;
  }

  public function addButton($route, $name, $icon = ''): static
  {
    $linkComponent = $this->componentFactory
        ->make(LinkTile::class)
        ->addRoute($route)
        ->addText($name)
        ->addIcon($icon);

    $this->componentWrapper->addComponent($linkComponent);

    return $this;
  }
}
