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

class IDVE_Block_Button {

    public static function register( IDVE_Blocks $registry ) {
        $registry->register( 'button', array(
            'label'    => __( 'Button', 'insight-dezign-visual-editor' ),
            'icon'     => 'button',
            'category' => 'content',
            'fields'   => array(
                'label'           => array( 'type' => 'text', 'label' => __( 'Button Text', 'insight-dezign-visual-editor' ), 'default' => 'Click Here' ),
                'url'             => array( 'type' => 'text', 'label' => __( 'URL', 'insight-dezign-visual-editor' ), 'default' => '#' ),
                'target'          => array( 'type' => 'select', 'label' => __( 'Open In', 'insight-dezign-visual-editor' ), 'options' => array( '_self', '_blank' ), 'default' => '_self' ),
                'alignment'       => array( 'type' => 'select', 'label' => __( 'Alignment', 'insight-dezign-visual-editor' ), 'options' => array( 'left', 'center', 'right' ), 'default' => 'center' ),
                'backgroundColor' => array( 'type' => 'color', 'label' => __( 'Background Color', 'insight-dezign-visual-editor' ), 'default' => '#0073aa' ),
                'textColor'       => array( 'type' => 'color', 'label' => __( 'Text Color', 'insight-dezign-visual-editor' ), 'default' => '#ffffff' ),
                'size'            => array( 'type' => 'select', 'label' => __( 'Size', 'insight-dezign-visual-editor' ), 'options' => array( 'small', 'medium', 'large' ), 'default' => 'medium' ),
                'fullWidth'       => array( 'type' => 'toggle', 'label' => __( 'Full Width', 'insight-dezign-visual-editor' ), 'default' => false ),
            ),
            'render' => array( __CLASS__, 'render' ),
        ) );
    }

    public static function render( $data, $settings ) {
        $label  = isset( $data['label'] ) ? $data['label'] : 'Click Here';
        $url    = isset( $data['url'] ) ? $data['url'] : '#';
        $target = isset( $data['target'] ) ? $data['target'] : '_self';
        $size   = isset( $data['size'] ) ? $data['size'] : 'medium';
        $full   = ! empty( $data['fullWidth'] );

        $allowed_sizes = array( 'small', 'medium', 'large' );
        if ( ! in_array( $size, $allowed_sizes, true ) ) {
            $size = 'medium';
        }

        $classes = array( 'idve-button', 'idve-button-' . $size );
        if ( $full ) {
            $classes[] = 'idve-button-full';
        }
        if ( ! empty( $settings['cssClass'] ) ) {
            $classes[] = $settings['cssClass'];
        }

        // Base button styles inline so output works without plugin CSS.
        $style_parts = array(
            'display:inline-block',
            'text-decoration:none',
            'cursor:pointer',
            'border:none',
            'text-align:center',
            'line-height:1.5',
        );

        // Size-specific padding and font-size.
        switch ( $size ) {
            case 'small':
                $style_parts[] = 'padding:6px 16px';
                $style_parts[] = 'font-size:14px';
                break;
            case 'large':
                $style_parts[] = 'padding:14px 32px';
                $style_parts[] = 'font-size:18px';
                break;
            default:
                $style_parts[] = 'padding:10px 24px';
                $style_parts[] = 'font-size:16px';
                break;
        }

        if ( $full ) {
            $style_parts[] = 'display:block';
            $style_parts[] = 'width:100%';
        }

        if ( ! empty( $data['backgroundColor'] ) ) {
            $c = self::sanitize_color( $data['backgroundColor'] );
            if ( $c ) {
                $style_parts[] = 'background-color:' . $c;
            }
        }
        if ( ! empty( $data['textColor'] ) ) {
            $c = self::sanitize_color( $data['textColor'] );
            if ( $c ) {
                $style_parts[] = 'color:' . $c;
            }
        }

        // Settings-level styles go on the <a>, not the wrapper.
        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['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 ) ) . '"';

        $rel = '_blank' === $target ? ' rel="noopener noreferrer"' : '';

        return '<a href="' . esc_url( $url ) . '" target="' . esc_attr( $target ) . '" class="' . esc_attr( implode( ' ', $classes ) ) . '"' . $style_attr . $rel . '>' . esc_html( $label ) . '</a>';
    }

    private static function sanitize_color( $color ) {
        if ( empty( $color ) || 'transparent' === $color ) {
            return '';
        }
        if ( preg_match( '/^#[0-9a-fA-F]{3,8}$/', $color ) ) {
            return $color;
        }
        if ( preg_match( '/^rgba?\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,\s*[\d.]+)?\s*\)$/', $color ) ) {
            return $color;
        }
        return '';
    }
}
