<?php
namespace WP_AUX\Entities\Admin\AuxComponents;

use WP_AUX\Services\HeadService;

class AuxComponentsPageEntity {
  protected AuxComponentsAuditEntity $auxComponentsAudit;

  public function __construct() {
    add_action( 'admin_menu', array( $this, 'menu' ) );
  }

  /**
   * Add the AUX Components menu under tools.
   * Available to users with the 'install_plugins' capability.
   *
   * @since 1.0.0
   */
  public function menu() {
    $hook = add_management_page(
      'AUX Components',
      'AUX Components',
      'edit_posts',
      'aux-components',
      array( $this, 'adminPage' ),
      null
    );
    add_action( "load-$hook", [ $this, 'adminLoad' ] );
  }

  /**
   * @todo: Dion - after the stories site launch - please document what each of these scripts is used for.
   * @todo: Use a local copy for all scripts and styles. Supply chain attacks have become common. NO REMOTE SCRIPTS ALLOWED (unless it's on one of our servers)
   */
  public function adminLoad() {
    $this->auxComponentsAudit = new AuxComponentsAuditEntity();

    wp_enqueue_style(
      "datatables-css",
      "https://cdn.datatables.net/2.0.8/css/dataTables.dataTables.min.css",
    );

    wp_enqueue_script(
      "datatables-js",
      "https://cdn.datatables.net/2.0.8/js/dataTables.min.js",
    );

    wp_enqueue_script(
      "popper-js",
      "https://unpkg.com/@popperjs/core@2",
    );
    wp_enqueue_script(
      "tippy-js",
      "https://unpkg.com/tippy.js@6",
    );

    add_action('admin_head', function() {
      echo '<style>
      .field-details { display: none; }
      .field-property-source-cell1 { display: none; }
      .field-details.active1 { display: none; }
      </style>';
    }, 100);

    HeadService::enqueueLocalScript('aux-components', 'js/aux-components.js');
    HeadService::enqueueLocalStyle('aux-components-css', 'css/aux-components-audit.css');
  }

  public function adminPage() {
    get_template_part('template-parts/admin/aux-components-audit', null, ['auxComponentsAudit' => $this->auxComponentsAudit]);
  }
}
