<?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_Newsletter_Controls;

class Sifency_Widget_Mailchimp extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Newsletter_Controls;

    public function get_name() {
        return 'sifency-mailchimp';
    }

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

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

    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_newsletter_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();
        $form_id = $settings['mc4wp_form_id'];
        $animation_manager = new Sifency_Animation_Manager($settings);
        if ( empty($form_id) ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo '<div class="sifency-form-placeholder">' . esc_html__('Please select a Mailchimp form to display.', 'sifency-addons') . '</div>';
            }
            return;
        }
        if ( ! function_exists('mc4wp_get_forms') ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo '<div class="sifency-form-placeholder">' . esc_html__('Mailchimp for WP plugin is not active.', 'sifency-addons') . '</div>';
            }
            return;
        }
        $wrapper_classes = ['sifency-mc4wp-form'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php
            
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php echo do_shortcode('[mc4wp_form id="' . esc_attr($form_id) . '"]'); ?>
        </div>
        <?php
    }
}