<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Button_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Button extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Button_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-button',
			'title'          => 'Sifency Button',
			'icon'           => 'eicon-button',
			'category'       => 'sifency-addons-widgets',
			'keywords'       => array( 'button', 'link', 'cta', 'sifency' ),
			'style_depends'  => array( 'sifency-button-style', 'sifency-icon' ),
			'script_depends' => array( 'sifency-button-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_button_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_circle_button_style_controls();
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'target_children' => true,
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'button_icon_',
				'label'           => esc_html__( 'Override Icon Style', 'sifency-addons' ),
				'target_children' => true,
				'hover_selector'  => '.sifency-button:hover .sifency-icon-style',
				'condition'       => array( 'button_icon[value]!' => '' ),
			)
		);
		$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.
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( $settings['button_text'] ) && empty( $settings['button_icon']['value'] ) ) {
			return;
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-button-wrapper' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		if ( ! empty( $settings['button_link']['url'] ) ) {
			$this->add_link_attributes( 'button_tag', $settings['button_link'] );
		}

		$style_type = isset( $settings['button_style_type'] ) ? $settings['button_style_type'] : 'default';

		?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
			if ( 'flip_text' === $style_type ) {
				$this->render_flip_button( $settings );
			} elseif ( 'circle' === $style_type ) {
				$this->render_circle_button( $settings );
			} else {
				$this->render_default_button( $settings );
			}
			?>
		</div>
		<?php
	}
	/**
	 * Render the Default Button Style
	 */
	protected function render_default_button( $settings ) {
		$effect          = $settings['button_effect'] ?? 'default';
		$underline_class = ( 'yes' === ( $settings['enable_underline_anim'] ?? '' ) ) ? 'text_underline_animation' : '';

		$button_classes = array( 'sifency-button', 'sifency-button-style', 'animated-btn', $effect );

		if ( ! empty( $settings['button_style_type'] ) ) {
			$button_classes[] = esc_attr( $settings['button_style_type'] );
		}

		$this->add_render_attribute( 'button_tag', 'class', $button_classes );

		if ( 'scramble' === $effect ) {
			$this->add_render_attribute( 'button_tag', 'data-scramble', 'true' );
		}

		?>
		<a <?php $this->print_render_attribute_string( 'button_tag' ); ?>>
			<span class="sifency-button-content-wrapper">
				
				<?php if ( ! empty( $settings['button_icon']['value'] ) && 'before' === $settings['icon_position'] ) : ?>
					<span class="sifency-button-icon sifency-icon-style">
						<?php \Elementor\Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) ); ?>
					</span>
				<?php endif; ?>

				<?php if ( ! empty( $settings['button_text'] ) ) : ?>
					<div class="sifency-button-text <?php echo esc_attr( $underline_class ); ?>">
						<span><?php echo esc_html( $settings['button_text'] ); ?></span>
					</div>
				<?php endif; ?>

				<?php if ( ! empty( $settings['button_icon']['value'] ) && 'after' === $settings['icon_position'] ) : ?>
					<span class="sifency-button-icon sifency-icon-style">
						<?php \Elementor\Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) ); ?>
					</span>
				<?php endif; ?>
				
			</span>
		</a>
		<?php
	}

	/**
	 * Render the Flip Text Button Style
	 */
	protected function render_flip_button( $settings ) {
		$button_classes  = array( 'sifency-button sifency-button-style flip-button-wrapper' );
		$underline_class = ( 'yes' === $settings['enable_underline_anim'] ) ? 'text_underline_animation' : '';
		$this->add_render_attribute( 'button_tag', 'class', $button_classes );
		?>
		<a <?php $this->print_render_attribute_string( 'button_tag' ); ?>>
			<span class="flip-text">
				<?php if ( ! empty( $settings['button_icon']['value'] ) && $settings['icon_position'] === 'before' ) : ?>
				<span class="sifency-button-icon sifency-icon-style">
					<?php \Elementor\Icons_Manager::render_icon( $settings['button_icon'] ); ?>
				</span>
				<?php endif; ?>
				<span
					class="default-text <?php echo esc_attr( $underline_class ); ?>"><?php echo esc_html( $settings['button_text'] ); ?>
				</span>
				<span
					class="hover-text <?php echo esc_attr( $underline_class ); ?>"><?php echo esc_html( $settings['button_text'] ); ?></span>
			</span>
			<?php if ( ! empty( $settings['button_icon']['value'] ) && $settings['icon_position'] === 'after' ) : ?>
			<span class="sifency-button-icon sifency-icon-style">
				<?php \Elementor\Icons_Manager::render_icon( $settings['button_icon'] ); ?>
			</span>
			<?php endif; ?>
		</a>
		<?php
	}
	/**
	 * Render the Flip Text Button Style
	 */
	protected function render_circle_button( $settings ) {
		$button_classes  = array( 'sifency-circle-btn sifency-button sifency-button-style' );
		$underline_class = ( 'yes' === $settings['enable_underline_anim'] ) ? 'text_underline_animation' : '';
		$this->add_render_attribute( 'button_tag', 'class', $button_classes );
		?>
		<a <?php $this->print_render_attribute_string( 'button_tag' ); ?>>

			<?php if ( ! empty( $settings['button_icon']['value'] ) && $settings['icon_position'] === 'before' ) : ?>
			<span class="sifency-button-icon sifency-icon-style">
				<?php \Elementor\Icons_Manager::render_icon( $settings['button_icon'] ); ?>
			</span>
			<?php endif; ?>

			<div class="button-in">
				<span
					class="default-text <?php echo esc_attr( $underline_class ); ?>"><?php echo esc_html( $settings['button_text'] ); ?></span>
			</div>

			<?php if ( ! empty( $settings['button_icon']['value'] ) && $settings['icon_position'] === 'after' ) : ?>
			<span class="sifency-button-icon sifency-icon-style">
				<?php \Elementor\Icons_Manager::render_icon( $settings['button_icon'] ); ?>
			</span>
			<?php endif; ?>
		</a>
		<?php
	}
}
