<?php

/**
 * @file
 * The farmOS UI Theme module.
 */

use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_theme().
 */
function farm_ui_theme_theme($existing, $type, $theme, $path) {
  return [
    'html__asset__map_popup' => [
      'base hook' => 'html',
    ],
    'menu_local_tasks__farm' => [
      'base hook' => 'menu_local_tasks',
    ],
    'menu_local_task__secondary' => [
      'base hook' => 'menu_local_task',
    ],
    'page__asset__map_popup' => [
      'base hook' => 'page',
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function farm_ui_theme_theme_suggestions_menu_local_task(array $variables) {
  // Add suggestions for primary and secondary task levels.
  $suggestions = [];
  if (isset($variables['element']['#level'])) {
    $suggestions[] = 'menu_local_task__' . $variables['element']['#level'];
  }
  return $suggestions;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function farm_ui_theme_theme_suggestions_menu_local_tasks(array $variables) {
  return ['menu_local_tasks__farm'];
}

/**
 * Implements hook_element_info_alter().
 */
function farm_ui_theme_element_info_alter(array &$info) {
  if (isset($info['farm_map'])) {
    $info['farm_map']['#attached']['library'][] = 'farm_ui_theme/map';
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function farm_ui_theme_form_quick_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#attached']['library'][] = 'farm_ui_theme/quick';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_block(&$variables) {
  if ($variables['plugin_id'] == 'help_block') {
    $variables['#attached']['library'][] = 'farm_ui_theme/help';
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_toolbar(&$variables) {
  $variables['#attached']['library'][] = 'farm_ui_theme/toolbar';
}

/**
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 */
function farm_ui_theme_block_view_farm_powered_by_block_alter(array &$build, BlockPluginInterface $block) {
  $build['#attached']['library'][] = 'farm_ui_theme/footer';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_field__flag(array &$variables) {
  $variables['#attached']['library'][] = 'farm_ui_theme/flag';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_asset__full(&$variables) {
  farm_ui_theme_build_stacked_twocol_layout($variables, 'asset');
  $variables['#attached']['library'][] = 'farm_ui_theme/layout';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_log__full(&$variables) {
  farm_ui_theme_build_stacked_twocol_layout($variables, 'log');
  $variables['#attached']['library'][] = 'farm_ui_theme/layout';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_page__dashboard(&$variables) {
  $variables['#attached']['library'][] = 'farm_ui_theme/dashboard';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_plan__full(&$variables) {
  farm_ui_theme_build_stacked_twocol_layout($variables, 'plan');
  $variables['#attached']['library'][] = 'farm_ui_theme/layout';
}

/**
 * Splits content into a stacked two-column layout.
 *
 * @param array $variables
 *   A $variables array that contains a 'content' item, which will be replaced
 *   by a stacked two-column layout.
 * @param string $entity_type
 *   The entity type.
 */
function farm_ui_theme_build_stacked_twocol_layout(array &$variables, string $entity_type) {

  // Ask modules for a list of region items.
  $region_items = \Drupal::moduleHandler()->invokeAll('farm_ui_theme_region_items', [$entity_type]);

  // Split the content items into regions.
  $regions = [];
  foreach ($variables['content'] as $index => $item) {
    $region = 'first';
    foreach ($region_items as $region_name => $items) {
      if (in_array($index, $items)) {
        $region = $region_name;
        break;
      }
    }
    $regions[$region][$index] = $item;
  }

  // Build the layout.
  /** @var \Drupal\Core\Layout\LayoutInterface $layoutInstance */
  $layout = \Drupal::service('plugin.manager.core.layout')->createInstance('layout_twocol', []);
  $variables['content'] = $layout->build($regions);
}

/**
 * Implements hook_farm_ui_theme_region_items().
 */
function farm_ui_theme_farm_ui_theme_region_items(string $entity_type) {

  // Define common asset, log, and plan region items on behalf of core modules.
  switch ($entity_type) {

    case 'asset':
      return [
        'top' => [
          'geometry',
        ],
        'first' => [],
        'second' => [
          'image',
          'inventory',
          'is_location',
          'is_fixed',
          'location',
          'owner',
          'status',
          'type',
        ],
        'bottom' => [
          'api',
          'file',
        ],
      ];

    case 'log':
      return [
        'top' => [
          'geometry',
        ],
        'first' => [],
        'second' => [
          'image',
          'owner',
          'status',
          'type',
        ],
        'bottom' => [
          'file',
        ],
      ];

    case 'plan':
      return [
        'top' => [],
        'first' => [],
        'second' => [
          'image',
          'status',
          'type',
        ],
        'bottom' => [
          'file',
        ],
      ];

    default:
      return [];
  }
}

/**
 * Implements hook_farm_update_exclude_config().
 */
function farm_ui_theme_farm_update_exclude_config() {

  // Exclude config that we have overridden in hook_install() or the
  // farm_ui_theme.overrider service.
  return [
    'block.block.gin_local_actions',
    'block.block.gin_content',
  ];
}
