<?php
namespace Calio\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;

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

/**
 * Renders a richer CTA button with layered text, icon motion and state-driven styles.
 */
class AdvancedButtonWidget extends BaseWidget {
    public function get_name(): string { return 'calio-advanced-button'; }
    public function get_title(): string { return esc_html__('Advanced Button', 'calio-addon'); }
    public function get_icon(): string { return 'eicon-button'; }
    public function get_style_depends(): array { return $this->resolve_widget_style_depends(); }

    /**
     * Registers CTA content and style controls used by the Advanced Button widget.
     */
    protected function register_controls(): void {
        $this->start_controls_section('content', ['label' => esc_html__('Button', 'calio-addon')]);
        $this->add_control('text', ['label' => esc_html__('Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Click Here']);
        $this->add_control('secondary_text', ['label' => esc_html__('Secondary Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('micro_note', ['label' => esc_html__('Micro Note', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('helper_text', ['label' => esc_html__('Helper Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('offer_kicker', ['label' => esc_html__('Offer Kicker', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('helper_position', ['label' => esc_html__('Helper Position', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'above', 'options' => ['above' => esc_html__('Above Button', 'calio-addon'), 'inside' => esc_html__('Inside Button', 'calio-addon'), 'below' => esc_html__('Below Button', 'calio-addon')]]);
        $this->add_control('badge_text', ['label' => esc_html__('Badge Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('side_note', ['label' => esc_html__('Side Note', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('preset', ['label' => esc_html__('Preset', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'cta', 'options' => ['cta' => esc_html__('CTA', 'calio-addon'), 'arrow' => esc_html__('Arrow', 'calio-addon'), 'stacked' => esc_html__('Stacked', 'calio-addon'), 'compact' => esc_html__('Compact', 'calio-addon')]]);
        $this->add_control('layout_mode', ['label' => esc_html__('Layout Mode', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'inline', 'options' => ['inline' => esc_html__('Inline', 'calio-addon'), 'split' => esc_html__('Split', 'calio-addon'), 'block' => esc_html__('Block CTA', 'calio-addon')]]);
        $this->add_control('density', ['label' => esc_html__('Density', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'comfy', 'options' => ['compact' => esc_html__('Compact', 'calio-addon'), 'comfy' => esc_html__('Comfy', 'calio-addon'), 'hero' => esc_html__('Hero', 'calio-addon')]]);
        $this->add_control('skin', ['label' => esc_html__('Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'solid', 'options' => ['solid' => esc_html__('Solid', 'calio-addon'), 'soft' => esc_html__('Soft', 'calio-addon'), 'outline' => esc_html__('Outline', 'calio-addon'), 'glass' => esc_html__('Glass', 'calio-addon')]]);
        $this->add_control('chrome', ['label' => esc_html__('Surface Chrome', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'flat', 'options' => ['flat' => esc_html__('Flat', 'calio-addon'), 'raised' => esc_html__('Raised', 'calio-addon'), 'glow' => esc_html__('Glow', 'calio-addon')]]);
        $this->add_control('link', ['label' => esc_html__('Link', 'calio-addon'), 'type' => Controls_Manager::URL, 'default' => ['url' => '']]);
        $this->add_control('icon', ['label' => esc_html__('Icon', 'calio-addon'), 'type' => Controls_Manager::ICONS]);
        $this->add_control('icon_position', ['label' => esc_html__('Icon Position', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'left', 'options' => ['left' => esc_html__('Before', 'calio-addon'), 'right' => esc_html__('After', 'calio-addon')]]);
        $this->add_control('icon_animation', ['label' => esc_html__('Icon Animation', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'slide', 'options' => ['none' => esc_html__('None', 'calio-addon'), 'slide' => esc_html__('Slide', 'calio-addon'), 'rotate' => esc_html__('Rotate', 'calio-addon'), 'pop' => esc_html__('Pop', 'calio-addon')]]);
        $this->add_control('icon_box_shape', ['label' => esc_html__('Icon Box Shape', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'pill', 'options' => ['pill' => esc_html__('Pill', 'calio-addon'), 'rounded' => esc_html__('Rounded', 'calio-addon'), 'square' => esc_html__('Square', 'calio-addon')]]);
        $this->add_control('button_alignment', ['label' => esc_html__('Alignment', 'calio-addon'), 'type' => Controls_Manager::CHOOSE, 'default' => 'flex-start', 'options' => ['flex-start' => ['title' => esc_html__('Left', 'calio-addon'), 'icon' => 'eicon-text-align-left'], 'center' => ['title' => esc_html__('Center', 'calio-addon'), 'icon' => 'eicon-text-align-center'], 'flex-end' => ['title' => esc_html__('Right', 'calio-addon'), 'icon' => 'eicon-text-align-right']], 'selectors' => ['{{WRAPPER}} .calio-advanced-button-wrap' => 'justify-content: {{VALUE}};']]);
        $this->add_control('content_align', ['label' => esc_html__('Content Align', 'calio-addon'), 'type' => Controls_Manager::CHOOSE, 'default' => 'flex-start', 'options' => ['flex-start' => ['title' => esc_html__('Start', 'calio-addon'), 'icon' => 'eicon-h-align-left'], 'center' => ['title' => esc_html__('Center', 'calio-addon'), 'icon' => 'eicon-h-align-center'], 'flex-end' => ['title' => esc_html__('End', 'calio-addon'), 'icon' => 'eicon-h-align-right']], 'selectors' => ['{{WRAPPER}} .calio-btn-content' => 'align-items: {{VALUE}};']]);
        $this->add_control('full_width', ['label' => esc_html__('Full Width', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes']);
        $this->add_control('show_arrow_hint', ['label' => esc_html__('Show Arrow Hint', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes']);
        $this->add_control('effect', ['label' => esc_html__('Hover Effect', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'lift', 'options' => ['none' => esc_html__('None', 'calio-addon'), 'lift' => esc_html__('Lift', 'calio-addon'), 'fill' => esc_html__('Fill', 'calio-addon'), 'slide' => esc_html__('Slide', 'calio-addon'), 'pulse' => esc_html__('Pulse', 'calio-addon')]]);
        $this->end_controls_section();

        $this->start_controls_section('style_button', ['label' => esc_html__('Button', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'btn_typography', 'selector' => '{{WRAPPER}} .calio-btn-text']);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'btn_sub_typography', 'selector' => '{{WRAPPER}} .calio-btn-subtext']);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'btn_note_typography', 'selector' => '{{WRAPPER}} .calio-btn-note']);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'btn_helper_typography', 'selector' => '{{WRAPPER}} .calio-btn-helper']);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'btn_kicker_typography', 'selector' => '{{WRAPPER}} .calio-btn-kicker, {{WRAPPER}} .calio-btn-side-note']);
        $this->add_style_heading('btn_shape_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('btn_radius', esc_html__('Border Radius', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_dimensions_control('button_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button', 'padding');
        $this->add_dimensions_control('button_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button', 'margin');
        $this->add_control('content_gap', ['label' => esc_html__('Content Gap', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 0, 'max' => 24]], 'selectors' => ['{{WRAPPER}} .calio-btn-content' => 'gap: {{SIZE}}{{UNIT}};']]);
        $this->add_control('helper_spacing', ['label' => esc_html__('Helper Spacing', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 0, 'max' => 24]], 'selectors' => ['{{WRAPPER}} .calio-advanced-button-wrap' => 'gap: {{SIZE}}{{UNIT}};']]);
        $this->add_dimensions_control('badge_radius', esc_html__('Badge Radius', 'calio-addon'), '{{WRAPPER}} .calio-btn-badge', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_style_heading('btn_icon_heading', esc_html__('Icon', 'calio-addon'));
        $this->add_control('icon_size', ['label' => esc_html__('Icon Size', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 8, 'max' => 72]], 'selectors' => ['{{WRAPPER}} .calio-btn-icon' => 'font-size: {{SIZE}}{{UNIT}};']]);
        $this->add_control('icon_gap', ['label' => esc_html__('Icon Gap', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 0, 'max' => 24]], 'selectors' => ['{{WRAPPER}} .calio-advanced-button' => 'gap: {{SIZE}}{{UNIT}};']]);
        $this->add_control('icon_box_size', ['label' => esc_html__('Icon Box Size', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 16, 'max' => 72]], 'selectors' => ['{{WRAPPER}} .calio-btn-icon' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};']]);
        $this->add_style_heading('btn_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('btn_tabs');
        $this->start_controls_tab('btn_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('btn_text_normal', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button');
        $this->add_text_paint_controls('btn_subtext_normal', esc_html__('Secondary Text Color', 'calio-addon'), '{{WRAPPER}} .calio-btn-subtext');
        $this->add_text_paint_controls('btn_note_normal', esc_html__('Micro Note Color', 'calio-addon'), '{{WRAPPER}} .calio-btn-note');
        $this->add_text_paint_controls('btn_helper_normal', esc_html__('Helper Text Color', 'calio-addon'), '{{WRAPPER}} .calio-btn-helper');
        $this->add_text_paint_controls('btn_kicker_normal', esc_html__('Kicker / Side Note Color', 'calio-addon'), ['{{WRAPPER}} .calio-btn-kicker', '{{WRAPPER}} .calio-btn-side-note']);
        $this->add_text_paint_controls('btn_badge_text_normal', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-btn-badge');
        $this->add_background_type_control('btn_badge_bg_normal_group', '{{WRAPPER}} .calio-btn-badge');
        $this->add_background_type_control('btn_bg_normal_group', '{{WRAPPER}} .calio-advanced-button');
        $this->add_text_paint_controls('btn_icon_normal', esc_html__('Icon Color', 'calio-addon'), '{{WRAPPER}} .calio-btn-icon');
        $this->add_background_type_control('btn_icon_bg_normal_group', '{{WRAPPER}} .calio-btn-icon');
        $this->end_controls_tab();
        $this->start_controls_tab('btn_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('btn_text_hover', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button:hover');
        $this->add_text_paint_controls('btn_subtext_hover', esc_html__('Secondary Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button:hover .calio-btn-subtext');
        $this->add_text_paint_controls('btn_note_hover', esc_html__('Micro Note Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button:hover .calio-btn-note');
        $this->add_text_paint_controls('btn_helper_hover', esc_html__('Helper Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button-wrap:hover .calio-btn-helper');
        $this->add_text_paint_controls('btn_kicker_hover', esc_html__('Kicker / Side Note Color', 'calio-addon'), ['{{WRAPPER}} .calio-advanced-button:hover .calio-btn-kicker', '{{WRAPPER}} .calio-advanced-button:hover .calio-btn-side-note']);
        $this->add_text_paint_controls('btn_badge_text_hover', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button:hover .calio-btn-badge');
        $this->add_background_type_control('btn_badge_bg_hover_group', '{{WRAPPER}} .calio-advanced-button:hover .calio-btn-badge');
        $this->add_background_type_control('btn_bg_hover_group', ['{{WRAPPER}} .calio-advanced-button:hover', '{{WRAPPER}} .calio-advanced-button::after']);
        $this->add_border_paint_controls('btn_border_hover', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button:hover');
        $this->add_text_paint_controls('btn_icon_hover', esc_html__('Icon Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button:hover .calio-btn-icon');
        $this->add_background_type_control('btn_icon_bg_hover_group', '{{WRAPPER}} .calio-advanced-button:hover .calio-btn-icon');
        $this->end_controls_tab();
        $this->start_controls_tab('btn_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('btn_text_active', esc_html__('Text Color', 'calio-addon'), ['{{WRAPPER}} .calio-advanced-button:active', '{{WRAPPER}} .calio-advanced-button:focus-visible']);
        $this->add_background_type_control('btn_bg_active_group', ['{{WRAPPER}} .calio-advanced-button:active', '{{WRAPPER}} .calio-advanced-button:focus-visible']);
        $this->add_text_paint_controls('btn_subtext_active', esc_html__('Secondary Text Color', 'calio-addon'), ['{{WRAPPER}} .calio-advanced-button:active .calio-btn-subtext', '{{WRAPPER}} .calio-advanced-button:focus-visible .calio-btn-subtext']);
        $this->add_text_paint_controls('btn_note_active', esc_html__('Micro Note Color', 'calio-addon'), ['{{WRAPPER}} .calio-advanced-button:active .calio-btn-note', '{{WRAPPER}} .calio-advanced-button:focus-visible .calio-btn-note']);
        $this->add_text_paint_controls('btn_helper_active', esc_html__('Helper Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button-wrap:focus-within .calio-btn-helper');
        $this->add_text_paint_controls('btn_kicker_active', esc_html__('Kicker / Side Note Color', 'calio-addon'), ['{{WRAPPER}} .calio-advanced-button:active .calio-btn-kicker', '{{WRAPPER}} .calio-advanced-button:active .calio-btn-side-note', '{{WRAPPER}} .calio-advanced-button:focus-visible .calio-btn-kicker', '{{WRAPPER}} .calio-advanced-button:focus-visible .calio-btn-side-note']);
        $this->add_border_paint_controls('btn_border_active', esc_html__('Border Color', 'calio-addon'), ['{{WRAPPER}} .calio-advanced-button:active', '{{WRAPPER}} .calio-advanced-button:focus-visible']);
        $this->add_text_paint_controls('btn_icon_active', esc_html__('Icon Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button:active .calio-btn-icon');
        $this->add_background_type_control('btn_icon_bg_active_group', '{{WRAPPER}} .calio-advanced-button:active .calio-btn-icon');
        $this->add_text_paint_controls('btn_badge_text_active', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-advanced-button:active .calio-btn-badge');
        $this->add_background_type_control('btn_badge_bg_active_group', '{{WRAPPER}} .calio-advanced-button:active .calio-btn-badge');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('btn_outline_heading', esc_html__('Outline & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'btn_border', 'selector' => '{{WRAPPER}} .calio-advanced-button']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'btn_shadow', 'selector' => '{{WRAPPER}} .calio-advanced-button']);
        $this->end_controls_section();
    }

    /**
     * Outputs the CTA markup using Elementor link attributes for sanitized URL handling.
     */
    protected function render(): void {
        $settings = $this->get_settings_for_display();
        $link = $settings['link'] ?? [];
        $classes = ['calio-advanced-button', 'effect-' . sanitize_html_class((string) ($settings['effect'] ?? 'lift')), 'skin-' . sanitize_html_class((string) ($settings['skin'] ?? 'solid')), 'preset-' . sanitize_html_class((string) ($settings['preset'] ?? 'cta')), 'chrome-' . sanitize_html_class((string) ($settings['chrome'] ?? 'flat')), 'layout-' . sanitize_html_class((string) ($settings['layout_mode'] ?? 'inline')), 'density-' . sanitize_html_class((string) ($settings['density'] ?? 'comfy')), 'icon-anim-' . sanitize_html_class((string) ($settings['icon_animation'] ?? 'slide')), 'icon-shape-' . sanitize_html_class((string) ($settings['icon_box_shape'] ?? 'pill'))];
        if (($settings['full_width'] ?? '') === 'yes') { $classes[] = 'is-full'; }
        if (($settings['show_arrow_hint'] ?? '') === 'yes') { $classes[] = 'has-arrow-hint'; }
        $this->add_render_attribute('button', 'class', implode(' ', $classes));
        if (!empty($link['url'])) { $this->add_link_attributes('button', $link); }
        $has_icon = !empty($settings['icon']['value']);
        $has_subtext = !empty($settings['secondary_text']);
        $has_note = !empty($settings['micro_note']);
        $has_helper = !empty($settings['helper_text']);
        $has_kicker = !empty($settings['offer_kicker']);
        $has_badge = !empty($settings['badge_text']);
        $has_side_note = !empty($settings['side_note']);
        $helper_position = (string) ($settings['helper_position'] ?? 'above');
        if (!in_array($helper_position, ['above', 'inside', 'below'], true)) { $helper_position = 'above'; }
        echo '<div class="calio-advanced-button-wrap">';
        if ($has_helper && $helper_position === 'above') { echo '<span class="calio-btn-helper is-above">' . esc_html((string) $settings['helper_text']) . '</span>'; }
        echo '<a ' . wp_kses_post($this->get_render_attribute_string('button')) . '>';
        if ($has_icon && ($settings['icon_position'] ?? 'left') === 'left') {
            echo '<span class="calio-btn-icon left" aria-hidden="true">' . wp_kses_post($this->get_icon_markup($settings['icon'])) . '</span>';
        }
        echo '<span class="calio-btn-main">';
        if ($has_helper && $helper_position === 'inside') { echo '<span class="calio-btn-helper is-inside">' . esc_html((string) $settings['helper_text']) . '</span>'; }
        if ($has_kicker) { echo '<span class="calio-btn-kicker">' . esc_html((string) $settings['offer_kicker']) . '</span>'; }
        echo '<span class="calio-btn-content">';
        echo '<span class="calio-btn-text-row">';
        echo '<span class="calio-btn-text">' . esc_html((string) ($settings['text'] ?? '')) . '</span>';
        if ($has_badge) { echo '<span class="calio-btn-badge">' . esc_html((string) $settings['badge_text']) . '</span>'; }
        if ($has_side_note) { echo '<span class="calio-btn-side-note">' . esc_html((string) $settings['side_note']) . '</span>'; }
        echo '</span>';
        if ($has_subtext) { echo '<span class="calio-btn-subtext">' . esc_html((string) $settings['secondary_text']) . '</span>'; }
        if ($has_note) { echo '<span class="calio-btn-note">' . esc_html((string) $settings['micro_note']) . '</span>'; }
        echo '</span>';
        echo '</span>';
        if ($has_icon && ($settings['icon_position'] ?? 'left') === 'right') {
            echo '<span class="calio-btn-icon right" aria-hidden="true">' . wp_kses_post($this->get_icon_markup($settings['icon'])) . '</span>';
        }
        if (($settings['show_arrow_hint'] ?? '') === 'yes') { echo '<span class="calio-btn-hint" aria-hidden="true">&#8594;</span>'; }
        echo '</a>';
        if ($has_helper && $helper_position === 'below') { echo '<span class="calio-btn-helper is-below">' . esc_html((string) $settings['helper_text']) . '</span>'; }
        echo '</div>';
    }
}






















