<?php
defined( 'ABSPATH' ) || exit;

class Jalpi_Element_Button extends Jalpi_Element {
    public static function type(): string { return 'button'; }

    public function render( array $node, Jalpi_Renderer $renderer ): string {
        $id           = sanitize_html_class( $node['id'] ?? '' );
        $content      = $node['content'] ?? [];
        $styles       = $node['styles']  ?? [];
        $text         = esc_html( $renderer->tags( $renderer->get_current_post_id() )->resolve( $content['text'] ?? __( 'Click Here', 'jalpi-page-builder' ) ) );
        $url          = $renderer->tags( $renderer->get_current_post_id() )->resolve_url( $content['url'] ?? '#' );
        $target       = ! empty( $content['newTab'] ) ? ' target="_blank" rel="noopener noreferrer"' : '';
        $a            = $renderer->get_css_attrs( $node );
        $icon_pos     = sanitize_key( $content['iconPosition'] ?? 'before' );
        $icon_spacing = absint( $content['iconSpacing'] ?? 6 );

        $sel = '.jalpi-el.jalpi-' . $id;

        // Normal state
        $renderer->add_css( $sel . '{' . $renderer->styles_to_css( $styles ) . '}' );

        // Hover state — compose from hover-specific style keys
        $hover_css = $this->button_hover_css( $styles );
        if ( '' !== $hover_css ) {
            $renderer->add_css( $sel . ':hover{' . $hover_css . '}' );
        }

        $raw_icon = $content['icon'] ?? null;
        if ( $raw_icon ) {
            $icon_html  = $renderer->render_icon_value( $raw_icon );
            $gap_style  = 'display:inline-flex;align-items:center;gap:' . $icon_spacing . 'px;';
            $inner      = $icon_pos === 'after'
                ? '<span style="display:contents">' . $text . '</span><span class="jalpi-btn-icon" aria-hidden="true">' . $icon_html . '</span>'
                : '<span class="jalpi-btn-icon" aria-hidden="true">' . $icon_html . '</span><span style="display:contents">' . $text . '</span>';
            $html = '<a href="' . esc_url( $url ) . '"' . $target . $a['id_attr'] . ' class="jalpi-el jalpi-btn jalpi-' . esc_attr( $id ) . $a['extra_cls'] . '" style="' . $gap_style . '">' . $inner . '</a>';
        } else {
            $html = '<a href="' . esc_url( $url ) . '"' . $target . $a['id_attr'] . ' class="jalpi-el jalpi-btn jalpi-' . esc_attr( $id ) . $a['extra_cls'] . '">' . $text . '</a>';
        }

        return $renderer->wrap_outer_padding( $html, $styles, $id );
    }

    // Build :hover CSS string for a button node from its hover-specific style keys.
    private function button_hover_css( array $styles ): string {
        $css = '';

        if ( ! empty( $styles['colorHover'] ) ) {
            $v = Jalpi_Sanitizer::sanitize_color( (string) $styles['colorHover'] );
            if ( $v ) $css .= 'color:' . $v . ';';
        }
        if ( ! empty( $styles['borderColorHover'] ) ) {
            $v = Jalpi_Sanitizer::sanitize_color( (string) $styles['borderColorHover'] );
            if ( $v ) $css .= 'border-color:' . $v . ';';
        }
        if ( ! empty( $styles['boxShadowHover'] ) ) {
            $v = Jalpi_Sanitizer::sanitize_css_value( (string) $styles['boxShadowHover'] );
            if ( $v ) $css .= 'box-shadow:' . $v . ';';
        }

        // Background hover — gradient or solid
        $bg_type = $styles['backgroundType'] ?? 'classic';
        if ( 'gradient' === $bg_type
             && ( ! empty( $styles['gradientColor1Hover'] ) || ! empty( $styles['gradientColor2Hover'] ) ) ) {
            $c1 = Jalpi_Sanitizer::sanitize_color( (string) ( $styles['gradientColor1Hover'] ?? '' ) )
                ?: Jalpi_Sanitizer::sanitize_color( (string) ( $styles['gradientColor1'] ?? '' ) )
                ?: '#000000';
            $c2 = Jalpi_Sanitizer::sanitize_color( (string) ( $styles['gradientColor2Hover'] ?? '' ) )
                ?: Jalpi_Sanitizer::sanitize_color( (string) ( $styles['gradientColor2'] ?? '' ) )
                ?: '#ffffff';
            $angle = sanitize_text_field( $styles['gradientAngle'] ?? '90deg' ) ?: '90deg';
            $css .= 'background-image:linear-gradient(' . $angle . ',' . $c1 . ',' . $c2 . ');';
        } elseif ( ! empty( $styles['backgroundColorHover'] ) ) {
            $v = Jalpi_Sanitizer::sanitize_color( (string) $styles['backgroundColorHover'] );
            if ( $v ) $css .= 'background-color:' . $v . ';background-image:none;';
        }

        return $css;
    }
}
