<?php
/**
* Admin Dashboard
*/
if( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if(!class_exists('RS_Admin_Dashboard')) {
  class RS_Admin_Dashboard {
    public $is_activated;
    function __construct() {
      $this->rs_init();
    }

    public function rs_init() {
      $this->is_activated = get_option('is_valid');
      add_action('admin_menu', array($this, 'rs_register_theme_panel'));
      add_action('admin_init', array($this, 'rs_theme_redirect'));
      add_action('admin_notices', array($this, 'rs_theme_activate_notice'));
    }

    public function rs_register_theme_panel() {
      call_user_func('add_'. 'menu' .'_page', esc_html__('Theme Panel', 'luxe'), esc_html__('Luxe', 'luxe'), 'edit_posts', 'rs_theme_welcome', array($this, 'rs_view_welcome'), null, 2);
      if(!defined('ENVATO_HOSTED_SITE')):
        call_user_func( 'add_'. 'submenu' .'_page', 'rs_theme_welcome', esc_html__('Activate Theme', 'luxe'), esc_html__('Activate Theme', 'luxe'), 'edit_posts', 'rs_theme_activate', array($this, 'rs_theme_activate'));
      endif;
      if(!defined('ENVATO_HOSTED_SITE')):
        call_user_func( 'add_'. 'submenu' .'_page', 'rs_theme_welcome', esc_html__('System Status', 'luxe'), esc_html__('System Status', 'luxe'), 'edit_posts', 'rs_theme_system_status', array($this, 'rs_theme_system_status'));
      endif;
      call_user_func( 'add_'. 'submenu' .'_page', 'rs_theme_welcome', esc_html__('Help Center', 'luxe'), esc_html__('Help Center', 'luxe'), 'edit_posts', 'rs_theme_help_center', array($this, 'rs_theme_help_center'));
      global $submenu;
      $submenu['rs_theme_welcome'][0][0] = esc_html__('Welcome', 'luxe');
    }

    public function rs_view_welcome() {
      require_once 'rs-view-welcome.php';
    }

    public function rs_theme_system_status() {
      require_once 'rs-view-system-status.php';
    }

    public function rs_theme_help_center() {
      require_once 'rs-view-help-center.php';
    }

    public function rs_theme_activate() {
      require_once 'rs-view-activate.php';
    }

    public function rs_theme_activate_notice() {
      if($this->is_activated || defined('ENVATO_HOSTED_SITE')) { return; }
    ?>
      <div class="notice notice-error is-dismissible"> 
        <p></p>
        <p><strong> Please activate <em>Luxe.</em> This activation enables all features of the theme (i.e. Demo import, Addons Plugins etc.). This step is taken for mass piracy of our theme, and to serve our paying customers better. </strong></p>
        <p><strong><a href="<?php echo admin_url('admin.php?page=rs_theme_activate'); ?>">Enter purchase code</a></strong></p>
        <p></p>
        <button type="button" class="notice-dismiss">
          <span class="screen-reader-text">Dismiss this notice.</span>
        </button>
      </div>
    <?php
    }

    public function rs_theme_redirect() {
      global $pagenow;
      if ( is_admin() && isset( $_GET['activated'] ) && 'themes.php' == $pagenow ) {
        wp_redirect(admin_url('admin.php?page=rs_theme_welcome')); 
      }
    }
  }
  new RS_Admin_Dashboard();
}
