<?php

declare(strict_types=1);

namespace Drupal\farm_sensor_listener\Hook;

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\asset\Entity\AssetInterface;
use Drupal\data_stream\Entity\DataStreamInterface;

/**
 * Theme hook implementations for farm_sensor_listener.
 */
class ThemeHooks {

  use StringTranslationTrait;

  /**
   * Implements hook_ENTITY_TYPE_view_alter().
   */
  #[Hook('asset_view_alter')]
  public function assetViewAlter(array &$build, AssetInterface $asset, EntityViewDisplayInterface $display) {

    // Bail if this is not a sensor asset.
    if ($asset->bundle() != 'sensor') {
      return;
    }

    // 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 ($display->getOriginalMode() === 'full') {

      // Bail if the sensor asset does not have any basic data streams.
      $basic_data_streams = array_filter($asset->get('data_stream')->referencedEntities(), function (DataStreamInterface $data_stream) {
        return $data_stream->bundle() === 'basic';
      });
      if (count($basic_data_streams) === 0) {
        return;
      }

      // Build URL to the sensor Legacy API endpoint.
      $url = new Url('farm_sensor_listener.data_stream', ['public_key' => $asset->get('public_key')->value]);

      // If the sensor is not public, include the private key.
      if (!$asset->get('public')->value) {
        $url->setOption('query', ['private_key' => $asset->get('private_key')->value]);
      }

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

}
