<?php

/**
 * @file
 * Code for Parade Demo.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;

/**
 * Implements hook_theme().
 */
function parade_demo_theme($existing, $type, $theme, $path) {
  return [
    'page__node__parade_onepage' => [
      'base hook' => 'page',
    ],
    'field__node__parade_onepage_sections__parade_onepage' => [
      'base hook' => 'field',
    ],
    'field__paragraph__parade_title' => [
      'base hook' => 'field',
    ],
    'field__paragraph__text_boxes' => [
      'base hook' => 'field',
    ],
    'field__node__parade_onepage_menu__parade_onepage' => [
      'base hook' => 'field',
    ],
    'node__parade_onepage' => [
      'base hook' => 'node',
    ],
  ];
}

/**
 * Implements hook_page_attachments().
 */
function parade_demo_page_attachments(array &$page) {
  $attributes = Drupal::request()->attributes;
  if ($attributes->has('node') &&  $attributes->get('_route') === 'entity.node.canonical' && $attributes->get('node')->getType() === 'parade_onepage') {
    $page['#attached']['library'][] = 'parade_demo/parade_onepage';
    $page['#attached']['library'][] = 'parade_demo/scrollspy';
    $page['#attached']['library'][] = 'parade_demo/responsive_tabs';
    $page['#attached']['library'][] = 'parade_demo/smooth-scroll';
  }
}

/**
 * Implements hook_form_alter().
 *
 * @todo Use more specific hook.
 */
function parade_demo_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (in_array($form_id, ['node_parade_onepage_form', 'node_parade_onepage_edit_form'], FALSE)) {
    $form['#attached']['library'][] = 'parade_demo/parade_onepage';
    $page['#attached']['library'][] = 'parade_demo/scrollspy';
    $page['#attached']['library'][] = 'parade_demo/responsive_tabs';
    $page['#attached']['library'][] = 'parade_demo/smooth-scroll';

    $form['parade_onepage_menu']['widget']['add_more']['#value'] = t('Add menu link');
  }
}

/**
 * Implements hook_field_widget_form_alter().
 */
function parade_demo_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  // @fixme check for field Type, not name.
  // @todo also check for Parade content type!
  $field_name = $context['items']->getFieldDefinition()->getName();

  // Change dropbuttons to normal list in Paragraphs inline forms.
  if ($field_name === 'parade_onepage_sections') {
    unset($element['top']['links']['#theme_wrappers']);
    $element['top']['links']['prefix']['#markup'] = '<ul class="parade-actions">';
  }

  // Changes to Menu field.
  if ($field_name === 'parade_onepage_menu') {
    $element['uri']['#description'] = NULL;
    $element['uri']['#title_display'] = 'none';
    $element['title']['#title_display'] = 'none';
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function parade_demo_preprocess_html(&$variables) {
  // If on a node add or edit page, add a node-layout class.
  $path_args = explode('/', Drupal::request()->getPathInfo());
  if ($suggestions = theme_get_suggestions($path_args, 'page', '-')) {
    foreach ($suggestions as $suggestion) {
      if ($suggestion === 'page-node-edit' || strpos($suggestion, 'page-node-add') !== FALSE) {
        $variables['attributes']['class'][] = 'node-form-layout';
      }
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function parade_demo_preprocess_menu_local_action(array &$variables) {
  $variables['link']['#options']['attributes']['class'][] = 'button--primary';
  $variables['link']['#options']['attributes']['class'][] = 'button--small';
}

/**
 * Implements hook_form_alter().
 *
 * Changes vertical tabs to container and adds meta information
 * and attach custom tabledrag library on campaign edit forms
 */
function parade_demo_form_node_form_alter(&$form, FormStateInterface $form_state) {
  /** @var Drupal\node\NodeInterface $node */
  $node = $form_state->getFormObject()->getEntity();

  $form['#theme'] = array('node_edit_form');
  // $form['#attached']['library'][] = 'tieto_admin/node-form';

  $form['advanced']['#type'] = 'container';
  $is_new = !$node->isNew() ? format_date($node->getChangedTime(), 'short') : t('Not saved yet');
  $form['meta'] = array(
    '#attributes' => array('class' => array('entity-meta__header')),
    '#type' => 'container',
    '#group' => 'advanced',
    '#weight' => -100,
    'published' => array(
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $node->isPublished() ? t('Published') : t('Not published'),
      '#access' => !$node->isNew(),
      '#attributes' => array(
        'class' => 'entity-meta__title',
      ),
    ),
    'changed' => array(
      '#type' => 'item',
      '#wrapper_attributes' => array('class' => array('entity-meta__last-saved', 'container-inline')),
      '#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
    ),
    'author' => array(
      '#type' => 'item',
      '#wrapper_attributes' => array('class' => array('author', 'container-inline')),
      '#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $node->getOwner()->getUsername(),
    ),
  );
  $form['revision_information']['#type'] = 'container';
  $form['revision_information']['#group'] = 'meta';

  // Custom tabledrag library
  // if ($form['form_id']['#id'] === 'edit-node-campaign-edit-form') {
  //  $form['#attached']['library'][] = 'tieto_admin/tabledrag';
  // }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function parade_demo_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  // @todo @fixme
  if (in_array('page__node__campaign', $suggestions, FALSE)) {
    $suggestions[] = 'page__node__campaign__revisions';
  }
  return $suggestions;
}

/**
 * Implements hook_preprocess_HOOK().
 */
function parade_demo_preprocess_field__parade_call_to_action(&$variables) {
  foreach ($variables['items'] as $key => $link) {
    if ($variables['element']['#entity_type'] === 'paragraph' && $variables['element']['#bundle'] === 'text_box') {
      $classes = ['button', 'button--green'];
    }
    else {
      $classes = ['button', 'button--primary'];
    }
    $variables['items'][$key]['content']['#options']['attributes']['class'] = $classes;
  }
}
