<?php
/**
 * WPFactory Admin Menu
 *
 * @version 1.0.7
 * @since   1.0.0
 * @author  WPFactory
 */

namespace WPFactory\WPFactory_Admin_Menu;

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'WPFactory\WPFactory_Admin_Menu\WPFactory_Admin_Menu' ) ) {

	/**
	 * WPFactory Admin Menu.
	 *
	 * @version 1.0.0
	 * @since   1.0.0
	 */
	class WPFactory_Admin_Menu {

		use Singleton;

		/**
		 * Version.
		 *
		 * @since   1.0.1
		 *
		 * @var string
		 */
		protected $version = '1.0.8';

		/**
		 * Menu slug.
		 *
		 * @since   1.0.0
		 *
		 * @var string
		 */
		protected $menu_slug = 'wpfactory';

		/**
		 * Page title.
		 *
		 * @since   1.0.0
		 *
		 * @var string
		 */
		protected $page_title = '';

		/**
		 * Menu title.
		 *
		 * @since   1.0.0
		 *
		 * @var string
		 */
		protected $menu_title = '';

		/**
		 * Capability.
		 *
		 * @since   1.0.0
		 *
		 * @var string
		 */
		protected $capability = '';

		/**
		 * Icon URL.
		 *
		 * @since   1.0.0
		 *
		 * @var string
		 */
		protected $icon_url = '';

		/**
		 * Position.
		 *
		 * @since   1.0.0
		 *
		 * @var string
		 */
		protected $position = 64;

		/**
		 * $wc_settings_menu_item_swapper.
		 *
		 * @since 1.0.0
		 *
		 * @var WC_Settings_Menu_Item_Swapper
		 */
		protected $wc_settings_menu_item_swapper = null;

		/**
		 * Constructor.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 */
		protected function __construct() {
			$this->localize();

			// Setups add_menu_page params.
			$this->set_menu_title( __( 'WPFactory', 'wpfactory-admin-menu' ) );
			$this->set_page_title( __( 'WPFactory', 'wpfactory-admin-menu' ) );
			$this->set_icon_url( 'data:image/svg+xml;base64, ' );

			// WPFactory admin page.
			add_action( 'admin_menu', array( $this, 'create_wpfactory_admin_menu' ), 9 );
		}

		/**
		 * Moves WooCommerce Settings tab to WPFactory menu as a submenu item.
		 *
		 * @version 1.0.7
		 * @since   1.0.0
		 *
		 * @param $args
		 *
		 * @return void
		 */
		public function move_wc_settings_tab_to_wpfactory_menu( $args = null ) {
			if ( is_null( $this->wc_settings_menu_item_swapper ) ) {
				$this->wc_settings_menu_item_swapper = new WC_Settings_Menu_Item_Swapper();
			}

			// Initial args.
			$args = wp_parse_args( $args, array(
				'wc_settings_tab_id' => '',
				'page_title'         => '',
				'menu_title'         => '',
				'capability'         => class_exists( 'WooCommerce' ) ? 'manage_woocommerce' : 'manage_options',
				'position'           => 30,
				'plugin_icon'        => array()
			) );
			if ( empty( $args['page_title'] ) ) {
				$args['page_title'] = $args['menu_title'];
			}

			// Admin menu.
			$replacement_menu_item_slug = 'admin.php?page=wc-settings&tab=' . $args['wc_settings_tab_id'];
			add_action( 'admin_menu', function () use ( $args, $replacement_menu_item_slug ) {
				\add_submenu_page(
					$this->menu_slug,
					$args['menu_title'],
					$args['menu_title'],
					$args['capability'],
					$replacement_menu_item_slug,
					'',
					$args['position']
				);
			} );

			// Setup plugin icon.
			$args['plugin_icon'] = wp_parse_args( $args['plugin_icon'], array(
				'wporg_plugin_slug' => '',
				'get_url_method'    => 'manual', // wporg_plugins_api || manual
				'url'               => '',
				'style'             => '',
				'width'             => '',
				'height'            => '36',
			) );

			// Swap.
			$this->wc_settings_menu_item_swapper->swap( array(
				'wc_settings_tab_id'         => $args['wc_settings_tab_id'],
				'replacement_menu_item_slug' => $replacement_menu_item_slug,
				'page_title'                 => $args['page_title'],
				'plugin_icon'                => $args['plugin_icon']
			) );

			// Init.
			$this->wc_settings_menu_item_swapper->init();
		}

		/**
		 * add_submenu_page.
		 *
		 * @version 1.0.2
		 * @since   1.0.2
		 *
		 * @param $page_title
		 * @param $menu_title
		 * @param $capability
		 * @param $menu_slug
		 * @param $callback
		 * @param $position
		 *
		 * @return void
		 */
		function add_submenu_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
			\add_submenu_page(
				$this->menu_slug,
				$page_title,
				$menu_title,
				$capability,
				$menu_slug,
				$callback,
				$position
			);
		}

		/**
		 * Creates WPFactory admin menu.
		 *
		 * @version 1.0.3
		 * @since   1.0.0
		 *
		 * @return void
		 */
		function create_wpfactory_admin_menu() {
			// Set capability.
			$this->set_capability( class_exists( 'WooCommerce' ) ? 'manage_woocommerce' : 'manage_options' );

			// Menu page.
			\add_menu_page(
				$this->get_page_title(),
				$this->get_menu_title(),
				$this->get_capability(),
				$this->get_menu_slug(),
				array( $this, 'render_page' ),
				$this->get_icon_url(),
				$this->get_position()
			);

			// Removes submenu page.
			add_action( 'admin_head', function () {
				remove_submenu_page( $this->menu_slug, $this->menu_slug );
			} );
		}

		/**
		 * Renders page.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return void
		 */
		function render_page() {

		}

		/**
		 * Localizes the plugin.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return void
		 */
		public function localize() {
			$domain = 'wpfactory-admin-menu';
			$locale = get_locale();
			$mofile = dirname( $this->get_library_file_path() ) . '/langs/' . $domain . '-' . $locale . '.mo';
			load_textdomain( $domain, $mofile );
		}

		/**
		 * get_menu_slug.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return string
		 */
		public function get_menu_slug() {
			return $this->menu_slug;
		}

		/**
		 * set_menu_slug.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @param   string  $menu_slug
		 */
		public function set_menu_slug( $menu_slug ) {
			$this->menu_slug = $menu_slug;
		}

		/**
		 * get_page_title.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return string
		 */
		public function get_page_title() {
			return $this->page_title;
		}

		/**
		 * set_page_title
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @param   string  $page_title
		 */
		public function set_page_title( $page_title ) {
			$this->page_title = $page_title;
		}

		/**
		 * get_menu_title.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return string
		 */
		public function get_menu_title() {
			return $this->menu_title;
		}

		/**
		 * set_menu_title.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @param   string  $menu_title
		 */
		public function set_menu_title( $menu_title ) {
			$this->menu_title = $menu_title;
		}

		/**
		 * get_capability.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return string
		 */
		public function get_capability() {
			return $this->capability;
		}

		/**
		 * set_capability.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @param   string  $capability
		 */
		public function set_capability( $capability ) {
			$this->capability = $capability;
		}

		/**
		 * get_icon_url.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return string
		 */
		public function get_icon_url() {
			return $this->icon_url;
		}

		/**
		 * set_icon_url
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @param   string  $icon_url
		 */
		public function set_icon_url( $icon_url ) {
			$this->icon_url = $icon_url;
		}

		/**
		 * get_position.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return int|string
		 */
		public function get_position() {
			return $this->position;
		}

		/**
		 * set_position.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @param   int|string  $position
		 */
		public function set_position( $position ) {
			$this->position = $position;
		}

		/**
		 * get_version.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return string
		 */
		public function get_version() {
			return $this->version;
		}

		/**
		 * get_file_path.
		 *
		 * @version 1.0.0
		 * @since   1.0.0
		 *
		 * @return string
		 */
		function get_library_file_path() {
			return dirname( __FILE__, 2 );
		}
	}
}