<?php
use Elementor\Widget_Base;
use Elementor\Controls_Manager;

class SeekMake_Instant_Quote_Elementor extends Widget_Base {

    public function get_name() {
        return 'seekmake_instant_quote';
    }

    public function get_title() {
        return __('SeekMake Instant Quote', 'seekmake');
    }

    public function get_icon() {
        return 'eicon-price-table';
    }

    public function get_categories() {
        return ['general'];
    }

    public function get_keywords() {
        return ['seekmake', 'quote', 'instant', 'pricing'];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content', 'seekmake'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'manufacturer_id',
            [
                'label' => __('Manufacturer ID', 'seekmake'),
                'type' => Controls_Manager::TEXT,
                'default' => get_option('seekmake_manufacturer_id', ''),
                'description' => __('Leave empty to use the default from settings', 'seekmake'),
            ]
        );

        $this->add_control(
            'display_mode',
            [
                'label' => __('Display Mode', 'seekmake'),
                'type' => Controls_Manager::SELECT,
                'default' => 'inline',
                'options' => [
                    'modal' => __('Modal (Popup)', 'seekmake'),
                    'inline' => __('Inline (Embedded)', 'seekmake'),
                    'button' => __('Button Only', 'seekmake'),
                ],
            ]
        );

        $this->add_control(
            'button_text',
            [
                'label' => __('Button Text', 'seekmake'),
                'type' => Controls_Manager::TEXT,
                'default' => __('Get Instant Quote', 'seekmake'),
                'condition' => [
                    'display_mode!' => 'inline',
                ],
            ]
        );

        $this->add_control(
            'language',
            [
                'label' => __('Language', 'seekmake'),
                'type' => Controls_Manager::SELECT,
                'default' => 'en',
                'options' => [
                    'en' => __('English', 'seekmake'),
                    'es' => __('Spanish', 'seekmake'),
                    'fr' => __('French', 'seekmake'),
                    'de' => __('German', 'seekmake'),
                    'ar' => __('Arabic', 'seekmake'),
                ],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'style_section',
            [
                'label' => __('Button Style', 'seekmake'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'display_mode!' => 'inline',
                ],
            ]
        );

        $this->add_control(
            'button_style',
            [
                'label' => __('Button Style', 'seekmake'),
                'type' => Controls_Manager::SELECT,
                'default' => 'primary',
                'options' => [
                    'primary' => __('Primary', 'seekmake'),
                    'secondary' => __('Secondary', 'seekmake'),
                    'outline' => __('Outline', 'seekmake'),
                    'link' => __('Link', 'seekmake'),
                ],
            ]
        );

        $this->add_responsive_control(
            'button_align',
            [
                'label' => __('Alignment', 'seekmake'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'left' => [
                        'title' => __('Left', 'seekmake'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => __('Center', 'seekmake'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'right' => [
                        'title' => __('Right', 'seekmake'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'left',
            ]
        );

        $this->add_control(
            'custom_css_class',
            [
                'label' => __('Custom CSS Class', 'seekmake'),
                'type' => Controls_Manager::TEXT,
                'description' => __('Add custom CSS class for additional styling', 'seekmake'),
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        
        $manufacturer_id = !empty($settings['manufacturer_id']) 
            ? $settings['manufacturer_id'] 
            : get_option('seekmake_manufacturer_id', '');
        
        $manufacturer_id = SeekMake_Security::sanitize_manufacturer_id($manufacturer_id);
        $button_text = SeekMake_Security::sanitize_button_text($settings['button_text']);
        $style = SeekMake_Security::sanitize_button_style($settings['button_style']);
        $display = SeekMake_Security::sanitize_display_mode($settings['display_mode']);
        $lang = SeekMake_Security::sanitize_language($settings['language']);
        $align = sanitize_text_field($settings['button_align']);
        $custom_class = sanitize_html_class($settings['custom_css_class']);

        if (empty($manufacturer_id)) {
            echo '<p class="seekmake-error">' . esc_html__('Manufacturer ID is not configured.', 'seekmake') . '</p>';
            return;
        }

        if ($display === 'inline') {
            $this->render_inline($manufacturer_id, $lang);
        } else {
            $this->render_button($manufacturer_id, $button_text, $style, $lang, $align, $custom_class);
        }
    }

    private function render_inline($manufacturer_id, $lang) {
        $iframe_url = SEEKMAKE_API_URL . '/instant-quote/?isEmbed=true&manufacturer=' . esc_attr($manufacturer_id);
        ?>
        <section id="seekmake-quote-container" class="seekmake-quote-container">
            <iframe
                sandbox="allow-downloads allow-scripts allow-same-origin allow-forms allow-popups"
                id="seekmake-embed-iq"
                class="seekmake-embed-iq"
                src="<?php echo esc_url($iframe_url); ?>"
                style="width: 100%; height: 100vh; border: 0px"
                scrolling="no"
            ></iframe>
        </section>
        <?php
    }

    private function render_button($manufacturer_id, $button_text, $style, $lang, $align, $custom_class) {
        ?>
        <div class="seekmake-widget-wrapper seekmake-widget-<?php echo esc_attr($align); ?>">
            <button 
                class="seekmake-widget-button seekmake-button-<?php echo esc_attr($style); ?> seekmake-modal-trigger <?php echo esc_attr($custom_class); ?>"
                data-widget-type="instant-quote"
                data-manufacturer-id="<?php echo esc_attr($manufacturer_id); ?>"
                data-lang="<?php echo esc_attr($lang); ?>"
                type="button"
            >
                <?php echo esc_html($button_text); ?>
            </button>
        </div>
        <?php
    }
}
