<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class QVIPP_Backend { 

	public $qvipp_plugin_dir_url;
	public $qvipp_options;
	public $qvipp_styles;
	public $qvipp_plugin_basename;

	public function __construct( $qvipp_plugin_dir_url, $qvipp_plugin_basename ) {
		$this->qvipp_plugin_dir_url = esc_url( $qvipp_plugin_dir_url );
		$this->qvipp_plugin_basename = $qvipp_plugin_basename;
		add_action( 'admin_menu', array( $this, 'qvipp_admin_menu' ) );
		add_filter(
            'plugin_action_links_' . $this->qvipp_plugin_basename,
            array( $this, 'qvipp_plugin_settings_link' )
        );
        add_action( 'admin_enqueue_scripts', array( $this, 'qvipp_enqueue_admin_assets' ) );
	}

	/**
	 * Add Settings link on Plugins page.
	 */
	public function qvipp_plugin_settings_link( $links ) {
        $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=quickview-instant-product-preview' ) ) . '">' . esc_html__( 'Settings', 'quickview-instant-product-preview' ) . '</a>';
        array_unshift( $links, $settings_link );
        return $links;
    }

	/**
	 * Add admin menu.
	 */
	public function qvipp_admin_menu() {
		add_menu_page(
			__( 'Quick View Options', 'quickview-instant-product-preview' ),
			__( 'QuickView – Instant Product Preview', 'quickview-instant-product-preview' ),
			'manage_options',
			'quickview-instant-product-preview',
			array( $this, 'qvipp_quick_view_options' ),
			'dashicons-visibility'
		);
	}

	public function qvipp_enqueue_admin_assets( $hook ) {

	    // WordPress turns this into a hook suffix like: 'toplevel_page_quickview-instant-product-preview'
	    if ( $hook !== 'toplevel_page_quickview-instant-product-preview' ) {
	        return; // bail if not our settings page
	    }

	    // enqueue styles/scripts only here
	    wp_enqueue_style( 'wp-color-picker' );
	    wp_enqueue_style(
	        'qvipp_admin_style',
	        $this->qvipp_plugin_dir_url . 'assets/css/admin.css',
	        array(),
	        '1.0'
	    );
	    wp_enqueue_script(
	        'qvipp_admin_js',
	        $this->qvipp_plugin_dir_url . 'assets/js/admin.js',
	        array( 'jquery', 'wp-color-picker' ),
	        '1.0',
	        true
	    );
	}

	/**
	 * Render admin page.
	 */
	public function qvipp_quick_view_options() {

		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'quickview-instant-product-preview' ) );
		}

		// Save settings.
		if ( 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['qvipp_nonce_field'] ) && check_admin_referer( 'qvipp-admin-nonce', 'qvipp_nonce_field' ) ) {

			// Sanitize array options.
			if ( isset( $_POST['qvipp_display_product_options'] ) && is_array( $_POST['qvipp_display_product_options'] ) ) {
			    $selected = array_values( $_POST['qvipp_display_product_options'] ); // reindex
			    $new = array();
			    $i = 1;
			    foreach ( $selected as $val ) {
			        $new[ $i ] = sanitize_text_field( $val );
			        $i++;
			    }
			    update_option( 'qvipp_display_product_options', $new );
			} else {
			    update_option( 'qvipp_display_product_options', array() );
			}

			// General options.
			$data = array(
				'qvipp_enable_quick_view'        => isset( $_POST['qvipp_enable_quick_view'] ) ? '1' : '0',
				'qvipp_enable_mobile'            => isset( $_POST['qvipp_enable_mobile'] ) ? '1' : '0',
				'qvipp_button_lable'             => isset( $_POST['qvipp_button_lable'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_button_lable'] ) ) : '',
				'qvipp_quick_view_btn_position'  => isset( $_POST['qvipp_quick_view_btn_position'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_quick_view_btn_position'] ) ) : 'after_image',
				'qvipp_view_product_detail'      => isset( $_POST['qvipp_view_product_detail'] ) ? '1' : '0',
				'qvipp_view_product_detail_text' => isset( $_POST['qvipp_view_product_detail_text'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_view_product_detail_text'] ) ) : '',
			);

			update_option( 'qvipp_options', $data );

			// Style options.
			$style = array(
				'qvipp_modal_bg'                   => isset( $_POST['qvipp_modal_bg'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_modal_bg'] ) ) : '',
				'qvipp_close_btn'                  => isset( $_POST['qvipp_close_btn'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_close_btn'] ) ) : '',
				'qvipp_close_btn_bg'               => isset( $_POST['qvipp_close_btn_bg'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_close_btn_bg'] ) ) : '',
				'qvipp_navigation_bg'              => isset( $_POST['qvipp_navigation_bg'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_navigation_bg'] ) ) : '',
				'qvipp_navigation_txt'             => isset( $_POST['qvipp_navigation_txt'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_navigation_txt'] ) ) : '',
				'qvipp_product_detail_btn_color'   => isset( $_POST['qvipp_product_detail_btn_color'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_product_detail_btn_color'] ) ) : '',
				'qvipp_quick_view_btn_lable_color' => isset( $_POST['qvipp_quick_view_btn_lable_color'] ) ? sanitize_text_field( wp_unslash( $_POST['qvipp_quick_view_btn_lable_color'] ) ) : '',
			);
			update_option( 'qvipp_styles', $style );
		}

		$this->qvipp_options = get_option( 'qvipp_options', array() );
		$this->qvipp_styles  = get_option( 'qvipp_styles', array() );

		$qvipp_admn_nonce = wp_create_nonce( 'qvipp-admin-nonce' );

		// Load display_product_options array.
		$op_val = get_option( 'qvipp_display_product_options', array() );
		if ( ! is_array( $op_val ) ) {
			$op_val = array();
		}
		?>
		<div class="wrap qvipp-admin-page">
			<div class="qvipp-page-header">
				<h1><?php esc_html_e( 'QuickView – Instant Product Preview Settings', 'quickview-instant-product-preview' ); ?><span class="qvipp-version"><?php echo esc_html( 'v' . QVIPP_VERSION ); ?></span></h1>
				<p class="qvipp-page-subtitle"><?php esc_html_e( 'Configure how the quick view button behaves and looks in the admin area.', 'quickview-instant-product-preview' ); ?></p>
			</div>
			<form action="" method="post">
				<?php wp_nonce_field( 'qvipp-admin-nonce', 'qvipp_nonce_field' ); ?>

				<div class="qvipp-card">
					<h2><?php esc_html_e( 'General Options', 'quickview-instant-product-preview' ); ?></h2>
					<table class="form-table qvipp-form-table">
					<tr>
						<th><?php esc_html_e( 'Enable Quick View', 'quickview-instant-product-preview' ); ?></th>
						<td>
						  <label class="qvipp-switch">
						    <input type="checkbox"
						           name="qvipp_enable_quick_view"
						           value="1"
						           <?php checked( $this->qvipp_options['qvipp_enable_quick_view'] ?? '', '1' ); ?> />
						    <span class="qvipp-slider"></span>
						  </label>
						  <p class="qvipp-field-note"><?php esc_html_e( 'Turn the quick view feature on or off.', 'quickview-instant-product-preview' ); ?></p>
						</td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Button Position', 'quickview-instant-product-preview' ); ?></th>
						<td>
							<div class="qvipp-radio-group">
								<?php
								$positions = array(
									'after_image'        => __( 'After Image', 'quickview-instant-product-preview' ),
									'on_hover_image'     => __( 'On Hover Image', 'quickview-instant-product-preview' ),
									'after_title'        => __( 'After Title', 'quickview-instant-product-preview' ),
									'before_add_to_cart' => __( 'Before Add to Cart', 'quickview-instant-product-preview' ),
								);
								$button_pos = $this->qvipp_options['qvipp_quick_view_btn_position'] ?? '';
								foreach ( $positions as $val => $label ) {
									echo '<input type="radio" id="qvipp_pos_' . esc_attr( $val ) . '" name="qvipp_quick_view_btn_position" value="' . esc_attr( $val ) . '" ' . checked( $button_pos, $val, false ) . ' />';
									echo '<label for="qvipp_pos_' . esc_attr( $val ) . '">' . esc_html( $label ) . '</label>';
								}
								?>
							</div>
							<p class="qvipp-field-note"><?php esc_html_e( 'Select where the quick view button appears on product cards.', 'quickview-instant-product-preview' ); ?></p>
						</td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Enable Quick View on mobile', 'quickview-instant-product-preview' ); ?></th>
						<td>
							<label class="qvipp-switch">
								<input name="qvipp_enable_mobile"
								       type="checkbox"
								       value="1"
								       <?php checked( $this->qvipp_options['qvipp_enable_mobile'] ?? '', '1' ); ?> />
								<span class="qvipp-slider"></span>
							</label>
							<p class="qvipp-field-note"><?php esc_html_e( 'Allow quick view on smaller screens.', 'quickview-instant-product-preview' ); ?></p>
						</td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Quick View Button Label', 'quickview-instant-product-preview' ); ?></th>
						<td>
							<input class="regular-text" name="qvipp_button_lable" type="text" value="<?php echo esc_attr( $this->qvipp_options['qvipp_button_lable'] ?? '' ); ?>" />
						</td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Quick View Button Label Color', 'quickview-instant-product-preview' ); ?></th>
						<td>
							<input type="text" name="qvipp_quick_view_btn_lable_color" value="<?php echo esc_attr( $this->qvipp_styles['qvipp_quick_view_btn_lable_color'] ?? '' ); ?>" class="qvipp-color-picker" data-default-color="#95979c" />
						</td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'View Product Detail Button', 'quickview-instant-product-preview' ); ?></th>
						<td>
							<label class="qvipp-switch">
								<input name="qvipp_view_product_detail"
								       type="checkbox"
								       value="1"
								       <?php checked( $this->qvipp_options['qvipp_view_product_detail'] ?? '', '1' ); ?> />
								<span class="qvipp-slider"></span>
							</label>
							<p class="qvipp-field-note"><?php esc_html_e( 'Show a button to visit the full product page.', 'quickview-instant-product-preview' ); ?></p>
						</td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'View Product Detail Text', 'quickview-instant-product-preview' ); ?></th>
						<td>
							<input class="regular-text" name="qvipp_view_product_detail_text" type="text" value="<?php echo esc_attr( $this->qvipp_options['qvipp_view_product_detail_text'] ?? '' ); ?>" />
						</td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Product Detail Button Color', 'quickview-instant-product-preview' ); ?></th>
						<td>
							<input type="text" name="qvipp_product_detail_btn_color" value="<?php echo esc_attr( $this->qvipp_styles['qvipp_product_detail_btn_color'] ?? '' ); ?>" class="qvipp-color-picker" data-default-color="#95979c" />
						</td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Select Options to Show', 'quickview-instant-product-preview' ); ?></th>
						<td>
							<?php
							$checkboxes = array(
								'product_title'           => __( 'Show Product Title', 'quickview-instant-product-preview' ),
								'product_image'           => __( 'Show Product Images', 'quickview-instant-product-preview' ),
								'product_price'           => __( 'Show Product Price', 'quickview-instant-product-preview' ),
								'product_excerpt'         => __( 'Show Product Excerpt', 'quickview-instant-product-preview' ),
								'product_add_to_cart_btn' => __( 'Show Add to Cart Button', 'quickview-instant-product-preview' ),
								'product_meta'            => __( 'Show Product Meta', 'quickview-instant-product-preview' ),
								'product_rating'          => __( 'Show Product Rating', 'quickview-instant-product-preview' ),
							);
							foreach ( $checkboxes as $val => $label ) {
								echo '<div class="qvipp-inline-switch">';
								echo '<label class="qvipp-switch">
										<input type="checkbox" name="qvipp_display_product_options[]" value="' . esc_attr( $val ) . '" ' . checked( in_array( $val, $op_val, true ), true, false ) . ' />
										<span class="qvipp-slider"></span>
									  </label>';
								echo '<span>' . esc_html( $label ) . '</span>';
								echo '</div>';
							}
							?>
						</td>
					</tr>
					</table>
				</div>
				
				<div class="qvipp-card">
					<h2><?php esc_html_e( 'Style Options', 'quickview-instant-product-preview' ); ?></h2>
					<table class="form-table qvipp-form-table">
					<tr>
						<th><?php esc_html_e( 'Modal Window Background Color', 'quickview-instant-product-preview' ); ?></th>
						<td><input type="text" name="qvipp_modal_bg" value="<?php echo esc_attr( $this->qvipp_styles['qvipp_modal_bg'] ?? '' ); ?>" class="qvipp-color-picker" data-default-color="#fff" /></td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Closing Button Color', 'quickview-instant-product-preview' ); ?></th>
						<td><input type="text" name="qvipp_close_btn" value="<?php echo esc_attr( $this->qvipp_styles['qvipp_close_btn'] ?? '' ); ?>" class="qvipp-color-picker" data-default-color="#95979c" /></td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Closing Button Hover Background Color', 'quickview-instant-product-preview' ); ?></th>
						<td><input type="text" name="qvipp_close_btn_bg" value="<?php echo esc_attr( $this->qvipp_styles['qvipp_close_btn_bg'] ?? '' ); ?>" class="qvipp-color-picker" data-default-color="#4C6298" /></td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Navigation Box Background Color', 'quickview-instant-product-preview' ); ?></th>
						<td><input type="text" name="qvipp_navigation_bg" value="<?php echo esc_attr( $this->qvipp_styles['qvipp_navigation_bg'] ?? '' ); ?>" class="qvipp-color-picker" data-default-color="rgba(255,255,255,0.2)" /></td>
					</tr>
					<tr>
						<th><?php esc_html_e( 'Navigation Box Text Color', 'quickview-instant-product-preview' ); ?></th>
						<td><input type="text" name="qvipp_navigation_txt" value="<?php echo esc_attr( $this->qvipp_styles['qvipp_navigation_txt'] ?? '' ); ?>" class="qvipp-color-picker" data-default-color="#fff" /></td>
					</tr>
					</table>
				</div>

				<?php submit_button( __( 'Save Changes', 'quickview-instant-product-preview' ) ); ?>
			</form>
		</div>
		<?php
	}

	/**
	 * Simple mobile detection.
	 */
	public function qvipp_mobile_detect() {
		$useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) : '';
		return (bool) preg_match( '/(android|iphone|ipod|iemobile|blackberry|kindle|mobile)/i', $useragent );
	}
}
