<?php
/**
 * WooCommerce My Account integration.
 * Handles overriding WooCommerce login forms with WAWP OTP forms.
 *
 * @package automation-web-platform
 */

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

add_action( 'init', 'wawp_override_all_woocommerce_logins' );

/**
 * Override all WooCommerce login forms if enabled.
 */
function wawp_override_all_woocommerce_logins() {
	// Only run if WooCommerce is active.
	if ( ! class_exists( 'WooCommerce' ) ) {
		return;
	}

	/**
	 * A) Override the WC templates (myaccount/form-login.php, checkout/form-login.php)
	 */
	if ( WAWP::instance()->get_wawp_setting( WAWP_Settings_Registry::REPLACE_WC_FORMS ) ) {
		add_filter(
			'woocommerce_locate_template',
			function ( $template, $template_name ) {
				// List of template files to override.
				$templates_to_override = array(
					'myaccount/form-login.php',
					'checkout/form-login.php',
					'global/form-login.php',
				);

				if ( in_array( $template_name, $templates_to_override, true ) ) {
					// The templates are located in the plugin's /templates folder.
					// Note: plugin_dir_path(__FILE__) points to integrations folder, so we go up one level.
					$custom_template = plugin_dir_path( __DIR__ ) . 'templates/' . $template_name;

					if ( file_exists( $custom_template ) ) {
						return $custom_template;
					}
				}

				return $template;
			},
			999,
			3
		);
	}

	/**
	 * B) Replace calls to woocommerce_login_form() with our shortcode
	 */
	if ( WAWP::instance()->get_wawp_setting( WAWP_Settings_Registry::REPLACE_WC_FORMS ) ) {
		remove_action( 'woocommerce_login_form', 'woocommerce_login_form', 10 );

		add_action(
			'woocommerce_login_form',
			function () {
				echo do_shortcode( '[wawp-fast-login]' );
			}
		);
	}

	/**
	 * C) WoodMart sidebar & header dropdown:
	 *    Remove WoodMart's sidebar login action and replace with our own.
	 *    Also filter the header dropdown HTML to swap in our form.
	 */
	if ( WAWP::instance()->get_wawp_setting( WAWP_Settings_Registry::REPLACE_WC_FORMS ) ) {
		add_action(
			'wp',
			function () {
				if ( is_user_logged_in() ) {
					return;
				}

				// 1. Replace WoodMart sidebar login form.
				if ( function_exists( 'woodmart_sidebar_login_form' ) ) {
					remove_action( 'woodmart_before_wp_footer', 'woodmart_sidebar_login_form', 160 );
					add_action(
						'woodmart_before_wp_footer',
						function () {
							if ( is_user_logged_in() || is_account_page() ) {
								return;
							}

							$settings   = function_exists( 'whb_get_settings' ) ? whb_get_settings() : array();
							$login_side = isset( $settings['account'] ) && ! empty( $settings['account']['login_dropdown'] ) && 'side' === $settings['account']['form_display'];

							if ( ! $login_side ) {
								return;
							}

							$position        = is_rtl() ? 'left' : 'right';
							$wrapper_classes = ' wd-' . $position;
							if ( function_exists( 'whb_get_dropdowns_color' ) && 'light' === whb_get_dropdowns_color() ) {
								$wrapper_classes .= ' color-scheme-light';
							}

							if ( function_exists( 'woodmart_enqueue_inline_style' ) ) {
								woodmart_enqueue_inline_style( 'header-my-account-sidebar' );
							}
							?>
							<div class="login-form-side wd-side-hidden woocommerce<?php echo esc_attr( $wrapper_classes ); ?>" role="complementary" aria-label="<?php esc_attr_e( 'Login sidebar', 'woodmart' ); ?>">
								<div class="wd-heading">
									<span class="title"><?php esc_html_e( 'Sign in', 'woodmart' ); ?></span>
									<div class="close-side-widget wd-action-btn wd-style-text wd-cross-icon">
										<a href="#" rel="nofollow">
											<span class="wd-action-icon"></span>
											<span class="wd-action-text"><?php esc_html_e( 'Close', 'woodmart' ); ?></span>
										</a>
									</div>
								</div>
								<div class="wawp-woodmart-sidebar-form">
									<?php echo do_shortcode( '[wawp-fast-login]' ); ?>
								</div>
							</div>
							<?php
						},
						160
					);
				}

				// 2. Replace WoodMart header dropdown login form.
				add_filter(
					'woodmart_get_header_links',
					function ( $links ) {
						if ( ! isset( $links['register']['dropdown'] ) ) {
							return $links;
						}

						$account_link = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) );

						ob_start();
						?>
						<div class="wd-dropdown wd-dropdown-register">
							<div class="login-dropdown-inner woocommerce">
								<span class="wd-heading">
									<span class="title"><?php esc_html_e( 'Sign in', 'woodmart' ); ?></span>
									<a class="create-account-link" href="<?php echo esc_url( add_query_arg( 'action', 'register', $account_link ) ); ?>">
										<?php esc_html_e( 'Create an Account', 'woodmart' ); ?>
									</a>
								</span>
								<?php echo do_shortcode( '[wawp-fast-login]' ); ?>
							</div>
						</div>
						<?php
						$links['register']['dropdown'] = ob_get_clean();

						return $links;
					}
				);
			}
		);
	}
}
