<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class IDVE_Block_Image {

    public static function register( IDVE_Blocks $registry ) {
        $registry->register( 'image', array(
            'label'    => __( 'Image', 'insight-dezign-visual-editor' ),
            'icon'     => 'format-image',
            'category' => 'media',
            'fields'   => array(
                'imageId'     => array( 'type' => 'number', 'label' => __( 'Image ID', 'insight-dezign-visual-editor' ), 'default' => 0 ),
                'imageUrl'    => array( 'type' => 'image', 'label' => __( 'Image', 'insight-dezign-visual-editor' ), 'default' => '' ),
                'alt'         => array( 'type' => 'text', 'label' => __( 'Alt Text', 'insight-dezign-visual-editor' ), 'default' => '' ),
                'alignment'   => array( 'type' => 'select', 'label' => __( 'Alignment', 'insight-dezign-visual-editor' ), 'options' => array( 'left', 'center', 'right', 'full' ), 'default' => 'center' ),
                'linkUrl'     => array( 'type' => 'text', 'label' => __( 'Link URL', 'insight-dezign-visual-editor' ), 'default' => '' ),
                'linkTarget'  => array( 'type' => 'select', 'label' => __( 'Link Target', 'insight-dezign-visual-editor' ), 'options' => array( '_self', '_blank' ), 'default' => '_self' ),
                'maxWidth'    => array( 'type' => 'text', 'label' => __( 'Max Width', 'insight-dezign-visual-editor' ), 'default' => '' ),
                'imageSize'   => array( 'type' => 'select', 'label' => __( 'Image Size', 'insight-dezign-visual-editor' ), 'options' => array( 'full', 'large', 'medium_large', 'medium', 'thumbnail' ), 'default' => 'full' ),
            ),
            'render' => array( __CLASS__, 'render' ),
        ) );
    }

    public static function render( $data, $settings ) {
        $url       = isset( $data['imageUrl'] ) ? $data['imageUrl'] : '';
        $image_id  = isset( $data['imageId'] ) ? absint( $data['imageId'] ) : 0;
        $image_size = isset( $data['imageSize'] ) && ! empty( $data['imageSize'] ) ? $data['imageSize'] : 'full';

        // Fall back: look up attachment ID from URL if not stored.
        if ( $image_id <= 0 && ! empty( $url ) ) {
            $image_id = attachment_url_to_postid( $url );
        }

        // Resolve URL from attachment ID + size when available.
        if ( $image_id > 0 && 'full' !== $image_size ) {
            $src = wp_get_attachment_image_src( $image_id, $image_size );
            if ( $src ) {
                $url = $src[0];
            }
        }

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

        $alt = isset( $data['alt'] ) ? $data['alt'] : '';

        // Always include responsive image base styles inline.
        $style_parts = array();
        if ( ! empty( $data['maxWidth'] ) ) {
            $style_parts[] = 'max-width:' . esc_attr( $data['maxWidth'] );
        } else {
            $style_parts[] = 'max-width:100%';
        }
        $style_parts[] = 'height:auto';

        // Settings-level styles go on the <img>, not the wrapper (matching button pattern).
        if ( ! empty( $settings['padding'] ) && is_array( $settings['padding'] ) ) {
            $p  = $settings['padding'];
            $pt = isset( $p['top'] ) ? intval( $p['top'] ) : 0;
            $pr = isset( $p['right'] ) ? intval( $p['right'] ) : 0;
            $pb = isset( $p['bottom'] ) ? intval( $p['bottom'] ) : 0;
            $pl = isset( $p['left'] ) ? intval( $p['left'] ) : 0;
            if ( $pt || $pr || $pb || $pl ) {
                $style_parts[] = 'padding:' . $pt . 'px ' . $pr . 'px ' . $pb . 'px ' . $pl . 'px';
            }
        }
        if ( ! empty( $settings['margin'] ) && is_array( $settings['margin'] ) ) {
            $m  = $settings['margin'];
            $mt = isset( $m['top'] ) ? intval( $m['top'] ) : 0;
            $mr = isset( $m['right'] ) ? intval( $m['right'] ) : 0;
            $mb = isset( $m['bottom'] ) ? intval( $m['bottom'] ) : 0;
            $ml = isset( $m['left'] ) ? intval( $m['left'] ) : 0;
            if ( $mt || $mr || $mb || $ml ) {
                $style_parts[] = 'margin:' . $mt . 'px ' . $mr . 'px ' . $mb . 'px ' . $ml . 'px';
            }
        }
        if ( ! empty( $settings['backgroundColor'] ) ) {
            $style_parts[] = 'background-color:' . esc_attr( $settings['backgroundColor'] );
        }
        if ( ! empty( $settings['border'] ) && is_array( $settings['border'] ) ) {
            $border = $settings['border'];
            $bcolor = ! empty( $border['color'] ) ? $border['color'] : '#000000';
            $bstyle = ! empty( $border['style'] ) ? sanitize_key( $border['style'] ) : 'solid';
            if ( ! empty( $border['width'] ) && is_array( $border['width'] ) ) {
                foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
                    $w = isset( $border['width'][ $side ] ) ? absint( $border['width'][ $side ] ) : 0;
                    if ( $w > 0 ) {
                        $style_parts[] = 'border-' . $side . ':' . $w . 'px ' . $bstyle . ' ' . esc_attr( $bcolor );
                    }
                }
            }
            if ( ! empty( $border['radius'] ) && is_array( $border['radius'] ) ) {
                $tl = isset( $border['radius']['topLeft'] ) ? absint( $border['radius']['topLeft'] ) : 0;
                $tr = isset( $border['radius']['topRight'] ) ? absint( $border['radius']['topRight'] ) : 0;
                $br = isset( $border['radius']['bottomRight'] ) ? absint( $border['radius']['bottomRight'] ) : 0;
                $bl = isset( $border['radius']['bottomLeft'] ) ? absint( $border['radius']['bottomLeft'] ) : 0;
                if ( $tl || $tr || $br || $bl ) {
                    $style_parts[] = 'border-radius:' . $tl . 'px ' . $tr . 'px ' . $br . 'px ' . $bl . 'px';
                }
            }
        }

        $style_attr = ' style="' . esc_attr( implode( ';', $style_parts ) ) . '"';

        $class_attr = '';
        if ( ! empty( $settings['cssClass'] ) ) {
            $class_attr = ' class="' . esc_attr( $settings['cssClass'] ) . '"';
        }

        $img = '<img src="' . esc_url( $url ) . '" alt="' . esc_attr( $alt ) . '" loading="lazy"' . $class_attr . $style_attr . '>';

        $link_url = isset( $data['linkUrl'] ) ? $data['linkUrl'] : '';
        if ( ! empty( $link_url ) ) {
            $target = isset( $data['linkTarget'] ) ? $data['linkTarget'] : '_self';
            $rel    = '_blank' === $target ? ' rel="noopener noreferrer"' : '';
            $img    = '<a href="' . esc_url( $link_url ) . '" target="' . esc_attr( $target ) . '"' . $rel . '>' . $img . '</a>';
        }

        return $img;
    }
}
