<?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_Button_Style_Controls;
use Sifency\Traits\Sifency_Icon_Style_Controls;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Button extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Button_Controls;
	use Sifency_Button_Style_Controls;
	use Sifency_Icon_Style_Controls;

	public function get_name() {
		return 'sifency-addons-button';
	}
	public function get_title() {
		return esc_html__( 'Sifency Button', 'sifency-addons' );
	}
	public function get_icon() {
		return 'eicon-button';
	}
	public function get_categories() {
		return [ 'sifency-addons-widgets' ];
	}
	public function get_keywords() {
		return [ 'button', 'link', 'cta', 'sifency' ];
	}
    public function get_style_depends() {
	    return [ 'sifency-icon', 'sifency-button-style' ];
    }
	protected function register_controls() {
		$this->add_sifency_button_controls();
		$this->add_sifency_flip_button_style_controls();
		$this->add_sifency_circle_button_style_controls();
		$this->add_sifency_button_style_controls();
		$this->add_sifency_icon_style_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.
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();

		// Basic validation
		if ( empty( $settings['button_text'] ) && empty( $settings['button_icon']['value'] ) ) {
			return;
		}

		// Animation Manager
		$animation_manager = new Sifency_Animation_Manager($settings);
		$wrapper_classes = [ 'sifency-button-wrapper' ];
		$wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);

		// Link Attributes
		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 ) {
		$button_classes = [ 'sifency-button sifency-button-style' ];
		
		if ( ! empty( $settings['button_style_type'] ) ) {
			$button_classes[] = esc_attr( $settings['button_style_type'] );
		}

		$this->add_render_attribute( 'button_tag', 'class', $button_classes );
		?>
		<a <?php $this->print_render_attribute_string( 'button_tag' ); ?>>
			<span class="sifency-button-content-wrapper">
				<?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; ?>

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

				<?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; ?>
			</span>
		</a>
		<?php
	}

	/**
	 * Render the Flip Text Button Style
	 */
	protected function render_flip_button( $settings ) {
		$button_classes = [ 'sifency-button sifency-button-style action-bar-button' ];
		
		$this->add_render_attribute( 'button_tag', 'class', $button_classes );
		?>
			<a <?php $this->print_render_attribute_string( 'button_tag' ); ?>>
				<span class="flip-text">
					<span class="default-text"><?php echo esc_html( $settings['button_text'] ); ?></span>
					<span class="hover-text"><?php echo esc_html( $settings['button_text'] ); ?></span>
				</span>
			</a>
		<?php
	}
	/**
	 * Render the Flip Text Button Style
	 */
	protected function render_circle_button( $settings ) {
		$button_classes = [ 'sifency-circle-btn sifency-button sifency-button-style' ];
		
		$this->add_render_attribute( 'button_tag', 'class', $button_classes );
		?>
			<a <?php $this->print_render_attribute_string( 'button_tag' ); ?>>
				<div class="button-in">
					<span class="default-text"><?php echo esc_html( $settings['button_text'] ); ?></span>
				</div>
			</a>
		<?php
	}
}