<?php

/**
 * The admin-specific functionality of the plugin.
 *
 */
class Ekc_Tournament_Manager_Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialization
	 *
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the admin area.
	 */
	public function enqueue_styles() {
		wp_enqueue_style( 'jquery-ui-theme', plugin_dir_url( __FILE__ ) . 'css/jquery-ui.min.css', array(), $this->version, 'all' );
		wp_enqueue_style( 'jquery-confirm', plugin_dir_url( __FILE__ ) . 'css/jquery-confirm.min.css', array(), $this->version, 'all' );
		wp_enqueue_style( 'selectmenu-flags', plugin_dir_url( __FILE__ ) . 'css/flag-icon.min.css', array(), $this->version, 'all' );
		wp_enqueue_style( $this->plugin_name . '-admin', plugin_dir_url( __FILE__ ) . 'css/admin.css', array(), $this->version, 'all' );
	}

	/**
	 * Register the JavaScript for the admin area.
	 */
	public function enqueue_scripts() {
		wp_enqueue_script( 'jquery-confirm', plugin_dir_url( __FILE__ ) . 'js/jquery-confirm.min.js', array( 'jquery' ), $this->version, false );
		wp_enqueue_script( $this->plugin_name . '-admin', plugin_dir_url( __FILE__ ) . 'js/admin.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-button', 'jquery-ui-menu', 'jquery-ui-tooltip', 'jquery-ui-datepicker', 'jquery-ui-selectmenu', 'jquery-ui-autocomplete' ), $this->version, false );
		wp_localize_script( $this->plugin_name . '-admin', 'ekc_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
	}

	/**
	 * Callback for filter 'mce_external_plugins'.
	 * Returns a map with TinyMCE plugin name to plugin url.
	 */
	public function tinymce_external_plugins( $plugins ) {
		$plugins['ekc-emoticons'] = plugin_dir_url( __FILE__ ) . 'tinymce/plugins/ekc-emoticons/plugin.js';
		return $plugins;
	}

	public function filter_map_meta_cap( $caps, $cap, $user_id, $args ) {
		$helper = new Ekc_Role_Helper();
		return $helper->filter_map_meta_cap( $caps, $cap, $user_id, $args );
	}

	public function on_wp_mail_succeeded( $mail_data ) {
		$helper = new Ekc_Shareable_Links_Helper();
		return $helper->on_wp_mail_succeeded( $mail_data );
	}

	public function on_wp_mail_failed( $wp_error ) {
		$helper = new Ekc_Shareable_Links_Helper();
		return $helper->on_wp_mail_failed( $wp_error );
	}

	public function check_scheduled_mails() {
		$helper = new Ekc_Shareable_Links_Helper();
		return $helper->check_scheduled_mails();
	}

	/**
	 * Adds a custom cron interval with 5 minutes
	 */
	public function filter_cron_schedules( $schedules ) {
		$schedules['ekc_email_scheduler'] = array(
			'interval' => 300, /* 300 seconds = 5 minutes */
			'display'  => esc_html__( 'Every 5 minutes' )
		);
		return $schedules;
	}

	public function ekc_duplicate_page_link( $actions, $post ) {
		$helper = new Ekc_Page_Helper();
		return $helper->ekc_duplicate_page_link( $actions, $post );
	}

	public function ekc_duplicate_page( $post ) {
		$helper = new Ekc_Page_Helper();
		return $helper->ekc_duplicate_page( $post );
	}

	public function add_tournament_menu() {
	    add_menu_page(
	        'Tournaments',
	        'EKC Tournaments',
	        Ekc_Role_Helper::CAPABILITY_EKC_READ_TOURNAMENTS,
	        'ekc-tournaments',
	        array( $this, 'create_tournaments_page' ),
			// EKA logo as base64 encoded svg image
			'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhLS0gQ3JlYXRlZCB3aXRoIElua3NjYXBlIChodHRwOi8vd3d3Lmlua3NjYXBlLm9yZy8pIC0tPgoKPHN2ZwogICB4bWxuczpkYz0iaHR0cDovL3B1cmwub3JnL2RjL2VsZW1lbnRzLzEuMS8iCiAgI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NDU4MzMzLC0xNC4xMDIwNTgsLTEzLjc4NTY3MikiCiAgICAgICBpZD0iZzEyIj4KICAgICAgPHBvbHlnb24KICAgICAgICAgcG9pbnRzPSIyMjkuNiwxNTIuODkgMjQ5LjkzLDIxNC45OCAxOTcuMjEsMTc2LjQzIDE0NC4yMSwyMTQuOTggMTU0Ljc2LDE4My44IDE2NS4xOCwxNTIuOTcgMTExLjg4LDExNS4xNCAxNzcuNzEsMTE1LjE0IDIxNy4yMywxMTUuMTQgMjgxLjQ1LDExNS4xNCAiCiAgICAgICAgIGlkPSJwb2x5Z29uNiIKICAgICAgICAgc3R5bGU9ImZpbGw6IzAwMDAwMCIgLz4KICAgICAgPHBvbHlnb24KICAgICAgICAgY2xhc3M9ImNscy0xIgogICAgICAgICBwb2ludHM9IjE5Ny4yMSwxNzYuNDMgMTQ0LjIxLDIxNC45OCAxNTQuNzYsMTgzLjggMjQ5LjEzLDExNS4xNCAyODEuNDUsMTE1LjE0ICIKICAgICAgICAgaWQ9InBvbHlnb244IgogICAgICAgICBzdHlsZT0iZmlsbDojMDAwMDAwIiAvPgogICAgICA8cG9seWdvbgogICAgICAgICBjbGFzcz0iY2xzLTIiCiAgICAgICAgIHBvaW50cz0iMTYyLjQ1LDEwMi4yMiAxNzcuMzYsMTAyLjIyIDIxNi4zMywxMDIuMjQgMjMwLjkxLDEwMi4yNCAyMzAuOTEsNzEuMTcgMjEwLjQ3LDgzLjkxIDE5Ni40Niw3MC44OCAxODEuNTMsODMuOTEgMTYyLjQzLDcxLjIyICIKICAgICAgICAgaWQ9InBvbHlnb24xMCIKICAgICAgICAgc3R5bGU9ImZpbGw6IzAwMDAwMCIgLz4KICAgIDwvZz4KICA8L2c+Cjwvc3ZnPgo=');

	    add_submenu_page(
	        '', // no parent menu
			'Teams',
	        'EKC Teams',
	        Ekc_Role_Helper::CAPABILITY_EKC_EDIT_TOURNAMENTS,
	        'ekc-teams',
			array( $this, 'create_teams_page' ) );

	    add_submenu_page(
	        '', // no parent menu
			'Elimination Bracket',
	        'EKC Elimination Bracket',
	        Ekc_Role_Helper::CAPABILITY_EKC_MANAGE_TOURNAMENTS,
	        'ekc-bracket',
			array( $this, 'create_bracket_page' ) );
			
	    add_submenu_page(
	        '', // no parent menu
			'Swiss System',
	        'EKC Swiss System',
	        Ekc_Role_Helper::CAPABILITY_EKC_MANAGE_TOURNAMENTS,
	        'ekc-swiss',
			array( $this, 'create_swiss_system_page' ) );

		add_submenu_page(
	        '', // no parent menu
			'Check-in',
			'EKC Check-in',
			Ekc_Role_Helper::CAPABILITY_EKC_MANAGE_TOURNAMENTS,
			'ekc-check-in',
			array( $this, 'create_check_in_page' ) );

	    add_submenu_page(
	        '', // no parent menu
			'Shareable Links',
	        'EKC Shareable Links',
	        Ekc_Role_Helper::CAPABILITY_EKC_MANAGE_TOURNAMENTS,
	        'ekc-links',
	        array( $this, 'create_shareable_links_page' ) );

		add_submenu_page(
			'', // no parent menu
			'Email Status',
			'EKC Email Status',
			Ekc_Role_Helper::CAPABILITY_EKC_MANAGE_TOURNAMENTS,
			'ekc-email-status',
			array( $this, 'create_email_status_page' ) );

	    add_submenu_page(
	        '', // no parent menu
			'Backup',
	        'EKC Tournament Backup',
	        Ekc_Role_Helper::CAPABILITY_EKC_MANAGE_BACKUPS,
	        'ekc-backup',
	        array( $this, 'create_tournaments_backup_page' ) );

		add_submenu_page(
	        '', // no parent menu
			'Result Log',
			'EKC Result Log',
			Ekc_Role_Helper::CAPABILITY_EKC_MANAGE_TOURNAMENTS,
			'ekc-result-log',
			array( $this, 'create_result_log_page' ) );
	}

	public function create_tournaments_page() {
		$tournaments_page = new Ekc_Tournaments_Admin_Page();
		return $tournaments_page->create_tournaments_page();
	}

	public function create_teams_page() {
		$teams_page = new Ekc_Teams_Admin_Page();
		return $teams_page->create_teams_page();
	}

	public function create_bracket_page() {
		$bracket_page = new Ekc_Elimination_Bracket_Admin_Page();
		return $bracket_page->create_elimination_bracket_page();
	}

	public function create_swiss_system_page() {
		$swiss_system_page = new Ekc_Swiss_System_Admin_Page();
		return $swiss_system_page->create_swiss_system_page();
	}

	public function create_shareable_links_page() {
		$links_page = new Ekc_Shareable_Links_Admin_Page();
		return $links_page->create_shareable_links_page();
	}

	public function create_email_status_page() {
		$email_status_page = new Ekc_Email_Status_Page();
		return $email_status_page->create_email_status_page();
	}

	public function create_check_in_page() {
		$check_in_page = new Ekc_Check_In_Page();
		return $check_in_page->create_check_in_page();
	}

	public function create_tournaments_backup_page() {
		$backup_page = new Ekc_Tournaments_Backup_Page();
		return $backup_page->create_tournaments_backup_page();
	}

	public function create_result_log_page() {
		$result_log_page = new Ekc_Result_Log_Page();
		return $result_log_page->create_result_log_page();
	}

	public function export_teams_as_csv() {
		$teams_page = new Ekc_Teams_Admin_Page();
		return $teams_page->export_teams_as_csv();
	}

	public function download_backup_file() {
		$backup_page = new Ekc_Tournaments_Backup_Page();
		return $backup_page->download_backup_file();
	}

	public function intercept_swiss_system_redirect() {
		$swiss_system_page = new Ekc_Swiss_System_Admin_Page();
		return $swiss_system_page->intercept_redirect();
	}

	public function intercept_elimination_bracket_redirect() {
		$elimination_bracket_page = new Ekc_Elimination_Bracket_Admin_Page();
		return $elimination_bracket_page->intercept_redirect();
	}

	public function intercept_shareable_links_redirect() {
		$shareable_links_page = new Ekc_Shareable_Links_Admin_Page();
		return $shareable_links_page->intercept_redirect();
	}

	public function intercept_check_in_redirect() {
		$check_in_page = new Ekc_Check_In_Page();
		return $check_in_page->intercept_redirect();
	}

	public function intercept_email_status_redirect() {
		$email_status_page = new Ekc_Email_Status_Page();
		return $email_status_page->intercept_redirect();
	}
}
