<?php

namespace Drupal\sector_navigation\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Illuminate\Support\Arr;
use Drupal\Core\Url;
use Drupal\system\Entity\Menu;


/**
 * Provides an example block.
 *
 * @Block(
 *   id = "sector_navigation_block",
 *   admin_label = @Translation("Sector Navigation Block"),
 *   category = @Translation("Sector Custom Blocks")
 * )
 */
class NavigationBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'component' => NULL,
      'primary' => NULL,
      'secondary' => NULL,
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function build() {


    return [
      '#type' => 'component',
      '#component' => 'sector_navigation:' . $this->configuration['component'],
      '#slots' => [
        'primary' => $this->configuration['primary'],
        'secondary' => $this->configuration['secondary']
      ],
      '#variant' => NULL,
      '#properties' => [

      ]
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    // Get available menus.
    $menus = Menu::loadMultiple();
    $menu_options = ['' => $this->t('- None -')];
    foreach ($menus as $menu_id => $menu) {
      $menu_options[$menu_id] = $menu->label();
    }

    // Display type select field.
    $form['component'] = [
      '#type' => 'select',
      '#title' => $this->t('Display Type'),
      '#options' => [
        'navbar' => $this->t('Navbar'),
        'handheld' => $this->t('Handheld'),
      ],
      '#default_value' => $this->configuration['component'] ?? NULL,
      '#required' => TRUE,
      '#description' => $this->t('Choose the display type for this navigation block.'),
    ];

    // Primary menu field.
    $form['primary'] = [
      '#type' => 'select',
      '#title' => $this->t('Primary'),
      '#options' => $menu_options,
      '#default_value' => $this->configuration['primary'] ?? NULL,
      '#required' => TRUE,
      '#description' => $this->t('Select the primary menu to display.'),
    ];

    // Secondary menu field.
    $form['secondary'] = [
      '#type' => 'select',
      '#title' => $this->t('Secondary'),
      '#options' => $menu_options,
      '#default_value' => $this->configuration['secondary'] ?? NULL,
      '#required' => FALSE,
      '#description' => $this->t('Select an optional secondary menu to display.'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['component'] = $form_state->getValue('component');
    $this->configuration['primary'] = $form_state->getValue('primary');
    $this->configuration['secondary'] = $form_state->getValue('secondary');
  }

}
