<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://wedima.de
 * @since      1.0.0
 *
 * @package    Wedima_Site
 * @subpackage Wedima_Site/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Wedima_Site
 * @subpackage Wedima_Site/admin
 * @author     Jitendra Bodmann <jitendra.bodmann@wedima.de>
 */
class Wedima_Site_Admin
{

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $wedima_site    The ID of this plugin.
	 */
	private $wedima_site;

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

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $wedima_site       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct($wedima_site, $version)
	{

		$this->wedima_site = $wedima_site;
		$this->version = $version;
	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles()
	{

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Wedima_Site_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Wedima_Site_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style($this->wedima_site, plugin_dir_url(__FILE__) . 'css/wedima-site-admin.css', array(), $this->version, 'all');
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts()
	{

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Wedima_Site_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Wedima_Site_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_script($this->wedima_site, plugin_dir_url(__FILE__) . 'js/wedima-site-admin.js', array('jquery'), $this->version, false);
	}

	public function wedima_site_register_cli_commands()
	{
		require_once WEDIMA_SITE_ROOT_DIR . 'cli/class-wedima-site-cli.php';
		Wedima_Site_CLI::register_all_commands();
	}

	/**
	 * Register the menu and sub menu items for the admin area
	 *
	 * WP admin hook defined in plugin-dir/includes/class-plugin-name.php -> define_admin_hooks()
	 *
	 * @since    1.0.0
	 */
	public function wedima_site_admin_menu()
	{
		// MAIN MENU

		$main_menu_slug = 'wedima-site';

		add_menu_page(
			__('Wedima Site', 'wedima-site'),
			__('Wedima Site', 'wedima-site'), // this is the name of the main menu item
			'manage_options',
			$main_menu_slug,
			array($this, 'wedima_site_main_admin_page'),
			'dashicons-plugins-checked',
			3
		);

		// submenu page to make the first entry appear as Dashboard like in https://youtu.be/QR1bRauCj74
		// therefore same main menu slug and callback function
		add_submenu_page(
			$main_menu_slug,
			__('Wedima Site', 'wedima-site'),
			__('Dashboard', 'wedima-site'),
			'manage_options',
			$main_menu_slug,
			array($this, 'wedima_site_main_admin_page')
		);
	}

	/**
	 * the view for the main admin page (dashboard page)
	 *
	 * @since 1.0.0
	 */
	public function wedima_site_main_admin_page()
	{
		require_once 'partials/wedima-site-admin-display.php';
	}

	/**
	 * register the settings for the plugin:
	 *
	 * WP admin hook defined in plugin-dir/includes/class-plugin-name.php -> define_admin_hooks()
	 *
	 * @since 1.0.0
	 */
	public function wedima_site_register_settings()
	{
		// general plugin related settings
		add_option('wedima_site_purge_on_uninstall', 0); // this setting is used in the includes/class-plugin-name-deactivator.php
		register_setting('wedima_site_options_group', 'wedima_site_purge_on_uninstall');

		// site options
		$site_options = array(
			['wedima_site_client_name', 'Wedima'],
			['wedima_site_enable_login_language_switcher', 0], 
			['wedima_site_disable_mail_templates', 0], 
		);
		foreach ($site_options as $option) {
			add_option($option[0], $option[1]);
			register_setting('wedima_site_site_options_options_group', $option[0]);
		}

		// maintenance settings
		add_option('wedima_site_maintenance_mode', 0);
		register_setting('wedima_site_maintenance_options_group', 'wedima_site_maintenance_mode');
	}

	/**
	 * Redesigns the emails for all outgoing mails
	 *
	 * @param [type] $args
	 * @return void
	 */
	function wedima_site_redesign_mails($args)
	{
		//$args['message'];
		//$args['subject']
		//$args['message']
		//$args['headers']
		//$args['attachments']

		require_once(WEDIMA_SITE_ROOT_DIR . 'utils/mail/class-wedima-site-mail-template-util.php');
		$mail_template = Wedima_Site_Mail_Template_Util::load_mail_template($args['message']);
		$args['message'] = $mail_template;
		return $args;
	}

	/**
	 * Sets mail_content_type to text/html by default.
	 *
	 * @return void
	 */
	function wedima_site_change_mail_content_type()
	{
		return "text/html";
	}

	/**
	 * New template for retrieve Password message email
	 *
	 * @param [type] $message
	 * @param [type] $key
	 * @param [type] $user_login
	 * @param [type] $user_data
	 * @return void
	 */
	function wedima_site_retrieve_password_message($message, $key, $user_login, $user_data)
	{
		$client_name = get_option('wedima_site_client_name', 'Wedima');
		ob_start();
		include(WEDIMA_SITE_ROOT_DIR . 'utils/mail/templates/retrieve-password.php');
		return ob_get_clean();
	}

	/**
	 * New template for the new user notification email
	 *
	 * @param [type] $wp_new_user_notification_email
	 * @param [type] $user
	 * @param [type] $blogname
	 * @return void
	 */
	function wedima_site_new_user_notification_email($wp_new_user_notification_email, $user, $blogname)
	{
		$key = get_password_reset_key($user);
		$client_name = get_option('wedima_site_client_name', 'Wedima');
		ob_start();
		include(WEDIMA_SITE_ROOT_DIR . 'utils/mail/templates/new-user-notification.php');
		$message = ob_get_clean();
		$wp_new_user_notification_email['message'] = $message;
		return $wp_new_user_notification_email;
	}

	/**
	 * Hides the admin notifications for every user except for the admin.
	 *
	 * @return void
	 */
	function wedima_site_hide_admin_notices()
	{
		if (!is_super_admin()) {
			remove_all_actions('user_admin_notices');
			remove_all_actions('admin_notices');
		}
	}
}
