<?php
/** Native adapters for Brizy, Bricks, Beaver Builder, and Divi. */

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

class YATOON_Builder_Integrations {
	public static function init() {
		add_filter( 'brizy_extensions', array( __CLASS__, 'register_brizy' ) );
		add_action( 'init', array( __CLASS__, 'register_bricks' ), 30 );
		add_action( 'init', array( __CLASS__, 'register_beaver' ), 30 );
		add_action( 'et_builder_ready', array( __CLASS__, 'register_divi' ) );
	}

	/**
	 * Register the compiled Yatoon element library with Brizy.
	 *
	 * @param array $extensions Existing Brizy third-party extensions.
	 * @return array
	 */
	public static function register_brizy( $extensions ) {
		if ( ! is_array( $extensions ) || ! class_exists( 'Brizy_Editor_ThirdParty_Extension' ) ) {
			return $extensions;
		}

		foreach ( $extensions as $extension ) {
			if ( is_object( $extension ) && method_exists( $extension, 'getName' ) && 'Yatoon.Brizy' === $extension->getName() ) {
				return $extensions;
			}
		}

		$config = YATOON_PLUGIN_DIR . 'assets/brizy/config.json';
		if ( ! is_readable( $config ) ) {
			return $extensions;
		}

		try {
			$extensions[] = new Brizy_Editor_ThirdParty_Extension( YATOON_PLUGIN_URL . 'assets/brizy/', $config );
		} catch ( Throwable $exception ) {
			// Keep WordPress and the editor usable if Brizy changes its extension loader.
		}

		return $extensions;
	}

	public static function surfaces() {
		return array(
			'booking'         => array( 'label' => __( 'Booking Form', 'yatoon-booking-system' ), 'shortcode' => 'yatoon_booking' ),
			'service_menu'    => array( 'label' => __( 'Service Menu', 'yatoon-booking-system' ), 'shortcode' => 'yatoon_service_menu' ),
			'catalog'         => array( 'label' => __( 'Service Catalog', 'yatoon-booking-system' ), 'shortcode' => 'yatoon_catalog' ),
			'gallery'         => array( 'label' => __( 'Gallery', 'yatoon-booking-system' ), 'shortcode' => 'yatoon_gallery' ),
			'reviews'         => array( 'label' => __( 'Reviews', 'yatoon-booking-system' ), 'shortcode' => 'yatoon_reviews' ),
			'customer_portal' => array( 'label' => __( 'Customer Portal', 'yatoon-booking-system' ), 'shortcode' => 'yatoon_customer_portal' ),
			'staff_portal'    => array( 'label' => __( 'Staff Portal', 'yatoon-booking-system' ), 'shortcode' => 'yatoon_staff_portal' ),
			'manage_booking'  => array( 'label' => __( 'Manage Booking', 'yatoon-booking-system' ), 'shortcode' => 'yatoon_manage_booking' ),
		);
	}

	public static function render_surface( $surface, array $settings = array() ) {
		$surfaces = self::surfaces();
		$surface  = sanitize_key( (string) $surface );
		if ( ! isset( $surfaces[ $surface ] ) ) {
			$surface = 'booking';
		}
		$parts = array( $surfaces[ $surface ]['shortcode'] );
		foreach ( array( 'location_id', 'service_id', 'staff_id', 'category', 'mode', 'limit', 'cols' ) as $key ) {
			if ( isset( $settings[ $key ] ) && '' !== (string) $settings[ $key ] ) {
				$value   = in_array( $key, array( 'location_id', 'service_id', 'staff_id', 'limit', 'cols' ), true ) ? absint( $settings[ $key ] ) : sanitize_text_field( $settings[ $key ] );
				$parts[] = $key . '="' . esc_attr( $value ) . '"';
			}
		}
		return do_shortcode( '[' . implode( ' ', $parts ) . ']' );
	}

	public static function register_bricks() {
		if ( ! class_exists( '\Bricks\Elements' ) ) {
			return;
		}
		$file = YATOON_PLUGIN_DIR . 'includes/builders/class-yatoon-bricks-element.php';
		if ( file_exists( $file ) ) {
			\Bricks\Elements::register_element( $file, 'yatoon-booking', 'YATOON_Bricks_Element' );
		}
	}

	public static function register_beaver() {
		if ( ! class_exists( 'FLBuilder' ) || ! class_exists( 'FLBuilderModule' ) || class_exists( 'YATOON_Beaver_Module' ) ) {
			return;
		}
		require_once YATOON_PLUGIN_DIR . 'includes/builders/beaver/class-yatoon-beaver-module.php';
		$options = array();
		foreach ( self::surfaces() as $key => $surface ) {
			$options[ $key ] = $surface['label'];
		}
		FLBuilder::register_module(
			'YATOON_Beaver_Module',
			array(
				'general' => array(
					'title'    => __( 'Yatoon Booking', 'yatoon-booking-system' ),
					'sections' => array(
						'content' => array(
							'title'  => __( 'Content', 'yatoon-booking-system' ),
							'fields' => array(
								'surface'     => array( 'type' => 'select', 'label' => __( 'Surface', 'yatoon-booking-system' ), 'default' => 'booking', 'options' => $options ),
								'location_id' => array( 'type' => 'text', 'label' => __( 'Location ID', 'yatoon-booking-system' ) ),
								'service_id'  => array( 'type' => 'text', 'label' => __( 'Service ID', 'yatoon-booking-system' ) ),
								'staff_id'    => array( 'type' => 'text', 'label' => __( 'Staff ID', 'yatoon-booking-system' ) ),
							),
						),
					),
				),
			)
		);
	}

	public static function register_divi() {
		if ( ! class_exists( 'ET_Builder_Module' ) || class_exists( 'YATOON_Divi_Module' ) ) {
			return;
		}
		require_once YATOON_PLUGIN_DIR . 'includes/builders/class-yatoon-divi-module.php';
		new YATOON_Divi_Module();
	}
}
