<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Utils;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;
use Sifency\Traits\Sifency_Button_Style_Controls;
use Sifency\Traits\Sifency_Item_Style_Controls;
use Sifency\Traits\Sifency_Image_Style_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Iconbox_Controls;

class Sifency_Widget_Iconbox extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Core_Text_Style;
	use Sifency_Button_Style_Controls;
	use Sifency_Item_Style_Controls;
	use Sifency_Image_Style_Controls;
	use Sifency_Iconbox_Controls;

	public function get_name() {
		return 'sifency-addons-iconbox';
	}
	public function get_title() {
		return esc_html__( 'Sifency Icon Box', 'sifency-addons' );
	}
	public function get_icon() {
		return 'eicon-info-box';
	}
	public function get_categories() {
		return [ 'sifency-addons-widgets' ];
	}
    public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-icon', 'sifency-iconbox-style' ];
    }
	protected function register_controls() {
		$this->add_sifency_iconbox_controls();
		$this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Title Style',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Content Style',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
		$this->add_sifency_iconbox_icon_style_controls();
		$this->add_sifency_image_style_controls();
		$this->update_control('image_style_section', ['condition' => ['icon_type' => 'image', ], ] );
		$this->update_control( 'image_mask_style_section', [ 'condition' => [ 'icon_type' => 'image', ], ] );
		$this->update_control( 'image_overlay_style_section', [ 'condition' => [ 'icon_type' => 'image', ], ] );
		$this->add_sifency_button_style_controls();
		$this->add_sifency_iconbox_button_icon_style_controls();
		$this->update_control( 'sifency_animation_target', [ 'type' => Controls_Manager::HIDDEN, 'default' => 'wrapper' ] );
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$has_link = ! empty( $settings['link']['url'] );
		$animation_manager = new Sifency_Animation_Manager($settings);
		$wrapper_attrs = $animation_manager->get_wrapper_attributes([ 'sifency-iconbox' ]);

		$this->add_render_attribute( 'content_wrapper', 'class', 'sifency-iconbox-content' );
		$this->add_render_attribute( 'title', 'class', 'sifency-iconbox-title sifency-heading-style' );
		$this->add_inline_editing_attributes( 'title_text', 'none' );
		$this->add_render_attribute( 'description', 'class', 'sifency-iconbox-description sifency-content-style' );
		$this->add_inline_editing_attributes( 'description_text' );

		if ( $has_link ) {
			$this->add_link_attributes( 'link_wrapper', $settings['link'] );
		}

        // Prepare the button icon HTML
        $button_icon_html = '';
        if ( 'yes' === $settings['show_read_more'] && ! empty( $settings['read_more_icon']['value'] ) ) {
            $this->add_render_attribute( 'button_icon', 'class', [
                'sifency-button-icon',
                'sifency-button-icon--position-' . $settings['read_more_icon_position'],
            ] );
            ob_start();
            Icons_Manager::render_icon( $settings['read_more_icon'], [ 'aria-hidden' => 'true' ] );
            $button_icon_html = '<span ' . $this->get_render_attribute_string('button_icon') . '>' . ob_get_clean() . '</span>';
        }
		?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php if ( $has_link ) : ?>
				<a <?php echo wp_kses_post ($this->get_render_attribute_string( 'link_wrapper' )); ?>>
			<?php endif; ?>

			<div class="sifency-iconbox-inner sifency-item-style">
				<?php if ( 'icon' === $settings['icon_type'] && ! empty( $settings['icon']['value'] ) ) : ?>
					<div class="sifency-iconbox-icon elementor-animation-<?php echo esc_attr($settings['hover_animation']); ?>">
						<?php Icons_Manager::render_icon( $settings['icon'], [ 'aria-hidden' => 'true' ] ); ?>
					</div>
				<?php elseif ( 'image' === $settings['icon_type'] && ! empty( $settings['image']['url'] ) ) : ?>
					<div class="sifency-iconbox-icon sifency-image-container">
						<img src="<?php echo esc_url( $settings['image']['url'] ); ?>" alt="<?php echo esc_attr( \Elementor\Control_Media::get_image_alt( $settings['image'] ) ); ?>" />
						<div class="sifency-image-overlay"></div>
					</div>
				<?php endif; ?>

				<div <?php echo wp_kses_post ($this->get_render_attribute_string( 'content_wrapper' )); ?>>
					<?php if ( ! empty( $settings['title_text'] ) ) : ?>
						<<?php echo tag_escape( $settings['title_html_tag'] ); ?> <?php echo wp_kses_post ($this->get_render_attribute_string( 'title' )); ?>>
							<?php echo esc_html( $settings['title_text'] ); ?>
						</<?php echo tag_escape( $settings['title_html_tag'] ); ?>>
					<?php endif; ?>

					<?php if ( ! empty( $settings['description_text'] ) ) : ?>
						<p <?php echo wp_kses_post ($this->get_render_attribute_string( 'description' )); ?>>
							<?php echo esc_html( $settings['description_text'] ); ?>
						</p>
					<?php endif; ?>


					<?php if ( 'yes' === $settings['show_read_more'] && ! empty( $settings['read_more_text'] ) ) : ?>
						<span class="sifency-button-style">
							<?php
							if ( 'before' === $settings['read_more_icon_position'] ) {
								echo wp_kses_post($button_icon_html);
							}
							echo '<span>' . esc_html( $settings['read_more_text'] ) . '</span>';
							if ( 'after' === $settings['read_more_icon_position'] ) {
								echo wp_kses_post($button_icon_html);
							}
							?>
						</span>
					<?php endif; ?>
				</div>
			</div>

			<?php if ( $has_link ) : ?>
				</a>
			<?php endif; ?>
		</div>
		<?php
	}


}