<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Form_Controls;
class Sifency_Widget_Contactform extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Form_Controls;
    public function get_name() {
        return 'contactform';
    }

    public function get_title() {
        return __('Sifency Form Styler', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-form-horizontal';
    }

    public function get_categories() {
        return ['sifency-addons-widgets'];
    }

    public function get_style_depends() {
	    return [ 'sifency-frontend-style' ];
    }

    public function get_script_depends() {
        return [ 'sifency-frontend-script' ];
    }
    
    protected function register_controls() {
        $this->add_sifency_form_controls();
        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        if ( empty($settings['selected_form']) ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo '<div class="sifency-form-placeholder">' . esc_html__('Please select a form to display.', 'sifency-addons') . '</div>';
            }
            return;
        }
        $wrapper_classes = ['sifency-form-styler'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        list($type, $id) = explode('-', $settings['selected_form'], 2);
        $shortcode = '';

        switch ($type) {
            case 'cf7':
                $shortcode = '[contact-form-7 id="' . esc_attr($id) . '"]';
                break;
            case 'wpf':
                $shortcode = '[wpforms id="' . esc_attr($id) . '"]';
                break;
        }
        ?>
        <div <?php
             
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php
                if ( ! empty($shortcode) ) {
                    echo do_shortcode($shortcode);
                } else {
                    if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                        echo esc_html__('Form plugin not active or shortcode could not be generated.', 'sifency-addons');
                    }
                }
            ?>
        </div>
        <?php
    }
}