<?php

declare(strict_types=1);

namespace Plugin\cin_portlets_5\Portlets\HerstellerSlider;

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

use Cin\Portlets5\PortletTrait;
use JTL\Catalog\Hersteller;
use JTL\Helpers\Manufacturer;
use JTL\Media\Image;
use JTL\OPC\InputType;
use JTL\OPC\Portlet;
use JTL\OPC\PortletInstance;
use stdClass;

final class HerstellerSlider extends Portlet
{
  use PortletTrait;

  private $fontAwesome = 'fab fa-creative-commons-remix';

  public function getPropertyDesc(): array
  {
    return [
      'showNames' => [
        'type'    => InputType::CHECKBOX,
        'label'   => __('showNames'),
        'width'   => 100,
        'default' => false
      ],
      'showWithoutPicture' => [
        'type'    => InputType::CHECKBOX,
        'label'   => __('showWithoutPicture'),
        'width'   => 100,
        'default' => false
      ],
      'showRandom' => [
        'type'    => InputType::CHECKBOX,
        'label'   => __('showRandom'),
        'width'   => 100,
        'default' => false
      ],
    ];
  }

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

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

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

    $manufacturers = Manufacturer::getInstance()->getManufacturers();

    if ($instance->getProperty('showWithoutPicture')) {
      $manufacturers = array_filter($manufacturers, static fn (Hersteller $manufacturer, $k) => ! empty($manufacturer->getImage() ?? ''), \ARRAY_FILTER_USE_BOTH);
    }

    array_walk($manufacturers, static function (Hersteller &$manufacturer): void {
      if (null === $manufacturer->getImage(Image::SIZE_MD) || empty($manufacturer->getImage(Image::SIZE_MD))) {
        $manufacturer->setImages(['md' => '/gfx/keinBild.gif']);
      }
    });

    if ($instance->getProperty('showRandom')) {
      shuffle($manufacturers);
    }

    return $manufacturers;
  }

  public function getPreviewManufacturers(PortletInstance $instance): array
  {
    $previewManufacturer           = new stdClass();
    $previewManufacturer->name     = __('hersteller');
    $previewManufacturer->image    = $instance->getPortlet()->getPlugin()->getPaths()->getAdminURL().'img/CiNLogoSquare.svg';
    $previewManufacturer->imageAlt = __('platzhalter');

    $manufacturers = Manufacturer::getInstance()->getManufacturers();

    // TODO REMOVE > ONLY FOR DEV
    //$manufacturers = array_filter($manufacturers, fn($val, $k) => strlen($val->cName) <= 3, ARRAY_FILTER_USE_BOTH);

    if ($instance->getProperty('showWithoutPicture')) {
      $manufacturers = array_filter($manufacturers, static fn (Hersteller $manufacturer, $k) => ! empty($manufacturer->getImage() ?? ''), \ARRAY_FILTER_USE_BOTH);
    }

    array_walk($manufacturers, static function (&$manufacturer): void {
      $previewManufacturer           = new stdClass();
      $previewManufacturer->name     = $manufacturer->getName();
      $previewManufacturer->image    = $manufacturer->getImage(Image::SIZE_MD) ?: '/gfx/keinBild.gif';
      $previewManufacturer->imageAlt = $manufacturer->getName();
      $manufacturer                  = $previewManufacturer;
    });

    if ($instance->getProperty('showRandom')) {
      shuffle($manufacturers);
    }

    $manufacturers = array_pad($manufacturers, 7, $previewManufacturer);

    return $manufacturers;
  }
}
