<?php

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

if ( ! class_exists( 'CR_Product_Feed_Admin_Menu' ) ):

	class CR_Product_Feed_Admin_Menu {

		/**
		* @var string URL to admin diagnostics page
		*/
		protected $page_url;

		/**
		* @var string The slug identifying this menu
		*/
		protected $menu_slug;

		/**
		* @var string The slug of the currently displayed tab
		*/
		protected $current_tab = 'overview';

		public function __construct() {
			$this->menu_slug = 'cr-reviews-product-feed';

			$this->page_url = add_query_arg( array(
				'page' => $this->menu_slug
			), admin_url( 'admin.php' ) );

			if ( isset( $_GET['tab'] ) ) {
				$this->current_tab = $_GET['tab'];
			}

			if ( isset( $_GET['cr_gen_prod_feed'] ) && 'true' === $_GET['cr_gen_prod_feed'] ) {
				// WPML compatibility for XML feeds in different languages
				if ( isset( $_GET['cr_prod_feed_lang'] ) && $_GET['cr_prod_feed_lang'] ) {
					do_action( 'cr_generate_prod_feed_chunk', $_GET['cr_prod_feed_lang'] );
				} else {
					do_action( 'cr_generate_prod_feed_chunk', '' );
				}
			}
			if ( isset( $_GET['cr_gen_rev_feed'] ) && 'true' === $_GET['cr_gen_rev_feed'] ) {
				// WPML compatibility for XML feeds in different languages
				if ( isset( $_GET['cr_rev_feed_lang'] ) && $_GET['cr_rev_feed_lang'] ) {
					do_action( 'cr_generate_product_reviews_feed_chunk', $_GET['cr_rev_feed_lang'] );
				} else {
					do_action( 'cr_generate_product_reviews_feed_chunk', '' );
				}
			}

			add_action( 'admin_init', array( $this, 'save_settings' ) );
			add_action( 'admin_notices', array( $this, 'check_cron' ) );
			add_action( 'admin_menu', array( $this, 'register_settings_menu' ), 11 );
			add_action( 'admin_enqueue_scripts', array( $this, 'load_product_feed_css_js' ) );
		}

		public function check_cron() {
			if ( current_user_can( 'manage_options' ) && function_exists( 'get_current_screen' ) ) {
				$current_screen = get_current_screen();
				if ( false !== strpos( $current_screen->id, 'cr-reviews-product-feed' ) ) {
					// XML Product Feed
					$cron_options = get_option(
						'ivole_product_feed_cron',
						array( 'started' => false )
					);
					// WPML compatibility to display updates about creation of multiple XML feeds for different languages
					if (
						has_filter( 'wpml_active_languages' ) &&
						isset( $cron_options['langs'] ) &&
						is_array( $cron_options['langs'] )
					) {
						$cron_options_for_feeds = $cron_options['langs'];
					} else {
						$cron_options_for_feeds = array(
							'__' => $cron_options
						);
					}
					foreach ( $cron_options_for_feeds as $language => $options) {
						if ( $options['started'] ) {
							$prefix = '';
							$link = admin_url( 'admin.php?page=cr-reviews-product-feed&cr_gen_prod_feed=true' );
							if ( '__' !== $language ) {
								$prefix = '[' . $language . '] ';
								$link = admin_url( 'admin.php?page=cr-reviews-product-feed&cr_gen_prod_feed=true&cr_prod_feed_lang=' . $language );
							}
							$offset = ( $options['offset'] < $options['total'] ) ? $options['offset'] : $options['total'];
							echo '<div class="updated cr-admin-xml-update-info"><p>';
							echo esc_html(
								$prefix .
								sprintf(
									/* translators: please keep %1$s, %2$s, and %3$s in the translation - they will be replaced with the counts of products */
									__(
										'XML Product Feed for Google Shopping is being generated in background - products %1$s to %2$s out of %3$s.',
										'customer-reviews-woocommerce'
									),
									$options['current'],
									$offset,
									$options['total']
								)
							);
							echo '</p><a class="button button-small" href="' . esc_url( $link ) . '">' . esc_html__( 'Update progress', 'customer-reviews-woocommerce' ) . '</a>';
							echo '</div>';
						}
					}
					// XML Product Review Feed
					$review_cron_options = get_option(
						'ivole_product_reviews_feed_cron',
						array( 'started' => false )
					);
					// WPML compatibility to display updates about creation of multiple XML feeds for different languages
					if (
						has_filter( 'wpml_active_languages' ) &&
						isset( $review_cron_options['langs'] ) &&
						is_array( $review_cron_options['langs'] )
					) {
						$review_cron_options_for_feeds = $review_cron_options['langs'];
					} else {
						$review_cron_options_for_feeds = array(
							'__' => $review_cron_options
						);
					}
					foreach ( $review_cron_options_for_feeds as $language => $options) {
						if ( $options['started'] ) {
							$prefix = '';
							$link = admin_url( 'admin.php?page=cr-reviews-product-feed&cr_gen_rev_feed=true' );
							if ( '__' !== $language ) {
								$prefix = '[' . $language . '] ';
								$link = admin_url( 'admin.php?page=cr-reviews-product-feed&cr_gen_rev_feed=true&cr_rev_feed_lang=' . $language );
							}
							$offset = ( $options['offset'] < $options['total'] ) ? $options['offset'] : $options['total'];
							echo '<div class="updated cr-admin-xml-update-info"><p>';
							echo esc_html(
								$prefix .
								sprintf(
									/* translators: please keep %1$s, %2$s, and %3$s in the translation - they will be replaced with the counts of products */
									__(
										'XML Product Review Feed for Google Shopping is being generated in background - reviews %1$s to %2$s out of %3$s.',
										'customer-reviews-woocommerce'
									),
									$options['current'],
									$offset,
									$options['total']
								)
							);
							echo '</p><a class="button button-small" href="' . esc_url( $link ) . '">' . esc_html__( 'Update progress', 'customer-reviews-woocommerce' ) . '</a>';
							echo '</div>';
						}
					}
				}
				// Check that WP Cron is disabled
				if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) {
					if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] === 'cr-reviews-product-feed' ) {
						if( 'yes' === get_option( 'ivole_product_feed', 'no' ) || 'yes' === get_option( 'ivole_google_generate_xml_feed', 'no' ) ) {
							WC_Admin_Settings::add_message( __( 'XML Feeds might not be created correctly because WP Cron is disabled', 'customer-reviews-woocommerce' ) );
						}
					}
				}
			}
		}

		public function register_settings_menu() {
			add_submenu_page(
				'cr-reviews',
				__( 'Integration with Google Services', 'customer-reviews-woocommerce' ),
				__( 'Google', 'customer-reviews-woocommerce' ),
				'manage_options',
				$this->menu_slug,
				array( $this, 'display_productfeed_admin_page' )
			);
		}

		public function display_productfeed_admin_page() {
			?>
			<div class="wrap ivole-new-settings woocommerce">
				<h1 class="wp-heading-inline" style="margin-bottom:8px;"><?php echo esc_html( get_admin_page_title() ); ?></h1>
				<hr class="wp-header-end">
				<?php
				$tabs = apply_filters( 'cr_productfeed_tabs', array() );

				if ( is_array( $tabs ) && sizeof( $tabs ) > 1 ) {
					echo '<ul class="subsubsub">';

					$array_keys = array_keys( $tabs );
					$last = end( $array_keys );

					foreach ( $tabs as $tab => $label ) {
						echo '<li><a href="' . $this->page_url . '&tab=' . $tab . '" class="' . ( $this->current_tab === $tab ? 'current' : '' ) . '">' . $label . '</a> ' . ( $last === $tab ? '' : '|' ) . ' </li>';
					}

					echo '</ul><br class="clear" />';
				}
				?>
				<form action="" method="post" id="mainform" enctype="multipart/form-data">
					<?php
					WC_Admin_Settings::show_messages();

					do_action( 'cr_productfeed_display_' . $this->current_tab );
					?>
					<p class="submit">
						<?php if ( empty( $GLOBALS['hide_save_button'] ) ) : ?>
							<button name="save" class="button-primary woocommerce-save-button" type="submit" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>"><?php esc_html_e( 'Save changes', 'woocommerce' ); ?></button>
						<?php endif; ?>
						<?php wp_nonce_field( 'cr-productfeed' ); ?>
					</p>
				</div>
			</form>
			<?php
			update_option( 'ivole_activation_notice', 0 );
		}

		public function save_settings() {
			if ( $this->is_this_page() && ! empty( $_POST ) ) {
				check_admin_referer( 'cr-productfeed' );

				do_action( 'cr_save_productfeed_' . $this->current_tab );

				WC_Admin_Settings::add_message( __( 'Your settings have been saved.', 'woocommerce' ) );
			}
		}

		public function is_this_page() {
			return ( isset( $_GET['page'] ) && $_GET['page'] === $this->menu_slug );
		}

		public function get_current_tab() {
			return $this->current_tab;
		}

		public function load_product_feed_css_js( $hook ) {
			$reviews_screen_id = sanitize_title( __( 'Reviews', 'customer-reviews-woocommerce' ) . Ivole_Reviews_Admin_Menu::$screen_id_bubble );
			if( $reviews_screen_id . '_page_cr-reviews-product-feed' === $hook ) {
				wp_enqueue_style( 'cr-admin-css', plugins_url('css/admin.css', dirname( dirname( __FILE__ ) ) ), array(), Ivole::CR_VERSION );
				wp_enqueue_style( 'cr_select2_admin_css', plugins_url('css/select2.min.css', dirname( dirname( __FILE__ ) ) ) );
				wp_enqueue_script( 'cr_select2_admin_js', plugins_url('js/select2.min.js', dirname( dirname( __FILE__ ) ) ) );
				wp_register_script( 'ivole-admin-categories', plugins_url('js/admin-categories.js', dirname( dirname( __FILE__ ) ) ), array( 'jquery' ), false, false );

				wp_localize_script('ivole-admin-categories', 'CrProductFeedStrings', array(
					'select_category' => __( 'Select a category', 'customer-reviews-woocommerce' ),
					'select_field' => __( 'Select a field', 'customer-reviews-woocommerce' ),
				));

				wp_enqueue_script( 'ivole-admin-categories' );
			}
		}
	}

endif;
