<?php

/**
 * @file
 * Contains installation and update routines for Lightning Workflow.
 */

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\node\Entity\NodeType;
use Drupal\views\Entity\View;

/**
 * Implements hook_install().
 */
function lightning_workflow_install() {
  $module = 'workbench_moderation';

  // Enable moderation for the Page content type, if it exists.
  $page_type = NodeType::load('page');
  if ($page_type) {
    $page_type
      ->setThirdPartySetting($module, 'enabled', TRUE)
      ->setThirdPartySetting($module, 'allowed_moderation_states', [
        'archived',
        'draft',
        'needs_review',
        'published',
      ])
      ->setThirdPartySetting($module, 'default_moderation_state', 'draft')
      ->save();

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
    $form_display = EntityFormDisplay::load('node.page.default');
    if ($form_display) {
      $weights = array_map(
        function (array $component) {
          return $component['weight'];
        },
        $form_display->getComponents()
      );

      sort($weights);
      $form_display->setComponent('scheduled_update', [
        'type' => 'inline_entity_form_complex',
        'weight' => end($weights) + 1,
        'settings' => [
          'override_labels' => FALSE,
          'label_singular' => '',
          'label_plural' => '',
          'allow_new' => TRUE,
          'allow_existing' => FALSE,
          'match_operator' => 'CONTAINS',
        ],
        'third_party_settings' => [],
      ])->save();
    }
  }

  // Set up content type-specific permissions.
  lightning_workflow_update_8001();

  /** @var \Drupal\views\ViewEntityInterface $view */
  $view = View::load('content');
  if (empty($view)) {
    return;
  }
  $display = &$view->getDisplay('default');

  unset($display['display_options']['fields']['status']);
  $display['display_options']['fields']['moderation_state'] = unserialize('a:37:{s:2:"id";s:16:"moderation_state";s:5:"table";s:15:"node_field_data";s:5:"field";s:16:"moderation_state";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:5:"label";s:6:"Status";s:7:"exclude";b:0;s:5:"alter";a:26:{s:10:"alter_text";b:0;s:4:"text";s:0:"";s:9:"make_link";b:0;s:4:"path";s:0:"";s:8:"absolute";b:0;s:8:"external";b:0;s:14:"replace_spaces";b:0;s:9:"path_case";s:4:"none";s:15:"trim_whitespace";b:0;s:3:"alt";s:0:"";s:3:"rel";s:0:"";s:10:"link_class";s:0:"";s:6:"prefix";s:0:"";s:6:"suffix";s:0:"";s:6:"target";s:0:"";s:5:"nl2br";b:0;s:10:"max_length";i:0;s:13:"word_boundary";b:1;s:8:"ellipsis";b:1;s:9:"more_link";b:0;s:14:"more_link_text";s:0:"";s:14:"more_link_path";s:0:"";s:10:"strip_tags";b:0;s:4:"trim";b:0;s:13:"preserve_tags";s:0:"";s:4:"html";b:0;}s:12:"element_type";s:0:"";s:13:"element_class";s:0:"";s:18:"element_label_type";s:0:"";s:19:"element_label_class";s:0:"";s:19:"element_label_colon";b:1;s:20:"element_wrapper_type";s:0:"";s:21:"element_wrapper_class";s:0:"";s:23:"element_default_classes";b:1;s:5:"empty";s:0:"";s:10:"hide_empty";b:0;s:10:"empty_zero";b:0;s:16:"hide_alter_empty";b:1;s:17:"click_sort_column";s:9:"target_id";s:4:"type";s:22:"entity_reference_label";s:8:"settings";a:1:{s:4:"link";b:0;}s:12:"group_column";s:9:"target_id";s:13:"group_columns";a:0:{}s:10:"group_rows";b:1;s:11:"delta_limit";i:0;s:12:"delta_offset";i:0;s:14:"delta_reversed";b:0;s:16:"delta_first_last";b:0;s:10:"multi_type";s:9:"separator";s:9:"separator";s:2:", ";s:17:"field_api_classes";b:0;s:11:"entity_type";s:4:"node";s:12:"entity_field";s:16:"moderation_state";s:9:"plugin_id";s:5:"field";}');

  unset($display['display_options']['filters']['status']);
  unset($display['display_options']['filters']['status_extra']);
  $display['display_options']['filters']['moderation_state'] = unserialize('a:16:{s:2:"id";s:16:"moderation_state";s:5:"table";s:15:"node_field_data";s:5:"field";s:16:"moderation_state";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:8:"operator";s:1:"=";s:5:"value";s:0:"";s:5:"group";i:1;s:7:"exposed";b:1;s:6:"expose";a:10:{s:11:"operator_id";s:19:"moderation_state_op";s:5:"label";s:6:"Status";s:11:"description";s:0:"";s:12:"use_operator";b:0;s:8:"operator";s:19:"moderation_state_op";s:10:"identifier";s:16:"moderation_state";s:8:"required";b:0;s:8:"remember";b:0;s:8:"multiple";b:0;s:14:"remember_roles";a:3:{s:13:"authenticated";s:13:"authenticated";s:9:"anonymous";s:1:"0";s:13:"administrator";s:1:"0";}}s:10:"is_grouped";b:0;s:10:"group_info";a:10:{s:5:"label";s:0:"";s:11:"description";s:0:"";s:10:"identifier";s:0:"";s:8:"optional";b:1;s:6:"widget";s:6:"select";s:8:"multiple";b:0;s:8:"remember";b:0;s:13:"default_group";s:3:"All";s:22:"default_group_multiple";a:0:{}s:11:"group_items";a:0:{}}s:11:"entity_type";s:4:"node";s:12:"entity_field";s:16:"moderation_state";s:9:"plugin_id";s:6:"string";}');

  $view->save();
}

/**
 * Applies workflow permissions to content management roles.
 */
function lightning_workflow_update_8001() {
  foreach (NodeType::loadMultiple() as $node_type) {
    \Drupal::moduleHandler()
      ->invoke('lightning_workflow', 'node_type_insert', [$node_type]);
  }
}

/**
 * Implements hook_update_dependencies().
 */
function lightning_workflow_update_dependencies() {
  return [
    'lightning_workflow' => [
      8001 => [
        'lightning' => 8002,
      ],
    ],
  ];
}
