<?php
/**
 * Plugin Name: BW Image Hotspot
 * Plugin URI: https://bowdenworks.com
 * Description: Add interactive hotspots to images with customizable tooltips
 * Version: 1.0.0
 * Author: Bowden Works
 * Author URI: https://bowdenworks.com
 * Text Domain: bw-image-hotspot
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.2
 */

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

define( 'BW_IMAGE_HOTSPOT_VERSION', '1.0.0' );
define( 'BW_IMAGE_HOTSPOT_PATH', plugin_dir_path( __FILE__ ) );
define( 'BW_IMAGE_HOTSPOT_URL', plugin_dir_url( __FILE__ ) );

class BW_Image_Hotspot {
    private static $instance = null;

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        add_action( 'init', array( $this, 'bw_register_block' ) );
        add_action( 'enqueue_block_editor_assets', array( $this, 'bw_enqueue_editor_assets' ) );
        add_action( 'wp_enqueue_scripts', array( $this, 'bw_enqueue_frontend_assets' ) );
    }

    public function bw_register_block() {
        register_block_type( BW_IMAGE_HOTSPOT_PATH . 'build/block.json', array(
            'render_callback' => array( $this, 'bw_render_hotspot_block' )
        ) );
    }

    public function bw_enqueue_editor_assets() {
        wp_enqueue_script(
            'bw-image-hotspot-editor',
            BW_IMAGE_HOTSPOT_URL . 'build/index.js',
            array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-data' ),
            BW_IMAGE_HOTSPOT_VERSION
        );

        wp_enqueue_style(
            'bw-image-hotspot-editor',
            BW_IMAGE_HOTSPOT_URL . 'build/editor.css',
            array( 'wp-edit-blocks' ),
            BW_IMAGE_HOTSPOT_VERSION
        );
    }

    public function bw_enqueue_frontend_assets() {
        if ( has_block( 'bw/image-hotspot' ) ) {
            wp_enqueue_style(
                'bw-image-hotspot-frontend',
                BW_IMAGE_HOTSPOT_URL . 'build/style.css',
                array(),
                BW_IMAGE_HOTSPOT_VERSION
            );

            wp_enqueue_script(
                'bw-image-hotspot-frontend',
                BW_IMAGE_HOTSPOT_URL . 'build/frontend.js',
                array(),
                BW_IMAGE_HOTSPOT_VERSION,
                true
            );
        }
    }

    public function bw_render_hotspot_block( $attributes ) {
        $image_url = isset( $attributes['imageUrl'] ) ? esc_url( $attributes['imageUrl'] ) : '';
        $image_id = isset( $attributes['imageId'] ) ? intval( $attributes['imageId'] ) : 0;
        $hotspots = isset( $attributes['hotspots'] ) ? $attributes['hotspots'] : array();
        $unique_id = 'bw-hotspot-' . uniqid();

        if ( empty( $image_url ) ) {
            return '';
        }

        ob_start();
        ?>
        <div class="bw-image-hotspot-container" id="<?php echo esc_attr( $unique_id ); ?>">
            <div class="bw-hotspot-image-wrapper">
                <img src="<?php echo esc_url( $image_url ); ?>" alt="" class="bw-hotspot-image">
                <?php foreach ( $hotspots as $index => $hotspot ) : ?>
                    <div 
                        class="bw-hotspot-dot" 
                        style="left: <?php echo esc_attr( $hotspot['x'] ); ?>%; top: <?php echo esc_attr( $hotspot['y'] ); ?>%;"
                        data-hotspot-id="<?php echo esc_attr( $index ); ?>"
                    >
                        <div class="bw-hotspot-pulse"></div>
                        <div class="bw-hotspot-tooltip">
                            <?php if ( ! empty( $hotspot['tooltipImage'] ) ) : ?>
                                <img src="<?php echo esc_url( $hotspot['tooltipImage'] ); ?>" alt="" class="bw-tooltip-image">
                            <?php endif; ?>
                            <?php if ( ! empty( $hotspot['text'] ) ) : ?>
                                <div class="bw-tooltip-text"><?php echo wp_kses_post( $hotspot['text'] ); ?></div>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
}

BW_Image_Hotspot::get_instance();