<?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_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Iconbox_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Iconbox extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Iconbox_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-iconbox',
			'title'         => 'Sifency Icon Box',
			'icon'          => 'eicon-info-box',
			'category'      => 'sifency-addons-widgets',
			'keywords'      => array( 'icon', 'box', 'feature', 'info' ),
			'style_depends' => array( 'sifency-typo-style', 'sifency-iconbox-style', 'sifency-icon' ),
		);}
	protected function register_controls() {
		$this->add_sifency_iconbox_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-iconbox-inner',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Title Style',
				'selector' => '.sifency-heading-style',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Content Style',
				'selector'   => '.sifency-content-style',
				'color_only' => true,
				'exclude'    => array( 'bg', 'radius', 'shadow', 'stroke' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'         => 'main_icon_',
				'condition'      => array( 'icon_type' => 'icon' ),
				'selector'       => '.sifency-iconbox-icon i, .sifency-iconbox-icon svg',
				'hover_selector' => '.sifency-iconbox:hover .sifency-iconbox-icon i, .sifency-iconbox:hover .sifency-iconbox-icon svg',
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'condition' => array( 'icon_type' => 'image' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'condition' => array(
					'show_read_more'  => 'yes',
					'read_more_text!' => '',
				),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'button_icon_',
				'condition' => array( 'show_read_more' => 'yes' ),
				'selector'  => '',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	protected function render() {
		$settings          = $this->get_settings_for_display();
		$has_link          = ! empty( $settings['link']['url'] );
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( array( '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'] );
		}

		$button_icon_html = '';
		if ( 'yes' === $settings['show_read_more'] && ! empty( $settings['read_more_icon']['value'] ) ) {
			$this->add_render_attribute(
				'button_icon',
				'class',
				array(
					'sifency-button-icon',
					'sifency-button-icon--position-' . $settings['read_more_icon_position'],
				)
			);
			ob_start();
			Icons_Manager::render_icon( $settings['read_more_icon'], array( '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">
				<?php if ( 'icon' === $settings['icon_type'] && ! empty( $settings['icon']['value'] ) ) : ?>
					<div class="sifency-iconbox-icon">
						<?php Icons_Manager::render_icon( $settings['icon'], array( '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 wp_kses_post( $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
	}


}
