<?php

namespace IGD_PRO;

use IGD\App;

defined( 'ABSPATH' ) || exit;

class Shortcode {

	private static $instance = null;

	private $data;
	private $current_user;
	private $type;

	public function __construct() {
		add_filter( 'igd_shortcode_data', [ $this, 'process_shortcode_data' ], 10, 2 );
		add_filter( 'igd_shortcode_access_screen', [ $this, 'get_shortcode_access_screen' ], 10, 4 );
		add_action( 'igd_shortcode_scripts', [ $this, 'enqueue_shortcode_scripts' ] );
	}

	/**
	 * Get shortcode access screen based on restrictions.
	 *
	 * @param $screen
	 * @param $data
	 * @param $current_user
	 *
	 * @return false|mixed|string
	 */
	public function get_shortcode_access_screen( $screen, $data, $current_user, $has_permission ) {

		$this->data         = $data;
		$this->current_user = $current_user;
		$this->type         = ! empty( $data['type'] ) ? $data['type'] : '';

		// Check password protection
		if ( Restrictions::instance()->should_password_protected( $data ) ) {
			return $this->get_password_protected_screen();
		}

		// Check email requirement for review and gallery modules
		if ( $this->requires_email_verification() ) {
			$user_email = $this->get_user_email();

			if ( empty( $user_email ) ) {
				$screen = $this->get_email_required_screen();
			}

			$selection = $this->fetch_user_selection( $user_email );
			if ( ! empty( $selection ) ) {

				add_filter( 'igd_shortcode_data', function ( $data ) use ( $selection ) {
					$data['selection'] = $selection;

					return $data;
				} );

			}

			return $screen;
		}

		// Display login screen for non-logged-in users
		if ( ! $has_permission && $this->requires_login() ) {
			return $this->get_login_screen();
		}

		return $screen;
	}

	public function get_login_screen() {
		ob_start();
		include IGD_PRO_INCLUDES . '/views/shortcode/login.php';

		return ob_get_clean();
	}

	private function get_password_protected_screen() {
		ob_start();
		include_once IGD_PRO_INCLUDES . '/views/shortcode/password-protected.php';

		return ob_get_clean();
	}

	private function get_email_required_screen() {
		ob_start();
		include_once IGD_PRO_INCLUDES . '/views/shortcode/email-required.php';

		return ob_get_clean();

	}

	/**
	 * Check if module requires email verification.
	 *
	 * @return bool True if email verification is required.
	 */
	private function requires_email_verification() {
		if ( 'review' === $this->type ) {
			return true;
		}

		if ( 'gallery' === $this->type && ! empty( $this->data['photoProof'] ) ) {
			return true;
		}

		return false;
	}

	private function requires_login() {


		return ! $this->current_user && ( ! isset( $this->data['displayLogin'] ) || ! empty( $this->data['displayLogin'] ) );
	}

	private function get_user_email() {
		if ( $this->current_user ) {
			return $this->current_user->user_email;
		}

		if ( ! empty( $_COOKIE['igd_user_email'] ) ) {
			return sanitize_email( $_COOKIE['igd_user_email'] );
		}

		return null;
	}

	private function fetch_user_selection( $user_email ) {
		// Fetch the selection based on the selection ID if provided
		if ( ! empty( $_GET['selection_id'] ) ) {
			$selections = Proofing::instance()->get_selections(
				[
					'id'           => intval( $_GET['selection_id'] ),
					'shortcode_id' => $this->data['id'],
				]
			);
		} else {
			// Fetch the selection based on the user email
			$selections = Proofing::instance()->get_selections(
				[
					'email'        => $user_email,
					'shortcode_id' => $this->data['id'],
				]
			);
		}

		return ! empty( $selections ) ? reset( $selections ) : null;
	}

	public function enqueue_shortcode_scripts( $data ) {

		$type = $data['type'];

		if ( 'media' == $type ) {
			wp_enqueue_style( 'igd-plyr' );
			wp_enqueue_script( 'igd-plyr' );
		} elseif ( 'slider' == $type ) {
			wp_enqueue_style( 'igd-slick' );
			wp_enqueue_script( 'igd-slick' );
		} elseif ( in_array( $type, [ 'browser', 'gallery', 'review' ] ) ) {

			$can_delete   = ! empty( $data['canDelete'] );
			$can_move     = ! empty( $data['moveCopy'] );
			$can_download = ! empty( $data['download'] ) && ! empty( $data['zipDownload'] );

			// Enqueue Drag-Select
			if ( 'review' == $type || $can_delete || $can_move || $can_download ) {
				wp_enqueue_script( 'igd-drag-select' );
			}
		}

		wp_enqueue_script( 'igd-frontend-pro' );

	}

	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

}

Shortcode::instance();