<?php

declare(strict_types=1);

namespace Drupal\farm_ui_views\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides task links for farmOS Taxonomy Term Views.
 */
class FarmTaxonomyTermViewsTaskLink extends DeriverBase implements ContainerDeriverInterface {

  use StringTranslationTrait;

  public function __construct(
    protected EntityTypeManagerInterface $entityTypeManager,
    protected EntityTypeBundleInfoInterface $entityTypeBundleInfo,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    // @todo Remove when DeriverBase provides a create() method with autowiring.
    // @see https://www.drupal.org/project/drupal/issues/3565338
    return new static(
      $container->get('entity_type.manager'),
      $container->get('entity_type.bundle.info')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $links = [];

    // Add asset and log task links to taxonomy term pages.
    foreach (['asset', 'log'] as $entity_type_id) {

      // Get the entity type definition.
      $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);

      // Add tab for each entity type.
      $links[$entity_type_id] = [
        'title' => $entity_type->getCollectionLabel(),
        'route_name' => "view.farm_$entity_type_id.page_term",
        'base_route' => 'entity.taxonomy_term.canonical',
        'weight' => 50,
      ] + $base_plugin_definition;

      // Build the parent id from the base ID.
      $base_id = $base_plugin_definition['id'];
      $parent_id = "$base_id:$entity_type_id";

      // Add default "All" secondary tab for each entity type.
      $links["$entity_type_id.all"] = [
        'title' => $this->t('All'),
        'parent_id' => $parent_id,
        'route_name' => "view.farm_$entity_type_id.page_term",
        'route_parameters' => [
          'entity_bundle' => 'all',
        ],
      ] + $base_plugin_definition;

      // Add secondary tab for each entity bundle.
      $entity_bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
      foreach ($entity_bundles as $entity_bundle => $info) {
        $links["$entity_type_id.$entity_bundle"] = [
          'title' => $info['label'],
          'parent_id' => $parent_id,
          'route_name' => "view.farm_$entity_type_id.page_term",
          'route_parameters' => [
            'entity_bundle' => $entity_bundle,
          ],
        ] + $base_plugin_definition;
      }
    }

    return $links;
  }

}
