<?php

/**
 * @file
 * The data stream module.
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Url;
use Drupal\data_stream\Entity\DataStreamInterface;

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function data_stream_data_stream_view_alter(array &$build, DataStreamInterface $data_stream, EntityViewDisplayInterface $display) {

  // Bail if not the basic type.
  if ($data_stream->bundle() != 'basic') {
    return;
  }

  // If the user has permission to edit this sensor asset, display developer
  // information.
  // Only render developer information in the full view mode.
  // Use getOriginalMode() because getMode() is not reliable. The default
  // display mode will return "full" unless a "default" display is actually
  // saved in config. In either case, the original mode is always "full".
  if ($data_stream->access('update') === TRUE && $display->getOriginalMode() === 'full') {

    // Add a Developer information details element with brief description.
    // @todo Include link to updated user guide.
    $build['api'] = [
      '#type' => 'details',
      '#title' => t('Developer information'),
      '#description' => t('This data stream type will listen for data posted to it from other web-connected devices. Use the information below to configure your device to begin posting data to this data stream.'),
      '#open' => FALSE,
    ];

    // Build URL to the data stream API endpoint.
    $url = new Url('data_stream.data', ['uuid' => $data_stream->uuid()]);

    // If the data stream is not public, include the private key.
    if (!$data_stream->isPublic()) {
      $url->setOption('query', ['private_key' => $data_stream->getPrivateKey()]);
    }

    // Render the API url.
    $url_string = $url->setAbsolute()->toString();
    $url_string_label = t('URL');
    $build['api']['url'] = [
      '#type' => 'link',
      '#title' => $url_string,
      '#url' => $url,
      '#prefix' => '<p><strong>' . $url_string_label . ':</strong> ',
      '#suffix' => '</p>',
    ];

    // Render JSON examples.
    $request_time = \Drupal::time()->getRequestTime();
    $stream_name = Html::escape($data_stream->label());
    $json_example = '{ "timestamp": ' . $request_time . ', "' . $stream_name . '": 76.5 }';
    $json_example_label = t('JSON Example');
    $build['api']['json_example'] = [
      '#markup' => '<p><strong>' . $json_example_label . ':</strong> ' . $json_example . '</p>',
    ];

    // Render example CURL command.
    $curl_example = 'curl -H "Content-Type: application/json" -X POST -d \'' . $json_example . '\' ' . $url_string;
    $curl_example_label = t('Example CURL command');
    $build['api']['curl_example'] = [
      '#markup' => '<p><strong>' . $curl_example_label . ':</strong> ' . $curl_example . '</p>',
    ];
  }

  // Add the basic data block view.
  $build['views']['data'] = views_embed_view('data_stream_basic_data', 'block', $data_stream->id());
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function data_stream_data_stream_delete(DataStreamInterface $data_stream) {

  // If this is a "basic" data stream, delete data associated with it.
  if ($data_stream->bundle() == 'basic' && !empty($data_stream->id())) {
    \Drupal::database()->delete('data_stream_basic')
      ->condition('id', $data_stream->id())
      ->execute();
  }
}
