<?php

declare(strict_types=1);

namespace Plugin\cin_portlets_5\Portlets\KategorieSchnellsuche;

require \dirname(__DIR__, 2).'/src/warmup.php';

use Cin\Portlets5\PortletTrait;
use JTL\Catalog\Category\Kategorie;
use JTL\Catalog\Category\KategorieListe;
use JTL\Helpers\Category;
use JTL\OPC\InputType;
use JTL\OPC\Portlet;
use JTL\OPC\PortletInstance;

final class KategorieSchnellsuche extends Portlet
{
  use PortletTrait;

  private $fontAwesome = 'fas fa-tachometer-alt';

  public function getPropertyDesc(): array
  {
    return [
      'rootCategory' => [
        'type'    => InputType::NUMBER,
        'label'   => __('rootCategory'),
        'width'   => 25,
        'default' => 0,
        'min'     => 0,
        'desc'    => __('rootCategoryDescription')
      ],
      'sortCategory' => [
        'type'    => InputType::SELECT,
        'label'   => __('sortCategory'),
        'width'   => 75,
        'options' => [
          'ascendingByName'  => $this->__t('ascendingByName', __('ascendingByName')),
          'descendingByName' => $this->__t('descendingByName', __('descendingByName')),
          'ascendingByWawi'  => $this->__t('ascendingByWawi', __('ascendingByWawi')),
          'descendingByWawi' => $this->__t('descendingByWawi', __('descendingByWawi')),
        ],
        'default' => 'ascendingByName'
      ],
      'resetChoise' => [
        'type'    => InputType::CHECKBOX,
        'label'   => __('resetChoise'),
        'width'   => 33,
        'desc'    => __('resetChoiseDescription'),
        'default' => true
      ],
      'arrowLeft' => [
        'type'    => InputType::CHECKBOX,
        'label'   => __('arrowLeft'),
        'width'   => 33,
        'desc'    => __('arrowLeftDescription'),
        'default' => false
      ],
      'showBreadcrumbs' => [
        'type'    => InputType::CHECKBOX,
        'label'   => __('showBreadcrumbs'),
        'width'   => 33,
        'desc'    => __('showBreadcrumbsDescription'),
        'default' => false
      ],
      'namingMode' => [
        'type'    => InputType::SELECT,
        'label'   => __('namingMode'),
        'width'   => 100,
        'options' => [
          'default'     => __('default'),
          'auto'        => __('auto'),
          'defaultHead' => __('defaultHead'),
          'custom'      => __('customHeading'),
        ],
        'default'     => 'default',
        'childrenFor' => [
          'defaultHead' => [
            'invitationText_dh' => [
              'label' => __('invitationText'),
              'type'  => InputType::TEXT,
              'width' => 100,
              'desc'  => __('emptyForDefault')
            ]
          ],
          'custom' => [
            'invitationText_c' => [
              'label' => __('invitationText'),
              'type'  => InputType::TEXT,
              'width' => 100,
              'desc'  => __('emptyForDefault')
            ],
            'dropdownAlias' => [
              'type'  => InputType::TEXT_LIST,
              'label' => __('dropdownAlias'),
              'width' => 100
            ]
          ],
        ]
      ],
    ];
  }

  public function getConfigPanelHtml(PortletInstance $instance): string
  {
    return parent::getConfigPanelHtml($instance)
      .$this->extendConfigDialog($instance);
  }

  public function getPropertyTabs(): array
  {
    return [
      __('Styles') => 'styles',
    ];
  }

  public function getCategories(PortletInstance $instance): array
  {
    if ( ! $this->isLicensed($this->class)) {
      return [];
    }

    $categories = new KategorieListe();

    $categories = $categories->getChildCategories((int) $instance->getProperty('rootCategory'), 0, 0);

    switch ($instance->getProperty('sortCategory')) {
      case 'ascendingByName':
        usort($categories, static fn (Kategorie $a, Kategorie $b) => strcmp($a->getName(), $b->getName()));

        break;
      case 'descendingByName':
        usort($categories, static fn (Kategorie $a, Kategorie $b) => strcmp($a->getName(), $b->getName()));
        $categories = array_reverse($categories);

        break;
      case 'descendingByWawi':
        $categories = array_reverse($categories);

        break;
    }

    return $categories;
  }

  public function getDefaultText(PortletInstance $instance): string
  {
    switch ($instance->getProperty('namingMode')) {
      case 'auto':
        $category = Category::getInstance()->getCategoryById((int) $instance->getProperty('rootCategory'));

        return $category
        ? __('chooseCategoryFrom').$category->getName()
        : __('chooseCategory');

        break;
      case 'defaultHead':
        return $instance->getProperty('invitationText_dh') ?: __('chooseCategory');

        break;
      case 'custom':
        return $instance->getProperty('invitationText_c') ?: __('chooseCategory');

        break;
      case 'default':
      default:
        return __('chooseCategory');

        break;
    }
  }
}
