<?php

/**
 * @file
 * Charts - Module.
 */

use Drupal\charts\Util\Util;
use Drupal\charts\Charts\ModuleSelector;

/**
 * Implements hook_theme().
 */
function charts_theme($existing, $type, $theme, $path) {

  return [
    'views_view_charts' => [
      'variables' => [
        'view' => NULL,
        'row'  => NULL,
      ],
    ],
  ];
}

/**
 * Implements template_preprocess_views_view_charts().
 */
function template_preprocess_views_view_charts(&$variables) {

  $options = $variables['view']->style_plugin->options;
  $attachmentDisplayOptions = [];

  $service = \Drupal::service('charts.charts_attachment');
  $attachmentView = $service->getAttachmentViews();

  $view = $variables['view'];
  $viewId = $view->id();
  $displayId = $view->display_handler->display['id'];
  $chartId = $viewId . '__' . $displayId;
  $categoriesAttachment = [];
  $seriesDataAttachment = [];
  $attachmentChartTypeOption = [];

  for ($i = 0; $i < count($attachmentView); $i++) {

    $attachmentId = $attachmentView[$i]->display_handler->display['id'];

    $attachmentDisplay = $view->storage->getDisplay($attachmentId);
    $attachmentChartType = $attachmentDisplay['display_options']['style']['options']['type'];
    array_push($attachmentChartTypeOption, $attachmentChartType);
    $attachedValueField = $attachmentDisplay['display_options']['style']['options']['data_fields'];

    $combinedAttachmentPage = Util::removeUnselectedFields($attachedValueField);
    $attachmentColor = $attachmentView[$i]->style_plugin->options['field_colors'];
    $labelField = $attachmentView[$i]->style_plugin->options['label_field'];
    $dataAttachment = Util::viewsData($attachmentView[$i], $combinedAttachmentPage, $labelField, $attachmentColor, $attachmentChartTypeOption[$i]);
    $dataAttachmentFormatted = Util::createChartableData($dataAttachment);
    for ($j = 0; $j < count($dataAttachmentFormatted[0]); $j++) {
      array_push($categoriesAttachment, $dataAttachmentFormatted[0][$j]);
    }

    for ($j = 0; $j < count($dataAttachmentFormatted[1]); $j++) {
      array_push($seriesDataAttachment, $dataAttachmentFormatted[1][$j]);
    }
  }
  $library = $view->style_plugin->options['library'];
  $variables['data'] = [];
  $labelField = $view->style_plugin->options['label_field'];
  $valueField = $view->style_plugin->options['data_fields'];
  $valueField = Util::removeUnselectedFields($valueField);
  $color = $view->style_plugin->options['field_colors'];

  $attachmentCount = count($attachmentView);
  $data = Util::viewsData($view, $valueField, $labelField, $color, $attachmentChartTypeOption = NULL);
  for ($i = 0; $i < $attachmentCount; $i++) {
    $data = Util::viewsData($view, $valueField, $labelField, $color, $attachmentChartTypeOption[$attachmentCount]);
  }

  $data = Util::createChartableData($data);
  $categories = $data[0];
  $seriesData = $data[1];
  $categories = array_merge($categories, $categoriesAttachment);
  $categories = array_unique($categories);

  for ($i = 0; $i < count($attachmentView); $i++) {
    $attachmentId = $attachmentView[$i]->display_handler->display['id'];
    $attachmentDisplay = $view->storage->getDisplay($attachmentId);
    $attachmentDisplayOptions[$i] = $attachmentDisplay['display_options'];
    // Gets rid of inherit_yaxis issue, but doesn't fix underlying issue.
    $attachmentDisplayOptions[$i]['inherit_yaxis'] = $view->displayHandlers->get($attachmentId)->options['inherit_yaxis'];
  }
  $seriesData = array_merge($seriesData, $seriesDataAttachment);

  $plugin_manager = \Drupal::service('plugin.manager.charts');
  $plugin_definitions = $plugin_manager->getDefinitions();

  if (!isset($plugin_definitions)) {
    // To be removed in drupal 9.
    $moduleSelector = new ModuleSelector($library, $categories, $seriesData, $options, $attachmentDisplayOptions, $chartId);
    if ($moduleSelector->moduleExists()) {
      $moduleSelector->buildVariables($variables);
    }
  }
  else {
    $plugin = $plugin_manager->createInstance($library);
    $plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId);
  }
}