<?php
/**
 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
 *
 * This source code is licensed under the license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @package MetaCommerce
 */

namespace WooCommerce\Facebook\Admin;

use WooCommerce\Facebook\Framework\Logger;
use WooCommerce\Facebook\Framework\Helper;

defined( 'ABSPATH' ) || exit;

/**
 * Admin WhatsApp Integration Settings handler.
 *
 * @since 3.5.0
 */
class WhatsApp_Integration_Settings {

	/** @var string */
	const PAGE_ID = 'wc-whatsapp';


	/** @var \WC_Facebookcommerce */
	private $plugin;


	/**
	 * WhatsApp Integration Settings constructor.
	 *
	 * @since 3.5.0
	 *
	 * @param \WC_Facebookcommerce $plugin is the plugin instance of WC_Facebookcommerce
	 */
	public function __construct( \WC_Facebookcommerce $plugin ) {
		$this->plugin = $plugin;
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'admin_menu', array( $this, 'add_menu_item' ) );
		add_action( 'admin_footer', array( $this, 'render_message_handler' ) );
		add_action( 'admin_head', array( $this, 'print_menu_icon_styles' ) );
	}


	/**
	 * Renders the message handler script in the footer.
	 *
	 * @since 3.5.0
	 */
	public function render_message_handler() {
		if ( ! $this->is_whatsapp_admin_page() ) {
			return;
		}

		wp_add_inline_script( 'plugin-api-client', $this->generate_inline_enhanced_onboarding_script(), 'after' );
	}

	/**
	 * Enqueues the assets.
	 *
	 * @since 3.5.0
	 *
	 * @internal
	 */
	public function enqueue_assets() {

		if ( ! $this->is_whatsapp_admin_page() ) {
			return;
		}

		wp_enqueue_style( 'wc-facebook-admin-whatsapp-enhanced', facebook_for_woocommerce()->get_plugin_url() . '/assets/css/admin/facebook-for-woocommerce-whatsapp-enhanced.css', array(), \WC_Facebookcommerce::VERSION );
	}


	/**
	 * Adds the WhatsApp menu item.
	 *
	 * Registers WhatsApp as its own top-level item in the WP Admin sidebar
	 * (a standalone entry, not nested under Marketing or WooCommerce). For a
	 * top-level menu the wc-admin screen id is 'toplevel_page_{slug}', so the
	 * add_menu_page() slug and connect_to_enhanced_admin() screen id are a
	 * matched pair. The position (58.9) places it just below the Marketing
	 * menu — tune as desired.
	 *
	 * @since 3.5.0
	 */
	public function add_menu_item() {

		add_menu_page(
			__( 'WhatsApp for WooCommerce', 'facebook-for-woocommerce' ),
			__( 'WhatsApp', 'facebook-for-woocommerce' ),
			'manage_woocommerce',
			self::PAGE_ID,
			[ $this, 'render' ],
			facebook_for_woocommerce()->get_plugin_url() . '/assets/images/whatsapp-menu-icon.svg',
			58.9
		);

		$this->connect_to_enhanced_admin( 'toplevel_page_wc-whatsapp' );
	}

	/**
	 * Prints CSS so the top-level WhatsApp menu icon matches the native admin menu icons.
	 *
	 * WordPress only recolors Dashicon (font) menu icons per state and color
	 * scheme; an icon passed as an image URL is merely dimmed with opacity and
	 * looks inconsistent. Rendering the icon as a CSS mask filled with
	 * `currentColor` makes it inherit the exact color WordPress applies to
	 * native icons in every state (idle grey, hover blue, current white).
	 *
	 * @since 3.5.0
	 */
	public function print_menu_icon_styles() {
		$icon_url = facebook_for_woocommerce()->get_plugin_url() . '/assets/images/whatsapp-menu-icon.svg';
		$selector = '#adminmenu #toplevel_page_' . self::PAGE_ID . ' .wp-menu-image';
		?>
		<style id="wc-whatsapp-menu-icon">
			<?php echo esc_html( $selector ); ?> img { display: none; }
			<?php echo esc_html( $selector ); ?>::before {
				content: "";
				display: block;
				width: 22px;
				height: 22px;
				margin: 0 auto;
				background-color: currentColor;
				-webkit-mask: url( "<?php echo esc_url( $icon_url ); ?>" ) no-repeat center;
				mask: url( "<?php echo esc_url( $icon_url ); ?>" ) no-repeat center;
				-webkit-mask-size: 22px 22px;
				mask-size: 22px 22px;
			}
		</style>
		<?php
	}

	/**
	 * Enables admin support for the main WhatsApp settings page.
	 *
	 * @since 3.5.0
	 *
	 * @param string $screen_id
	 */
	private function connect_to_enhanced_admin( $screen_id ) {
		if ( is_callable( 'wc_admin_connect_page' ) ) {
			wc_admin_connect_page(
				array(
					'id'        => self::PAGE_ID,
					'screen_id' => $screen_id,
					'path'      => add_query_arg( 'page', self::PAGE_ID, 'admin.php' ),
					'title'     => [ __( 'WhatsApp for WooCommerce', 'facebook-for-woocommerce' ) ],
				)
			);
		}
	}

	/**
	 * Checks if marketing feature is enabled in woocommerce.
	 *
	 * @since 3.5.0
	 *
	 * @return bool
	 */
	public function is_marketing_enabled() {
		if ( class_exists( WooAdminFeatures::class ) ) {
			return WooAdminFeatures::is_enabled( 'marketing' );
		}

		return is_callable( '\Automattic\WooCommerce\Admin\Features\Features::is_enabled' )
				&& \Automattic\WooCommerce\Admin\Features\Features::is_enabled( 'marketing' );
	}

	/**
	 * Gets the root menu item.
	 *
	 * @since 3.5.0
	 *
	 * @return string
	 */
	public function root_menu_item() {
		if ( $this->is_marketing_enabled() ) {
			return 'woocommerce-marketing';
		}

		return 'woocommerce';
	}

	/**
	 * Checks if the page is WhatsApp admin page.
	 *
	 * @since 3.5.0
	 *
	 * @return string
	 */
	private function is_whatsapp_admin_page() {
		return is_admin() && self::PAGE_ID === Helper::get_requested_value( 'page' );
	}

	/**
	 * Renders the whatsapp utility settings page.
	 *
	 * @since 3.5.0
	 */
	public function render() {
		$whatsapp_connection = $this->plugin->get_whatsapp_connection_handler();
		$is_connected        = $whatsapp_connection->is_connected();

		if ( $is_connected ) {
			$iframe_url = \WooCommerce\Facebook\Handlers\WhatsAppExtension::generate_wa_iframe_management_url( $this->plugin, );
		} else {
			$iframe_url = \WooCommerce\Facebook\Handlers\WhatsAppExtension::generate_wa_iframe_splash_url(
				$this->plugin,
				$whatsapp_connection->get_whatsapp_external_id()
			);
		}

		if ( empty( $iframe_url ) ) {
			return $this->error_banner();
		}
		?>
		<div class="facebook-whatsapp-iframe-container">
			<iframe
				id="facebook-whatsapp-iframe-enhanced"
				src="<?php echo esc_url( $iframe_url ); ?>"
				></iframe>
		</div>
		<?php
	}

	private function error_banner() {
		?>
		<div class="facebook-whatsapp-iframe-error-container">
			<div class="notice notice-error" style="margin: 0; padding-bottom: 20px;">
				<h3><?php esc_html_e( 'WhatsApp Utility Connection Error', 'facebook-for-woocommerce' ); ?></h3>
				<p><?php esc_html_e( 'There was an error loading the WhatsApp Utility Message Integration. Please try reloading the page or resetting your settings.', 'facebook-for-woocommerce' ); ?></p>
				<div style="margin-top: 15px;">
					<button
						type="button"
						class="button button-primary"
						onclick="window.location.reload();"
						style="margin-right: 10px;"
					>
						<?php esc_html_e( 'Reload Page', 'facebook-for-woocommerce' ); ?>
					</button>
					<button
						type="button"
						class="button button-secondary"
						onclick="if(confirm('<?php echo esc_js( __( 'Are you sure you want to reset WhatsApp settings? This action cannot be undone and you will have to re-onboard.', 'facebook-for-woocommerce' ) ); ?>')) { resetWhatsAppSettings(); }"
					>
						<?php esc_html_e( 'Reset Settings', 'facebook-for-woocommerce' ); ?>
					</button>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			function resetWhatsAppSettings() {
				// Use the same API client that's available on the page
				if (typeof whatsAppAPI !== 'undefined') {
					whatsAppAPI.uninstallWhatsAppSettings()
						.then(function(response) {
							if (response.success) {
								window.location.reload();
							}
						})
						.catch(function(error) {
							console.error('Error during settings reset:', error);
							alert('<?php echo esc_js( __( 'Error resetting settings. Please try again or contact support.', 'facebook-for-woocommerce' ) ); ?>');
						});
				}
			}
		</script>
		<?php
	}

	/**
	 * Generates the inline script for the whatsapp iframe onboarding flow.
	 *
	 * @since 3.5.0
	 *
	 * @return string
	 */
	public function generate_inline_enhanced_onboarding_script() {
		// Generate a fresh nonce for this request
		$nonce = wp_json_encode( wp_create_nonce( 'wp_rest' ) );

		return "
			const whatsAppAPI = GeneratePluginAPIClient({$nonce});
			const ALLOWED_ORIGINS = [
				'https://www.commercepartnerhub.com',
				'https://www.facebook.com',
				'https://business.facebook.com'
			];
			window.addEventListener('message', function(event) {
				if (ALLOWED_ORIGINS.indexOf(event.origin) === -1) {
					return;
				}
				const message = event.data;
				if (!message || typeof message !== 'object') {
					return;
				}
				const messageEvent = message.event;

				if (messageEvent === 'CommerceExtension::WA_INSTALL' && message.success) {

					const requestBody = {
						access_token: message.access_token,
						business_id: message.business_id,
						phone_number_id: message.phone_number_id,
						waba_id: message.waba_id,
						wa_installation_id: message.wa_installation_id,
					};

					whatsAppAPI.updateWhatsAppSettings(requestBody)
						.then(function(response) {
							if (response.success) {
								window.location.reload();
							} else {
								console.error('Error updating Facebook settings:', response);
							}
						})
						.catch(function(error) {
							console.error('Error during settings update:', error);
						});
				}

				if (messageEvent === 'CommerceExtension::WA_CONNECT' && message.success) {
					whatsAppAPI.notifyWhatsAppOnboardingComplete()
						.then(function(response) {
							if (!response.success) {
								console.error('Error marking WhatsApp onboarding complete:', response);
							}
						})
						.catch(function(error) {
							console.error('Error during onboarding-complete notify:', error);
						});
				}

				if (messageEvent === 'CommerceExtension::WA_RESIZE') {
					const iframe = document.getElementById('facebook-whatsapp-iframe-enhanced');
					if ( iframe ) {
						if ( message.height ) {
							iframe.height = message.height;
						}
						if ( message.width ) {
							iframe.width = message.width;
						}
					}
				}

				if (messageEvent === 'CommerceExtension::WA_UNINSTALL') {
					whatsAppAPI.uninstallWhatsAppSettings()
						.then(function(response) {
							if (response.success) {
								window.location.reload();
							}
						})
						.catch(function(error) {
							console.error('Error during uninstall:', error);
							window.location.reload();
						});
				}
			});
		";
	}
}
