<?php
/**
 * Class for Smart Offers + WooCommerce Buy Now Settings
 *
 * @package     smart-offers/includes/admin/
 * @since       4.0.0
 * @version     1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'SO_Admin_Settings' ) ) {

	/**
	 * Class for Smart Offers Settings
	 */
	class SO_Admin_Settings {

		/**
		 * Variable to hold instance of Smart Offers Settings
		 *
		 * @var $instance
		 */
		private static $instance = null;

		/**
		 * The Smart Offers settings page name
		 *
		 * @var string
		 */
		public $page = 'so-settings';

		/**
		 * Default tab to load
		 *
		 * @var string
		 */
		public $default_tab = 'so_settings';

		/**
		 * List of tabs
		 *
		 * @var array
		 */
		public $nav_tabs = array();

		/**
		 * Current active tab
		 *
		 * @var string
		 */
		public $current_tab = '';

		/**
		 * Constructor
		 */
		public function __construct() {

		}

		/**
		 * Get single instance of Smart Offers Settings.
		 *
		 * @return SO_Admin_Settings Singleton object of SO_Admin_Settings
		 */
		public static function get_instance() {

			// Check if instance is already exists.
			if ( is_null( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Function to load Smart Offers page content
		 */
		public function load() {
			$this->register_nav_tabs();
			$this->set_current_tab();
			$this->render_nav_tabs();
		}

		/**
		 * Function to register navigation tabs
		 */
		public function register_nav_tabs() {
			$this->nav_tabs = array(
				'so_settings'           => __( 'Smart Offers', 'smart-offers' ),
				'bn_storewide_settings' => __( 'Buy Now', 'smart-offers' ),
			);
		}

		/**
		 * Function to set current active tab
		 */
		public function set_current_tab() {
			$this->current_tab = isset( $_GET['tab'] ) ? sanitize_title( wp_unslash( $_GET['tab'] ) ) : $this->default_tab; // phpcs:ignore
		}

		/**
		 * Function to display navigation tabs on Smart Offers admin page
		 */
		public function render_nav_tabs() {
			if ( ! empty( $this->nav_tabs ) ) { ?>
				<div class="wrap woocommerce">
					<h2 class="nav-tab-wrapper">
					<?php
					foreach ( $this->nav_tabs as $tab => $name ) {
						$class = ( $tab === $this->current_tab ) ? 'nav-tab-active' : '';

						$tab_url = add_query_arg(
							array(
								'post_type' => 'smart_offers',
								'page' => $this->page,
								'tab'  => $tab,

							),
							admin_url( 'edit.php' )
						);

						echo '<a class="nav-tab ' . esc_attr( $class ) . '" href="' . esc_url( $tab_url ) . '">' . esc_html( $name ) . '</a>';
					}
					?>
					</h2>
					<?php
						$method = 'render_' . $this->current_tab . '_fields';
						$this->$method();
					?>
				</div>
				<?php
			}
		}

		/**
		 * Function to display Smart Offers storewide settings
		 */
		public function render_so_settings_fields() {
			include ( 'class-so-admin-setting.php' );
		}

		/**
		 * Function to display Buy Now storewide settings
		 */
		public function render_bn_storewide_settings_fields() {
			include_once SA_SO_PLUGIN_DIRPATH . '/modules/buy-now/php/admin/class-sa-wc-buy-now-settings.php';
			$bn_generate_link = SA_WC_Buy_Now_Settings::get_instance();
			$bn_generate_link->show_bn_settings();
		}

	}
}

new SO_Admin_Settings();
