<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Image_Size;
use \Elementor\Group_Control_Typography;
use \Elementor\Icons_Manager;
use \Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Info_Box extends Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency_info_box',
			'title'         => 'Sifency Info Box',
			'icon'          => 'eicon-info-box',
			'category'      => array( 'sifency-addons-widgets' ),
			'style_depends' => array( 'sifency-info-style', 'sifency-frontend-style', 'sifency-icon', 'sifency-typo-style' ),
		);}

	protected function register_controls() {

		// --- LAYOUT SECTION ---
		$this->start_controls_section(
			'section_content_layout',
			array( 'label' => esc_html__( 'Layout & Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'skin',
			array(
				'label'        => esc_html__( 'Layout Skin', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'classic',
				'options'      => array(
					'classic' => esc_html__( 'Classic (Icon Top)', 'sifency-addons' ),
					'side'    => esc_html__( 'Side Feature (Icon Left/Right)', 'sifency-addons' ),
					'hang'    => esc_html__( 'Overhanging Icon', 'sifency-addons' ),
					'card'    => esc_html__( 'Cover Card (Image BG)', 'sifency-addons' ),
				),
				'prefix_class' => 'sifency-ib-skin-',
			)
		);

		// Icon / Media
		$this->add_control(
			'icon_type',
			array(
				'label'   => esc_html__( 'Media Type', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'icon'  => array(
						'title' => 'Icon',
						'icon'  => 'eicon-star',
					),
					'image' => array(
						'title' => 'Image',
						'icon'  => 'eicon-image',
					),
					'text'  => array(
						'title' => 'Number',
						'icon'  => 'eicon-t-letter',
					),
				),
				'default' => 'icon',
			)
		);
		$this->add_control(
			'selected_icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-layer-group',
					'library' => 'fa-solid',
				),
				'condition' => array( 'icon_type' => 'icon' ),
			)
		);
		$this->add_control(
			'selected_image',
			array(
				'label'     => esc_html__( 'Upload Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => array( 'url' => Utils::get_placeholder_image_src() ),
				'condition' => array( 'icon_type' => 'image' ),
			)
		);
		$this->add_control(
			'selected_text',
			array(
				'label'     => esc_html__( 'Number/Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => '01',
				'condition' => array( 'icon_type' => 'text' ),
			)
		);

		// Content
		$this->add_control(
			'title_text',
			array(
				'label'       => esc_html__( 'Title', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'Modern Solutions',
				'label_block' => true,
			)
		);
		$this->add_control(
			'description_text',
			array(
				'label'   => esc_html__( 'Description', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'We help you grow your business with our modern strategies and tools.',
			)
		);

		$this->add_control(
			'show_subtitle',
			array(
				'label'   => esc_html__( 'Show Subtitle', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'subtitle_text',
			array(
				'label'     => esc_html__( 'Subtitle', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => 'SUMMER SALE 30% OFF',
				'condition' => array( 'show_subtitle' => 'yes' ),
			)
		);

		// Link
		$this->add_control(
			'link_type',
			array(
				'label'   => esc_html__( 'Link Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'none',
				'options' => array(
					'none' => 'None',
					'box'  => 'Link Whole Box',
					'btn'  => 'Read More Button',
				),
			)
		);
		$this->add_control(
			'link_url',
			array(
				'label'       => esc_html__( 'Link URL', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => 'https://your-link.com',
				'condition'   => array( 'link_type!' => 'none' ),
			)
		);
		$this->add_control(
			'btn_text',
			array(
				'label'     => esc_html__( 'Button Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Read More',
				'condition' => array( 'link_type' => 'btn' ),
			)
		);
		$this->add_control(
			'btn_icon',
			array(
				'label'     => esc_html__( 'Button Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'icon-right',
					'library' => 'sifency-icons',
				),
				'condition' => array( 'link_type' => 'btn' ),
			)
		);

		// Alignment Controls
		$this->add_responsive_control(
			'content_align',
			array(
				'label'        => esc_html__( 'Content Alignment', 'sifency-addons' ),
				'type'         => Controls_Manager::CHOOSE,
				'options'      => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'      => 'left',
				'prefix_class' => 'sifency-align-',
				'separator'    => 'before',
			)
		);
		$this->add_responsive_control(
			'vertical_align',
			array(
				'label'     => esc_html__( 'Vertical Align (Icon)', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'start'  => array(
						'title' => 'Top',
						'icon'  => 'eicon-v-align-top',
					),
					'center' => array(
						'title' => 'Middle',
						'icon'  => 'eicon-v-align-middle',
					),
					'end'    => array(
						'title' => 'Bottom',
						'icon'  => 'eicon-v-align-bottom',
					),
				),
				'default'   => 'start',
				'selectors' => array( '{{WRAPPER}} .sifency-ib-inner' => 'align-items: {{VALUE}};' ),
				'condition' => array( 'skin' => 'side' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'container_ib',
				'label'    => 'Container Style',
				'selector' => '.sifency-info-box',
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'number_style',
				'label'          => 'Text Icon Style',
				'selector'       => '.sifency-number-style',
				'show_alignment' => false,
				'condition'      => array( 'icon_type' => 'text' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'sep_icon',
				'label'           => 'Icon Style',
				'selector'        => '.sifency-ib-icon',
				'hover_selector'  => '.sifency-info-box:hover .sifency-ib-icon',
				'condition'       => array( 'icon_type' => 'icon' ),
				'show_dimensions' => true,
			)
		);

		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'       => 'ib_image',
				'label'        => 'Image Style',
				'selectors'    => array(
					'image'      => '.sifency-ib-image-innger img',
					'container'  => '.sifency-ib-image-innger img',
					'dimensions' => '.sifency-ib-image-innger img',
				),
				'show_overlay' => false,
				'condition'    => array( 'icon_type' => 'image' ),
			)
		);
		// --- TYPOGRAPHY ---
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'title_style',
				'label'          => 'Title Style',
				'selector'       => '.sifency-heading-style',
				'hover_selector' => '.sifency-info-box:hover .sifency-heading-style',
				'condition'      => array(
					'title_text!' => '',
				),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'label_style',
				'label'          => 'Subtitle Style',
				'selector'       => '.sifency-caption-style',
				'hover_selector' => '.sifency-info-box:hover .sifency-caption-style',
				'exclude'        => array( 'bg', 'radius', 'shadow', 'stroke' ),
				'condition'      => array( 'show_subtitle' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'desc_style',
				'label'          => 'Description Style',
				'selector'       => '.sifency-content-style',
				'hover_selector' => '.sifency-info-box:hover .sifency-content-style',
				'exclude'        => array( 'bg', 'radius', 'shadow', 'stroke' ),
				'color_only'     => true,
				'show_max_width' => true,
				'condition'      => array(
					'description_text!' => '',
				),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'          => 'button_',
				'label'           => esc_html__( 'Button Style', 'sifency-addons' ),
				'selector'        => '.sifency-button-style',
				'hover_selector'  => '.sifency-info-box:hover .sifency-button-style',
				'target_children' => true,
				'condition'       => array(),
			)
		);

		$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();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes();

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-info-box' );

		$link_url = $settings['link_url']['url'] ?? '';
		$tag      = 'div';

		if ( 'box' === $settings['link_type'] && ! empty( $link_url ) ) {
			$this->add_link_attributes( 'wrapper', $settings['link_url'] );
			$this->add_render_attribute( 'wrapper', 'class', 'sifency-box-linked' );
			$tag = 'a';
		}

		?>
		<<?php echo wp_kses_post( $tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $wrapper_attrs ); ?>>
			
			<div class="sifency-ib-inner">
				
				<div class="sifency-ib-icon-wrap">
						<?php if ( 'image' === $settings['icon_type'] ) : ?>
							<div class="sifency-ib-image-innger"><?php echo wp_kses_post( Group_Control_Image_Size::get_attachment_image_html( $settings, 'selected_image', 'selected_image' ) ); ?></div>
						<?php elseif ( 'text' === $settings['icon_type'] ) : ?>
							<span class="sifency-ib-text-icon sifency-number-style"><?php echo esc_html( $settings['selected_text'] ); ?></span>
						<?php else : ?>
							<div class="sifency-ib-icon">
							<?php Icons_Manager::render_icon( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</div>
						<?php endif; ?>
				</div>

				<div class="sifency-ib-content">
					
					<?php if ( ! empty( $settings['subtitle_text'] ) && 'yes' === $settings['show_subtitle'] ) : ?>
						<h5 class="sifency-ib-subtitle sifency-caption-style"><?php echo esc_html( $settings['subtitle_text'] ); ?></h5>
					<?php endif; ?>

					<?php if ( ! empty( $settings['title_text'] ) ) : ?>
						<h3 class="sifency-ib-title sifency-heading-style"><?php echo esc_html( $settings['title_text'] ); ?></h3>
					<?php endif; ?>


					<?php if ( ! empty( $settings['description_text'] ) ) : ?>
						<div class="sifency-ib-desc sifency-content-style">
							<?php echo wp_kses_post( $settings['description_text'] ); ?>
						</div>
					<?php endif; ?>

					<?php
					if ( 'btn' === $settings['link_type'] && ! empty( $link_url ) ) :
						$this->add_link_attributes( 'btn_link', $settings['link_url'] );
						?>
						<div class="sifency-ib-btn-wrap">
							<a <?php echo wp_kses_post( $this->get_render_attribute_string( 'btn_link' ) ); ?> class="sifency-ib-btn text_underline_animation sifency-button-style sifency-addons-primary-button">
								<span><?php echo esc_html( $settings['btn_text'] ); ?></span>
								<?php if ( ! empty( $settings['btn_icon']['value'] ) ) : ?>
									<?php Icons_Manager::render_icon( $settings['btn_icon'] ); ?>
								<?php endif; ?>
							</a>
						</div>
					<?php endif; ?>
				</div>

			</div>
		</<?php echo wp_kses_post( $tag ); ?>>
		<?php
	}
}
