<?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_Form_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Contactform extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Form_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;
	use \Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'contactform',
			'title'          => 'Sifency Contact Form 7',
			'icon'           => 'eicon-form-horizontal',
			'category'       => 'sifency-addons-widgets',
			'keywords'       => array( 'form', 'contact', 'cf7', 'wpf', 'sifency' ),
			'style_depends'  => array( 'sifency-form-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->add_sifency_form_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'label'          => esc_html__( 'Submit Button', 'sifency-addons' ),
				'selector'       => '.sifency-form-styler form input[type="submit"], .sifency-form-styler form button[type="submit"]',
				'hover_selector' => '.sifency-form-styler form input[type="submit"]:hover, .sifency-form-styler form button[type="submit"]:hover',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
		$this->add_Sifency_Addons_Custom_CSS_Controls();
	}
	protected function render() {
		$settings = $this->get_settings_for_display();
		$this->sifency_addons_render_custom_css();
		$animation_manager = new Sifency_Addons_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 = array( '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;
		}
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
			if ( ! empty( $shortcode ) ) {
				echo do_shortcode( $shortcode );
			} elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
					echo esc_html__( 'Form plugin not active or shortcode could not be generated.', 'sifency-addons' );
			}
			?>
		</div>
		<?php
	}
}
