<?php

namespace Yuvayana\Acadlix\Common\Admin;

defined('ABSPATH') || exit();

class Setup
{
  public function __construct()
  {
    add_action('admin_menu', array($this, 'admin_menus'));
    add_action('admin_init', array($this, 'setup_wizard'));
    add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
  }

  public function admin_menus()
  {
    add_dashboard_page('', '', 'manage_options', 'acadlix-setup', '');
  }

  public function setup_wizard()
  {
    if (isset($_GET['page']) && $_GET['page'] === 'acadlix-setup') {
      $this->acadlix_setup_wizard_header();
      $this->acadlix_setup_wizard();
      $this->acadlix_setup_wizard_footer();
      exit;
    }
  }

  public function acadlix_setup_wizard()
  {
    echo '<div id="acadlix-setup-wizard"><h2>' . esc_html__('Loading...', 'acadlix') . '</h2></div>';
  }

  public function acadlix_setup_wizard_header()
  {
    set_current_screen();
    ?>
    <!DOCTYPE html>
    <html <?php language_attributes(); ?>>

    <head>
      <meta name="viewport" content="width=device-width" />
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title><?php esc_html_e('Acadlix &rsaquo; Setup Wizard', 'acadlix'); ?></title>
      <?php
      try {
        do_action('admin_enqueue_scripts');
      } catch (\Throwable $th) { //phpcs:ignore
      }
      ?>
      <?php do_action('admin_print_styles'); ?>
      <?php do_action('admin_print_scripts'); ?>
      <?php do_action('admin_head'); ?>
    </head>

    <body class="acadlix-setup wp-core-ui">
      <?php
  }

  public function acadlix_setup_wizard_footer()
  {
    ?>
      <?php do_action('admin_footer'); ?>
      <?php do_action('admin_print_footer_scripts'); ?>
    </body>

    </html>
    <?php
  }

  public function localize_options()
  {
    $current_user = wp_get_current_user();
    $capabilities = $current_user->exists() ? $current_user->allcaps : [];
    return [
      'acadlix_home' => admin_url('admin.php?page=acadlix'),
      'api_url' => esc_url_raw(rest_url('acadlix/v1')),
      'max_execution_time' => acadlix()->helper()->acadlix_max_execution_time(),
      'nonce' => wp_create_nonce('wp_rest'),
      'settings' => acadlix()->helper()->acadlix_get_all_options(),
      'theme_settings' => acadlix()->helper()->acadlix_get_option('acadlix_theme_settings'),
      'default_img_url' => esc_url(ACADLIX_ASSETS_IMAGE_URL . "demo-course.jpg"),
      'user_id' => get_current_user_id(),
      'capabilities' => $capabilities,
      'acadlix_docs_url' => ACADLIX_DOCUMENTATION_URL,
      'isActive' => acadlix()->license()->isActive ?? false,
      'image_url' => ACADLIX_ASSETS_IMAGE_URL,
      'user' => get_current_user_id() > 0
        ? acadlix()
          ->model()
          ->wpUsers()
          ->select([
            'ID',
            'user_email',
            'display_name',
            'user_url'
          ])
          ->where('ID', get_current_user_id())
          ->first()
        : [],
    ];
  }

  public function enqueue_scripts()
  {
    $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
    if ('acadlix-setup' === $page) {
      wp_enqueue_editor();
      acadlix()->assets()->manager()->load_assets('admin_setup', $this->localize_options());
    }
  }
}