<?php

/**
 * @file
 * Charts module file that provides hook_theme.
 */

use Drupal\charts\Util\Util;

/**
 * {@inheritdoc}
 */
function charts_theme($existing, $type, $theme, $path) {

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

/**
 * {@inheritdoc}
 */
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'];

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

  $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'];
  }
  $seriesData = array_merge($seriesData, $seriesDataAttachment);

  // Handles the toggling from one library to another.
  switch ($library) {
    case 'google':
      Util::checkMissingLibrary('charts_google', '/vendor/google/loader.js');
      $googleData = charts_google_render_charts($categories, $seriesData);
      $googleOptions = charts_google_create_charts_options($options, $seriesData, $attachmentDisplayOptions);
      $googleChartType = charts_google_create_chart_type($options);
      $variables['chart_type'] = 'google';
      $variables['attributes']['class'][0] = 'charts-google';
      $variables['attributes']['id'][0] = $chartId;
      $variables['content_attributes']['data-chart'][] = $googleData;
      $variables['attributes']['google-options'][1] = json_encode($googleOptions);
      $variables['attributes']['google-chart-type'][2] = json_encode($googleChartType);

      break;

    case 'highcharts':
      Util::checkMissingLibrary('charts_highcharts', '/vendor/highcharts/highcharts.js');
      $highchart = charts_highcharts_render_charts($options, $categories, $seriesData, $attachmentDisplayOptions);
      $variables['chart_type'] = 'highcharts';
      $variables['content_attributes']['data-chart'][] = json_encode($highchart);
      $variables['attributes']['id'][0] = $chartId;
      $variables['attributes']['class'][] = 'charts-highchart';

      break;

    case 'c3':
      Util::checkMissingLibrary('charts_c3', '/vendor/cthree/c3.min.js');
      $c3 = charts_c3_render_charts($options, $categories, $seriesData, $chartId, $attachmentDisplayOptions);
      $variables['chart_type'] = 'c3';
      $variables['content_attributes']['data-chart'][] = json_encode($c3);
      $variables['attributes']['id'][0] = $chartId;
      $variables['attributes']['class'][] = 'charts-c3';
      break;

    default:
      // Not handled.
  }

}