<?php

namespace Prodigy\Includes\Frontend\Shortcodes;

use Prodigy\Includes\Content\Prodigy_Request_Maker;
use Prodigy\Includes\Frontend\Cart_Error_Handler;
use Prodigy\Includes\Helpers\Prodigy_Cookies;
use Prodigy\includes\helpers\Prodigy_Elementor;
use Prodigy\includes\models\Prodigy_Order;
use Prodigy\Includes\Prodigy_Cart;
use Prodigy\Includes\Prodigy_User;
use Prodigy\Includes\Support\Addons\Elementor\Widgets\Prodigy_My_Account;

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

/**
 * Prodigy my account shortcode class
 *
 * @version    2.8.3
 * @package    Prodigy
 * @subpackage Prodigy/includes
 */
class Prodigy_Short_Code_My_Account {

	/**
	 * Shortcode name
	 *
	 * @var string
	 */
	public const NAME = 'prodigy_my_account';

	public const DEFAULT_TITLE = 'Account';

	/**
	 * Display type
	 *
	 * @var string
	 */
	public const DISPLAY_DROPDOWN = 'dropdown';

	/** @var Prodigy_User */
	public Prodigy_User $user;

	/**
	 * Set hooks
	 *
	 * @throws \Exception
	 */
	public function __construct() {
		$this->user = new Prodigy_User();
		add_shortcode( self::NAME, array( $this, 'output' ) );

		add_action( 'wp_ajax_prodigy-user-logout', array( $this, 'launch_logout' ) );
		add_action( 'wp_ajax_nopriv_prodigy-user-logout', array( $this, 'launch_logout' ) );

		add_action( 'wp_ajax_prodigy-user-login', array( $this, 'user_login' ) );
		add_action( 'wp_ajax_nopriv_prodigy-user-login', array( $this, 'user_login' ) );

		add_action( 'wp_ajax_prodigy-render-my-account-widget', array( $this, 'prodigy_render_my_account_widget' ) );
		add_action( 'wp_ajax_nopriv_prodigy-render-my-account-widget', array( $this, 'prodigy_render_my_account_widget' ) );
	}

	/**
	 * @return void
	 * @throws \Exception
	 */
	public function prodigy_render_my_account_widget(): void {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'store-nonce' ) ) {
			return;
		}

		$atts = array();
		if ( isset( $_POST['parameters'] ) && is_array( $_POST['parameters'] ) ) {
			$atts = wp_unslash( $_POST['parameters'] );
			// Prevent LFI: never allow template/path overrides from request (WordPress.org security report).
			$disallowed = array( 'template_name', 'template_path', 'template', 'template_part' );
			foreach ( $disallowed as $key ) {
				unset( $atts[ $key ] );
			}
		}

		if ( Prodigy_Elementor::is_elementor_enabled() && ! empty( $atts ) ) {
			$widget = new Prodigy_My_Account();
			$atts   = $widget->set_widget_options( $atts );
			$html   = $this->get_partial_main_html( $atts );
			wp_send_json_success( array( 'html' => $html ) );

			return;
		}

		$attr = $this->set_widget_parameters( $atts );
		$html = $this->get_partial_main_html( $attr );

		wp_send_json_success( array( 'html' => $html ) );
	}

	/**
	 * Get rendered HTML for my account widget
	 *
	 * @param   array $atts
	 *
	 * @return string
	 */
	private function get_partial_main_html( array $atts ): string {
		ob_start();
		do_action( 'prodigy_shortcode_template_my_account_main_content', $atts );
		wp_reset_postdata();

		return ob_get_clean();
	}

	/**
	 * @param   array $atts
	 *
	 * @return array
	 * @throws \Exception
	 */
	public function set_widget_parameters( array $atts ): array {
		$atts['is_dropdown'] = false;
		if ( isset( $atts['display'] ) && $atts['display'] === self::DISPLAY_DROPDOWN ) {
			$atts['is_dropdown']     = true;
			$atts['container_class'] = 'prodigy-dropdown-account__wrap';
		}

		$atts['heading_text'] = $atts['title'] ?? self::DEFAULT_TITLE;

		return $atts;
	}

	/**
	 * @throws \Exception
	 */
	public function user_login() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ), 'store-nonce' ) ) {
			return;
		}

		$options['login_url'] = Prodigy_User::get_hosted_system_login_url();
		wp_send_json_success( $options );
	}

	/**
	 * @return void
	 * @throws \Exception
	 */
	public function launch_logout() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['nonce'] ?? '' ), 'store-nonce' ) ) {
			return;
		}
		$refresh_key = Prodigy_Cookies::get_cookie( Prodigy_User::REFRESH_COOKIE_NAME );
		$this->user->remove_impersonation_user( $refresh_key );
		$this->user->hosted_system_logout_process( $refresh_key );
		$this->user->logout();
		$this->user->delete_user_cookie();
		$this->user->clear_external_checkout_cookie();

		wp_send_json_success( array( 'success' => true ) );
	}

	/**
	 * @param   array       $atts
	 * @param   string|null $content
	 *
	 * @return string
	 * @throws \Exception
	 */
	public function output( $atts, ?string $content = null ): string {
		$attr = $this->set_widget_parameters( (array) $atts );
		$this->render_view( $attr );
		$content = ob_get_clean();

		return $content;
	}

	/**
	 * @param   array $atts
	 *
	 * @return void
	 */
	public function render_view( array $atts ): void {
		ob_start();
		do_action( 'prodigy_shortcode_template_my_account', $atts );
		wp_reset_postdata();
	}
}
