<?php

/**
 * @file
 * This is the Calendar module for calendar filtering and display in views.
 */

use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\calendar\CalendarHelper;
use Drupal\datetime\Plugin\views\argument\Date;
use Drupal\views\ViewExecutable;

define('CALENDAR_SHOW_ALL', 0);
define('CALENDAR_HIDE_ALL', -1);
define('CALENDAR_EMPTY_STRIPE', '#ffffff');

/**
 * Implements hook_page_attachments().
 */
function calendar_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'calendar/calendar.theme';
}

/**
 * Implements hook_views_pre_render().
 */
function calendar_views_pre_render(ViewExecutable $view) {
  _calendar_views_track_current_date($view);
}

/**
 * Track the current date as the user moves between calendar displays.
 *
 * This function updates the session with the current date based on the user's
 * navigation through the calendar.
 */
function _calendar_views_track_current_date(ViewExecutable $view): void {
  $user = \Drupal::currentUser();

  $tracking = \Drupal::config('calendar.settings')->get('track_date');
  if (!empty($tracking) && ($tracking == 2 || !empty($user->uid))) {
    foreach ($view->argument as $id => &$argument) {

      // If this is not a default date, i.e. we have browsed to a new calendar
      // period on a display we were already on, store the midpoint of the
      // current view as the current date in a session.
      if ($argument instanceof Date && empty($argument->is_default)) {
        $date_range = $argument->date_handler->arg_range($argument->argument);
        $session_date = $date_range[0];
        $days = intval(($date_range[1]->format('U') - $date_range[0]->format('U')) / 3600 / 24 / 2);
        date_modify($session_date, "+$days days");
        $_SESSION[$view->name]['default_date'] = $session_date;
      }
    }
  }
}

/**
 * Implements hook_help().
 */
function calendar_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.calendar':
      return '<p>' . t('View complete documentation at <a href=":link">Date and Calendar Documentation</a>.',
          [':link' => 'http://drupal.org/node/262062']) . '</p>';
  }
}

/**
 * Implements hook_theme().
 */
function calendar_theme() {
  return [
    'calendar_mini' => [
      'variables' => ['view' => NULL, 'options' => [], 'rows' => []],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_month_multiple_entity' => [
      'variables' => [
        'curday' => NULL,
        'count' => NULL,
        'view' => NULL,
        'ids' => NULL,
      ],
    ],
    'calendar_datebox' => [
      'variables' => [
        'date' => NULL,
        'view' => NULL,
        'items' => NULL,
        'selected' => NULL,
      ],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_empty_day' => [
      'variables' => ['curday' => NULL, 'view' => NULL],
    ],
    'calendar_month_col' => [
      'variables' => ['item' => NULL],
    ],
    'calendar_month_row' => [
      'variables' => ['inner' => NULL, 'class' => NULL, 'iehint' => NULL],
    ],
    'calendar_day' => [
      'variables' => ['view' => NULL, 'options' => [], 'rows' => []],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_month' => [
      'variables' => ['view' => NULL, 'options' => [], 'rows' => []],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_stripe_legend' => [
      'variables' => ['view_and_display_id' => NULL, 'title' => NULL],
    ],
    'calendar_item' => [
      'variables' => [
        'view' => NULL,
        'rendered_fields' => NULL,
        'item' => NULL,
      ],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_week' => [
      'variables' => ['view' => NULL, 'day_names' => [], 'rows' => []],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_week_overlap' => [
      'variables' => ['view' => NULL, 'day_names' => [], 'rows' => []],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_day_overlap' => [
      'variables' => ['view' => NULL, 'options' => [], 'rows' => []],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_year' => [
      'variables' => [
        'view' => NULL,
        'rows' => [],
        'options' => [],
        'months' => [],
        'mini' => FALSE,
      ],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_header' => [
      'variables' =>
        [
          'view' => NULL,
          'options' => [],
          'title' => '',
          'empty' => FALSE,
          'granularity' => NULL,
        ],
      'file' => 'calendar.theme.inc',
    ],
    'calendar_time_row_heading' => [
      'variables' => [
        'start_time' => NULL,
        'next_start_time' => NULL,
        'curday_date' => NULL,
      ],
    ],
    'calendar_pager' => [
      'variables' => [
        'parameters' => [],
        'items' => [],
        'exclude' => FALSE,
      ],
    ],

  ];
}

/**
 * Default settings array for calendar time grouping.
 */
function calendar_groupby_times($type = '') {
  $times = [];
  switch ($type) {
    case 'hour':
      for ($i = 0; $i <= 23; $i++) {
        $times[] = DateTimePlus::datePad($i) . ':00:00';
      }
      break;

    case 'half':
      for ($i = 0; $i <= 23; $i++) {
        $times[] = DateTimePlus::datePad($i) . ':00:00';
        $times[] = DateTimePlus::datePad($i) . ':30:00';
      }
      break;

    default:
      break;
  }
  return $times;
}

/**
 * Find the path for the calendar display that has a specific granularity.
 *
 * @param \Drupal\views\ViewExecutable $view
 *   The current view.
 * @param string $granularity
 *   The granularity to find.
 *
 * @return string
 *   The path for the specific granularity, or an empty string if none was
 *   found.
 *
 * @deprecated in drupal:9.5.0 and is removed from drupal:11.0.0. Use
 *   \Drupal\calendar\CalendarHelper::getUrlForGranularity() instead.
 * @see https://www.drupal.org/project/calendar/issues/2610940
 */
function calendar_granularity_path(ViewExecutable &$view, $granularity) {
  // @todo Find out what should happen here.
  $paths = &drupal_static(__FUNCTION__, []);
  $view_name = $view->id();
  if (!array_key_exists($view_name, $paths) || (isset($paths[$view->id()]) && !(array_key_exists($granularity, $paths[$view->id()])))) {
    $paths[$view_name][$granularity] = '';

    foreach ($view->displayHandlers->getConfiguration() as $id => $display) {
      // Check for !empty() in case the view is not fully formed or has displays
      // that are marked to be deleted.
      // phpcs:disable
      // @todo handle deleted displays
      //   if (!empty($display->deleted) || empty($display->display_options['style_plugin']) || (isset($display->display_options['enabled']) && $display->display_options['enabled'] == FALSE)) {
      //     continue;
      //   }
      // phpcs:enable
      if ($display['display_plugin'] != 'feed' && !empty($display['display_options']['path']) && !empty($display['display_options']['arguments'])) {

        // Set to the default value, reset below if another value is found.
        $type = 'month';
        $argument = CalendarHelper::getDateArgumentHandler($view, $id);

        if ($argument->getGranularity() == $granularity) {
          $part_path = $display['display_options']['path'];
          $parts = explode('/', $part_path);
          if (in_array('%', $parts)) {
            $url = Url::fromRoute('<current>');
            $current_path = $url->toString();
            $current_parts = explode('/', $current_path);
            foreach ($parts as $key => $part) {
              if ($part == '%' && !empty($current_parts[$key])) {
                $parts[$key] = $current_parts[$key];
              }
            }
            $part_path = implode('/', $parts);
          }
          $paths[$view->id()][$granularity] = $part_path;
        }
      }
    }
  }
  return $paths[$view->id()][$granularity];
}
