<?php

/**
 * @file
 * Contains halo_views_2.module.
 */

use Drupal\cfp_modification_request\Entity\CfpModificationRequest;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\User;

module_load_include('inc', 'halo_front_page', 'halo_front_page.views');

/**
 * Implements hook_toolbar().
 */
function halo_front_page_toolbar() {
  return [
    'admin_front_page_link' => [
      '#type' => 'toolbar_item',
      'tab' => [
        '#type' => 'link',
        '#title' => t('Admin front page'),
        '#url' => Url::fromRoute('halo_front_page.admin_front_page'),
      ],
      '#weight' => -5,
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function halo_front_page_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Cfp state.
  if (in_array($form['#id'], [
    'views-exposed-form-cfp-page-1',
    'views-exposed-form-cfp-page-2',
    'views-exposed-form-cfp-page-3',
    'views-exposed-form-ced-main-data-page-1',
    'views-exposed-form-cfp-modification-requests-page-1',
  ])) {
    $workflow_manager = \Drupal::service('plugin.manager.workflow');
    $workflow = $workflow_manager->createInstance('cfp');
    $states = $workflow->getStates();
    $states = array_map(function ($state) {
      return $state->getLabel();
    }, $states);
    $form['state'] = [
      '#type' => 'select',
      '#options' => [
        '' => t('- Any -'),
      ] + $states,
      '#default_value' => reset($states),
    ];

    $entity_field_manager = \Drupal::service('entity_field.manager');
    $fields = $entity_field_manager->getFieldStorageDefinitions('cfp');
    $sub_state_options = options_allowed_values($fields['sub_state']);
    unset($sub_state_options['void_sub_state']);
    $form['sub_state'] = [
      '#title' => t('Sub state'),
      '#type' => 'select',
      '#options' => [
        '' => t('- Any -'),
      ] + $sub_state_options,
    ];
  }

  if ($form['#id'] === 'views-exposed-form-payment-requests-page-1') {
    $workflow_manager = \Drupal::service('plugin.manager.workflow');
    $workflow = $workflow_manager->createInstance('payment_request');
    $states = $workflow->getStates();
    $states = array_map(function ($state) {
      return $state->getLabel();
    }, $states);

    $form['state'] = [
      '#type' => 'select',
      '#options' => [
          '' => t('- Any -'),
        ] + $states,
    ];

    $form['state'] = [
      '#type' => 'select',
      '#options' => [
          '' => t('- Any -'),
        ] + $states,
    ];
    $entity_field_manager = \Drupal::service('entity_field.manager');
    $fields = $entity_field_manager->getFieldStorageDefinitions('cfp');
    $form['location_check_state'] = [
      '#type' => 'select',
      '#options' => [
          '' => t('- Any -'),
        ] + $fields['location_check_state']->getSetting('allowed_values'),
    ];
    $form['location_check_type'] = [
      '#type' => 'select',
      '#options' => [
          '' => t('- Any -'),
        ] + $fields['location_check_type']->getSetting('allowed_values'),
    ];
  }

  if ($form['#id'] === 'views-exposed-form-locations-halo-page-1') {
    if (!isset($entity_field_manager)) {
      $entity_field_manager = \Drupal::service('entity_field.manager');
    }
    $fields = $entity_field_manager->getFieldStorageDefinitions('cfp');
    $form['location_check_state'] = [
      '#type' => 'select',
      '#options' => [
          '' => t('- Any -'),
        ] + $fields['location_check_state']->getSetting('allowed_values'),
    ];
    $form['location_check_type'] = [
      '#type' => 'select',
      '#options' => [
          '' => t('- Any -'),
        ] + $fields['location_check_type']->getSetting('allowed_values'),
    ];
  }

  if ($form['#id'] === 'views-exposed-form-advance-payment-page-1') {
    $workflow_manager = \Drupal::service('plugin.manager.workflow');
    $workflow = $workflow_manager->createInstance('advance_payment');
    $states = $workflow->getStates();
    $states = array_map(function ($state) {
      return $state->getLabel();
    }, $states);
    $form['state'] = [
      '#type' => 'select',
      '#options' => [
        '' => t('- Any -'),
      ] + $states,
    ];
  }
  if ($form['#id'] === 'views-exposed-form-cfp-modification-requests-page-1') {
    $form['request_type'] = [
      '#type' => 'select',
      '#options' => [
        '' => t('- Any -'),
      ] + _request_type_callback()
    ];
    $form['state_1'] = [
      '#type' => 'select',
      '#options' => [
        '' => t('- Any -'),
      ] + CfpModificationRequest::getStates(),
    ];
  }
  if ($form['#id'] === 'views-exposed-form-technical-report-page-1') {
    $workflow_manager = \Drupal::service('plugin.manager.workflow');
    $workflow = $workflow_manager->createInstance('technical_report');
    $states = $workflow->getStates();
    $states = array_map(function ($state) {
      return $state->getLabel();
    }, $states);
    $form['state']  = $form['transition_history_to'] = [
      '#type' => 'select',
      '#options' => [
          '' => t('- Any -'),
        ] + $states
    ];
  }

  // First submission date.
  if (in_array($form['#id'], ['views-exposed-form-payment-requests-page-1', 'views-exposed-form-technical-report-page-1', 'views-exposed-form-advance-payment-page-1'])) {
    $form['first_submission_date_wrapper']['first_submission_date']['min']['#type'] = 'date';
    $form['first_submission_date_wrapper']['first_submission_date']['min']['#title'] = '-tól';
    $form['first_submission_date_wrapper']['first_submission_date']['max']['#type'] = 'date';
    $form['first_submission_date_wrapper']['first_submission_date']['max']['#title'] = '-ig';
//    if (isset($form['correction_deadline_wrapper'])) {
//      $form['correction_deadline_wrapper']['correction_deadline']['min']['#type'] = 'date';
//      $form['correction_deadline_wrapper']['correction_deadline']['min']['#title'] = '-tól';
//      $form['correction_deadline_wrapper']['correction_deadline']['max']['#type'] = 'date';
//      $form['correction_deadline_wrapper']['correction_deadline']['max']['#title'] = '-ig';
//    }
    if (isset($form['deadline_expired_wrapper'])) {
      $form['deadline_expired_wrapper']['deadline_expired']['min']['#type'] = 'date';
      $form['deadline_expired_wrapper']['deadline_expired']['min']['#title'] = '-tól';
      $form['deadline_expired_wrapper']['deadline_expired']['max']['#type'] = 'date';
      $form['deadline_expired_wrapper']['deadline_expired']['max']['#title'] = '-ig';
    }
  }

  if ($form['#id'] === 'views-exposed-form-applicants-page-1') {
    $form['cfp_type_group_owner_value'] = [
      '#type' => 'select',
      '#options' => [
          '' => t('- Any -'),
        ] + _cfp_group_types(),
    ];
  }


  if ($form['#id'] === 'views-exposed-form-locations-page-1') {
    $form['closed']['#options'][1] = t('Closed');
    $form['closed']['#options'][0] = t('Open');
    $form['date']['#type'] = 'date';
    $form['date_1']['#type'] = 'date';
  }
}

/**
 * Implements hook_link_alter().
 */
function halo_front_page_link_alter(&$variables) {
  $url = $variables['url'];
  if ($url->isExternal() || !$url->isRouted()) {
    return;
  }

  $route = $url->getRouteName();
  if ($route === 'user.page') {
    $variables['text'] = \Drupal::currentUser()->getDisplayName();
  }
}

/**
 * Implements hook_user_format_name_alter().
 */
function halo_front_page_user_format_name_alter(&$name, AccountInterface $account) {
  $user = User::load($account->id());
  $name = $user->field_lastname->value . ' ' . $user->field_firstname->value;
}

/**
 * Implements hook_views_data_alter().
 */
function halo_front_page_views_data_alter(array &$data) {
  $data['review']['assignable_filter'] = [
    'title' => t('Assignable filter'),
    'filter' => [
      'title' => t('Assignable filter'),
      'help' => t('Filters by assignable entities.'),
      'field' => 'id',
      'id' => 'assignable_filter',
    ],
  ];
}