<?php

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

require_once(ABSPATH . 'wp-admin/includes/file.php');

class BookYourTravel_Theme_Admin extends BookYourTravel_BaseSingleton
{
    private $includes_path;

    protected function __construct()
    {
        $this->includes_path = wp_normalize_path( dirname( __FILE__ ) );

        // our parent class might contain shared code in its constructor
        parent::__construct();
    }

    public function init()
    {
        add_action( 'admin_init', array ( $this, 'admin_init' ) );
        add_action( 'admin_menu', array( $this, 'admin_menu' ));
        add_action( 'admin_menu', array( $this, 'edit_admin_menus' ), 1000 );
        add_action( 'after_switch_theme', array( $this, 'activation_redirect' ) );
        add_filter( 'tgmpa_notice_action_links', array( $this, 'edit_tgmpa_notice_action_links' ) );
        add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_admin_scripts_styles'));
    }

    public function admin_enqueue_admin_scripts_styles() {
      wp_register_script('bookyourtravel-admin-panel', BookYourTravel_Theme_Utils::get_file_uri('/js/admin/admin-panel.js'), array('jquery'), BOOKYOURTRAVEL_VERSION, true);
      wp_enqueue_script('bookyourtravel-admin-panel');

      $ajaxurl = admin_url('admin-ajax.php');
      wp_localize_script('bookyourtravel-admin-panel', 'BookYourTravelAjax', array(
        'ajaxurl' => $ajaxurl,
        'nonce' => wp_create_nonce('bookyourtravel-ajax-nonce'),
      ));
    }

    public function admin_init() {

			if ( isset( $_GET['bookyourtravel-deactivate'] ) && 'deactivate-plugin' === $_GET['bookyourtravel-deactivate'] ) { // phpcs:ignore WordPress.Security
				check_admin_referer( 'bookyourtravel-deactivate', 'bookyourtravel-deactivate-nonce' );

				$plugins = TGM_Plugin_Activation::$instance->plugins;

				foreach ( $plugins as $plugin ) {
					if ( isset( $_GET['plugin'] ) && $plugin['slug'] === $_GET['plugin'] ) {
						deactivate_plugins( $plugin['file_path'] );
					}
				}
      }

			if ( isset( $_GET['bookyourtravel-activate'] ) && 'activate-plugin' === $_GET['bookyourtravel-activate'] ) {
				check_admin_referer( 'bookyourtravel-activate', 'bookyourtravel-activate-nonce' );

				$plugins = TGM_Plugin_Activation::$instance->plugins;

				foreach ( $plugins as $plugin ) {
					if ( isset( $_GET['plugin'] ) && $plugin['slug'] === $_GET['plugin'] ) {
						activate_plugin( $plugin['file_path'] );

						wp_safe_redirect( admin_url( 'admin.php?page=bookyourtravel-plugins' ) );
						exit;
					}
				}
      }

      if (isset($_GET['demo']) && isset($_GET['bookyourtravel-command']) && 'import-demo' === $_GET['bookyourtravel-command']) {
        check_admin_referer( 'bookyourtravel-demo-import', 'bookyourtravel-demo-import-nonce' );

        global $bookyourtravel_theme_filters;

        $demos = $bookyourtravel_theme_filters->list_demo_import_files();

        foreach ($demos as $demo) {
          if ($demo['import_id'] == $_GET['demo']) {

						wp_safe_redirect( admin_url( 'admin.php?page=bookyourtravel-demos' ) );
						exit;
          }
        }
      }
    }

    public function edit_tgmpa_notice_action_links( $action_links ) {
      $current_screen = get_current_screen();

      unset($action_links['dismiss']);
      unset($action_links['install']);
      unset($action_links['update']);
      unset($action_links['activate']);

      if ($current_screen->id != 'bookyourtravel_page_bookyourtravel-plugins') {
          $link_template = '<a class="button-primary" style="margin-top: 10px" href="' . esc_url(self_admin_url('admin.php?page=bookyourtravel-plugins')) . '">' . esc_attr__('Manage Plugins', 'bookyourtravel') . '</a>';
          $action_links  = array(
            'install' => $link_template,
          );
      }

      return $action_links;
    }

    public function edit_admin_menus() {
        global $submenu;

        // Change BookYourTravel to Welcome.
        if (current_user_can('switch_themes')) {
            $submenu['bookyourtravel'][0][0] = esc_attr__('Welcome', 'bookyourtravel');
        }
    }

    public function admin_menu() {
        global $bookyourtravel_theme_globals;
        $plugins_callback = [ $this, 'plugins_tab' ];
        if ( isset( $_GET['tgmpa-install'] ) || isset( $_GET['tgmpa-update'] ) ) { // phpcs:ignore WordPress.Security
            require_once $this->includes_path . '/plugins/class-tgm-plugin-activation.php';
            remove_action( 'admin_notices', array( $GLOBALS['tgmpa'], 'notices' ) );
            $plugins_callback = array( $GLOBALS['tgmpa'], 'install_plugins_page' );
        }

        $welcome_screen = add_menu_page( 'BookYourTravel', 'BookYourTravel', 'switch_themes', 'bookyourtravel', array($this, 'welcome_screen'), 'dashicons-controls-play', 4.9 );
        $license       = add_submenu_page( 'bookyourtravel', esc_attr__( 'License', 'bookyourtravel' ), esc_attr__( 'License', 'bookyourtravel' ), 'install_plugins', 'bookyourtravel-licence', array( $this, 'licence_tab' ) );
        if ($bookyourtravel_theme_globals->is_activation_completed()) {
          $demos         = add_submenu_page( 'bookyourtravel', esc_attr__( 'Demos', 'bookyourtravel' ), esc_attr__( 'Demos', 'bookyourtravel' ), 'manage_options', 'bookyourtravel-demos', array( $this, 'demos_tab' ) );
          $plugins       = add_submenu_page( 'bookyourtravel', esc_attr__( 'Plugins', 'bookyourtravel' ), esc_attr__( 'Plugins', 'bookyourtravel' ), 'install_plugins', 'bookyourtravel-plugins', $plugins_callback );
          $support       = add_submenu_page( 'bookyourtravel', esc_attr__( 'Support', 'bookyourtravel' ), esc_attr__( 'Support', 'bookyourtravel' ), 'install_plugins', 'bookyourtravel-support', array( $this, 'support_tab' ) );
        }
    }

    public function welcome_screen() {
        require_once $this->includes_path . '/admin/screens/welcome.php';
    }

    public function demos_tab() {
      global $bookyourtravel_theme_globals;
      require_once $this->includes_path . '/admin/screens/demos.php';
    }

    public function support_tab() {
      global $bookyourtravel_theme_globals;
      require_once $this->includes_path . '/admin/screens/support.php';
    }

    public function plugins_tab() {
      global $bookyourtravel_theme_globals;
      require_once $this->includes_path . '/admin/screens/plugins.php';
    }

    public function licence_tab() {
      require_once $this->includes_path . '/admin/screens/licence.php';
    }

    public function activation_redirect() {
      if ( current_user_can( 'switch_themes' ) ) {
        header( 'Location:' . admin_url() . 'admin.php?page=bookyourtravel' );
      }
    }

    public function render_admin_screen_tabs($screen = 'welcome') {
      ?>
      <div class="about-text">
        <?php
          global $bookyourtravel_theme_globals;
          if (!$bookyourtravel_theme_globals->is_activation_completed()) {
          ?>
          <?php echo esc_html__( 'Thank you for choosing BookYourTravel! Please activate your copy of the theme in order to unlock full access.', 'bookyourtravel' ); ?>
        <?php } else { ?>
          <?php echo esc_html__( 'Congratulations! BookYourTravel is now successfully installed and ready to use. To kickstart your journey, use the interface below to install bundled plugins and import BookYourTravel demos. Check out the Support tab to learn how to receive product support. Enjoy! ', 'bookyourtravel' ); ?>
        <?php } ?>
      </div>
      <div class="nav-tab-wrapper">
        <a href="<?php echo esc_url_raw( ( 'welcome' === $screen ) ? '#' : admin_url( 'admin.php?page=bookyourtravel' ) ); ?>" class="<?php echo ( 'welcome' === $screen ) ? 'nav-tab-active' : ''; ?> nav-tab"><?php esc_attr_e( 'Welcome', 'bookyourtravel' ); ?></a>
        <a href="<?php echo esc_url_raw( ( 'licence' === $screen ) ? '#' : admin_url( 'admin.php?page=bookyourtravel-licence' ) ); ?>" class="<?php echo ( 'licence' === $screen ) ? 'nav-tab-active' : ''; ?> nav-tab"><?php esc_attr_e( 'License', 'bookyourtravel' ); ?></a>
        <?php
          if ($bookyourtravel_theme_globals->is_activation_completed()) {
        ?>
        <a href="<?php echo esc_url_raw( ( 'plugins' === $screen ) ? '#' : admin_url( 'admin.php?page=bookyourtravel-plugins' ) ); ?>" class="<?php echo ( 'plugins' === $screen ) ? 'nav-tab-active' : ''; ?> nav-tab"><?php esc_attr_e( 'Plugins', 'bookyourtravel' ); ?></a>
        <a href="<?php echo esc_url_raw( ( 'demos' === $screen ) ? '#' : admin_url( 'admin.php?page=bookyourtravel-demos' ) ); ?>" class="<?php echo ( 'demos' === $screen ) ? 'nav-tab-active' : ''; ?> nav-tab"><?php esc_attr_e( 'Demos', 'bookyourtravel' ); ?></a>
        <a href="<?php echo esc_url_raw( ( 'support' === $screen ) ? '#' : admin_url( 'admin.php?page=bookyourtravel-support' ) ); ?>" class="<?php echo ( 'support' === $screen ) ? 'nav-tab-active' : ''; ?> nav-tab"><?php esc_attr_e( 'Support', 'bookyourtravel' ); ?></a>
        <?php } ?>
        <a href="https://www.facebook.com/themeenergy" target="_blank" class="dashicons dashicons-facebook-alt nav-tab"></a>
        <a href="https://twitter.com/themeenergy" target="_blank" class="dashicons dashicons-twitter nav-tab"></a>
	    </div>
      <?php
    }

    public function render_admin_screen_footer($screen = 'welcome') {
     ?>
      <div class="bookyourtravel-thanks">
        <p class="description"><?php esc_html_e( 'Thank you for choosing BookYourTravel.', 'bookyourtravel' ); ?></p>
      </div>
     <?php
    }
}

global $bookyourtravel_theme_admin;
// store the instance in a variable to be retrieved later and call init
$bookyourtravel_theme_admin = BookYourTravel_Theme_Admin::get_instance();
