<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Newsletter_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Mailchimp extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Newsletter_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-mailchimp',
			'title'          => 'Sifency Mailchimp Form',
			'icon'           => 'eicon-mailchimp',
			'category'       => 'sifency-addons-widgets',
			'keywords'       => array( 'mailchimp', 'newsletter', 'form', 'email' ),
			'style_depends'  => array( 'sifency-newsletter-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_newsletter_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector'       => '.sifency-mc4wp-form form input[type="submit"]',
				'hover_selector' => '.sifency-mc4wp-form form input[type="submit"]:hover',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'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_Addons_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 = array( '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
	}
}
