<?php
/**
 * Woostify Compare
 *
 * @package  Woostify Pro
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Woostify_Compare' ) ) :

	/**
	 * Woostify Compare
	 */
	class Woostify_Compare {
		/**
		 * Instance Variable
		 *
		 * @var instance
		 */
		private static $instance;

		/**
		 *  Initiator
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Constructor.
		 */
		public function __construct() {
			$this->define_constants();
			$this->includes();

			// Save settings.
			$woocommerce_helper = Woostify_Woocommerce_Helper::init();
			add_action( 'wp_ajax_woostify_save_compare_options', array( $woocommerce_helper, 'save_options' ) );

			// Add Setting url.
			add_action( 'admin_menu', array( $this, 'add_setting_url' ) );
			// Register settings.
			add_action( 'admin_init', array( $this, 'register_settings' ) );
		}

		/**
		 * Define constant
		 */
		public function define_constants() {
			if ( ! defined( 'WOOSTIFY_PRO_COMPARE' ) ) {
				define( 'WOOSTIFY_PRO_COMPARE', WOOSTIFY_PRO_VERSION );
			}
		}

		/**
		 * Include required core files used in admin and on the frontend.
		 */
		public function includes() {
			require_once WOOSTIFY_PRO_MODULES_PATH . 'woocommerce/compare/inc/class-woostify-compare-frontend.php';

			if ( is_admin() ) {
				require_once WOOSTIFY_PRO_MODULES_PATH . 'woocommerce/compare/inc/class-woostify-compare-admin.php';
			}
		}

		/**
		 * Add submenu
		 *
		 * @see  add_submenu_page()
		 */
		public function add_setting_url() {
			$position = 7;
			$sub_menu = add_submenu_page( 'woostify-welcome', 'Settings', __( 'Compare', 'woostify-pro' ), 'manage_options', 'compare-settings', array( $this, 'add_settings_page' ), $position );
		}

		/**
		 * Register settings
		 */
		public function register_settings() {
			register_setting( 'compare-settings', 'woostify_compare_enable' );
			register_setting( 'compare-settings', 'woostify_compare_select_page' );
			register_setting( 'compare-settings', 'woostify_compare_fields' );
			register_setting( 'compare-settings', 'woostify_compare_empty_text' );
		}

		/**
		 * Get options
		 */
		public function get_options() {
			$options                = array();
			$options['enable']      = get_option( 'woostify_compare_enable', '0' );
			$options['select_page'] = get_option( 'woostify_compare_select_page', '' );
			$options['fields']      = get_option( 'woostify_compare_fields', 'description,sku,price,availability,rating,weight,dimensions,add_to_cart' );
			$options['empty_text']  = get_option( 'woostify_compare_empty_text', __( 'No products added in the compare list. You must add some products to compare them. You will find a lot of interesting products on our "Shop" page.', 'woostify-pro' ) );

			return $options;
		}

		/**
		 * Create Settings page
		 */
		public function add_settings_page() {
			$options = $this->get_options();
			$pages   = get_pages();
			$available_fields = array(
				'description'  => __( 'Description', 'woostify-pro' ),
				'sku'          => __( 'Sku', 'woostify-pro' ),
				'availability' => __( 'Availability', 'woostify-pro' ),
				'weight'       => __( 'Weight', 'woostify-pro' ),
				'dimensions'   => __( 'Dimensions', 'woostify-pro' ),
			);

			if ( function_exists( 'wc_get_attribute_taxonomies' ) ) {
				$attribute_taxonomies = wc_get_attribute_taxonomies();
				if ( ! empty( $attribute_taxonomies ) ) {
					foreach ( $attribute_taxonomies as $tax ) {
						$available_fields[ 'pa_' . $tax->attribute_name ] = wc_attribute_label( 'pa_' . $tax->attribute_name );
					}
				}
			}
			?>
			<div class="woostify-options-wrap woostify-featured-setting woostify-compare-setting" data-id="compare" data-nonce="<?php echo esc_attr( wp_create_nonce( 'woostify-compare-setting-nonce' ) ); ?>">

				<?php Woostify_Admin::get_instance()->woostify_save_option_messages(); ?>

				<?php Woostify_Admin::get_instance()->woostify_welcome_screen_header(); ?>

				<div class="wrap woostify-settings-box">
					<div class="woostify-welcome-container">
						<div class="woostify-notices-wrap">
							<h2 class="notices" style="display:none;"></h2>
						</div>
						<div class="woostify-settings-content">
							<h4 class="woostify-settings-section-title"><?php esc_html_e( 'Compare', 'woostify-pro' ); ?></h4>

							<div class="woostify-settings-section-content">
								<table class="form-table">

									<tr class="woostify-filter-item">
										<th scope="row"><?php esc_html_e( 'Enable', 'woostify-pro' ); ?>:</th>
										<td>
											<label for="woostify_compare_enable">
												<input class="woostify-filter-value" name="woostify_compare_enable" type="checkbox" id="woostify_compare_enable" value="<?php echo esc_attr( '1' === $options['enable'] ? '1' : '0' ); ?>" <?php checked( $options['enable'], '1' ); ?>>
											</label>
										</td>
									</tr>

									<tr class="woostify-filter-item<?php echo esc_attr( '1' === $options['enable'] ? '' : ' hidden' ); ?>" data-type="0">
										<th scope="row"><?php esc_html_e( 'Compare page', 'woostify-pro' ); ?>:</th>
										<td>
											<select name="woostify_compare_select_page" id="woostify_compare_select_page" class="field-empty">
												<option value=""><?php esc_html_e( 'Select Page', 'woostify-pro' ); ?></option>
												<?php foreach ( $pages as $page ) : ?>
													<option value="<?php echo esc_attr( $page->ID ); ?>" <?php selected( $options['select_page'], $page->ID ); ?>>
														<?php echo esc_html( $page->post_title ); ?>
													</option>
												<?php endforeach; ?>
											</select>
											<p class="woostify-setting-description"><?php esc_html_e( 'Select a page for the compare table. It should contain the shortcode: [woostify_compare]', 'woostify-pro' ); ?></p>
										</td>
									</tr>

									<tr class="woostify-filter-item<?php echo esc_attr( '1' === $options['enable'] ? '' : ' hidden' ); ?>" data-type="0">
										<th scope="row"><?php esc_html_e( 'Select fields for compare table', 'woostify-pro' ); ?>:</th>
										<td>
											<div class="woostify-add-custom-field woostify-compare-fields-wrapper">
												<div class="custom-field-wrapper">
													<div class="woostify-custom-field-control">
														<?php
														// Draw list selected.
														if ( ! empty( $options['fields'] ) ) {
															$compare_fields_selected = explode( ',', $options['fields'] );
															foreach ( $compare_fields_selected as $field ) {
																if ( ! isset( $available_fields[ $field ] ) ) {
																	continue;
																}
																?>
																<span class="woostify-auto-complete-key">
																	<span class="woostify-title">
																		<?php echo esc_html( $available_fields[ $field ] ); ?>
																	</span>
																	<span class="btn-woostify-auto-complete-delete ion-close" data-item="<?php echo esc_attr( $field ); ?>"></span>
																</span>
																<?php
															}
														}
														?>
														<input type="text" id="woostify_add_compare_field" placeholder="<?php esc_attr_e( 'Select fields...', 'woostify-pro' ); ?>">
														<input type="hidden" id="woostify_compare_fields_input" name="woostify_compare_fields" class="field-empty" value="<?php echo esc_attr( $options['fields'] ); ?>">
													</div>

													<div class="list-custom-field">
														<ul class="custom-fields">
															<?php foreach ( $available_fields as $key => $label ) : ?>
																<li class="field-item" data-value="<?php echo esc_attr( $key ); ?>" data-label="<?php echo esc_attr( $label ); ?>"><?php echo esc_html( $label ); ?></li>
															<?php endforeach; ?>
														</ul>
													</div>
												</div>
											</div>
											<p class="woostify-setting-description"><?php esc_html_e( 'Choose which fields should be presented on the product compare page with table.', 'woostify-pro' ); ?></p>
										</td>
									</tr>

									<tr class="woostify-filter-item<?php echo esc_attr( '1' === $options['enable'] ? '' : ' hidden' ); ?>" data-type="0">
										<th scope="row"><?php esc_html_e( 'Empty compare text', 'woostify-pro' ); ?>:</th>
										<td>
											<textarea name="woostify_compare_empty_text" class="field-empty" rows="5" style="width: 100%; max-width: 600px;" placeholder="<?php esc_attr_e( 'No products added in the compare list. You must add some products to compare them. You will find a lot of interesting products on our "Shop" page.', 'woostify-pro' ); ?>"><?php echo esc_html( $options['empty_text'] ); ?></textarea>
											<p class="woostify-setting-description"><?php esc_html_e( 'Text will be displayed if user don\'t add any products to compare', 'woostify-pro' ); ?></p>
										</td>
									</tr>

								</table>
							</div>

							<div class="woostify-settings-section-footer">
								<span class="save-options button button-primary"><?php esc_html_e( 'Save', 'woostify-pro' ); ?></span>
								<span class="spinner"></span>
							</div>
						</div>
					</div>
				</div>
			</div>

			<?php
		}
	}

	Woostify_Compare::get_instance();
endif;
