<?php
namespace Sifency\Traits;
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use \Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;

trait Sifency_Iconbox_Controls {
	/**
	 * Registers the main content controls for the Icon Box widget.
	 */
	protected function add_sifency_iconbox_controls() {
		$this->start_controls_section(
			'section_content',
			[
				'label' => esc_html__( 'Icon Box', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'icon_type',
			[
				'label'   => esc_html__( 'Media Type', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => [
					'icon'  => [
						'title' => esc_html__( 'Icon', 'sifency-addons' ),
						'icon'  => 'eicon-star',
					],
					'image' => [
						'title' => esc_html__( 'Image', 'sifency-addons' ),
						'icon'  => 'eicon-image-bold',
					],
				],
				'default' => 'icon',
			]
		);

		$this->add_control(
			'icon',
			[
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => [
					'value'   => 'icon-heart-empty',
					'library' => 'sifency-icons',
				],
				'condition' => [
					'icon_type' => 'icon',
				],
			]
		);

		$this->add_control(
			'image',
			[
				'label'     => esc_html__( 'Choose Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'default'   => [
					'url' => Utils::get_placeholder_image_src(),
				],
				'condition' => [
					'icon_type' => 'image',
				],
			]
		);

		$this->add_control(
			'title_text',
			[
				'label'   => esc_html__( 'Title', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'Icon Box Title', 'sifency-addons' ),
				'dynamic' => [ 'active' => true ],
			]
		);

		$this->add_control(
			'description_text',
			[
				'label'   => esc_html__( 'Description', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => esc_html__( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'sifency-addons' ),
				'dynamic' => [ 'active' => true ],
			]
		);

		$this->add_control(
			'show_read_more',
			[
				'label'     => esc_html__( 'Show Read More', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'separator' => 'before',
			]
		);

		$this->add_control(
			'read_more_text',
			[
				'label'     => esc_html__( 'Read More Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Read More', 'sifency-addons' ),
				'condition' => [ 'show_read_more' => 'yes' ],
			]
		);

        $this->add_control(
            'read_more_icon',
            [
                'label' => esc_html__( 'Read More Icon', 'sifency-addons' ),
                'type' => Controls_Manager::ICONS,
                'default' => [
                    'value' => 'icon-right',
                    'library' => 'sifency-icons',
                ],
                'condition' => [ 'show_read_more' => 'yes' ],
            ]
        );

        $this->add_control(
            'read_more_icon_position',
            [
                'label' => esc_html__( 'Icon Position', 'sifency-addons' ),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'before' => esc_html__( 'Before', 'sifency-addons' ),
                    'after' => esc_html__( 'After', 'sifency-addons' ),
                ],
                'default' => 'after',
                'condition' => [
                    'show_read_more' => 'yes',
                    'read_more_icon[value]!' => '',
                ],
            ]
        );

		$this->add_control(
			'link',
			[
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => esc_html__( 'https://your-link.com', 'sifency-addons' ),
				'dynamic'     => [ 'active' => true ],
			]
		);

		$this->add_control(
			'position',
			[
				'label'        => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'         => Controls_Manager::CHOOSE,
				'options'      => [
					'left'  => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon'  => 'eicon-h-align-left' ],
					'top'   => [ 'title' => esc_html__( 'Top', 'sifency-addons' ), 'icon'  => 'eicon-v-align-top' ],
					'right' => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon'  => 'eicon-h-align-right' ],
				],
				'default'      => 'top',
				'prefix_class' => 'sifency-iconbox-position-',
			]
		);

		$this->add_control(
			'title_html_tag',
			[
				'label'   => esc_html__( 'Title HTML Tag', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'h1'  => 'H1', 'h2'  => 'H2', 'h3'  => 'H3', 'h4'  => 'H4', 'h5'  => 'H5', 'h6'  => 'H6', 'div' => 'div', 'span' => 'span', 'p' => 'p',
				],
				'default' => 'h3',
			]
		);
		$this->end_controls_section();
	}

	/**
	 * Registers the advanced style controls for the main icon.
	 */
	protected function add_sifency_iconbox_icon_style_controls() {
		$this->start_controls_section(
			'section_style_icon',
			[
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'tab' => Controls_Manager::TAB_STYLE,
				'condition' => [ 'icon_type' => 'icon' ],
			]
		);

		$icon_container_selector = '{{WRAPPER}} .sifency-iconbox-icon';
		$icon_element_selector = '{{WRAPPER}} .sifency-iconbox-icon i, {{WRAPPER}} .sifency-iconbox-icon svg';
		$hover_trigger_selector = '{{WRAPPER}} .sifency-iconbox-inner:hover';

		$this->add_responsive_control(
			'icon_size',
			[
				'label' => esc_html__( 'Size', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 6, 'max' => 300 ] ],
				'selectors' => [
					$icon_container_selector => 'font-size: {{SIZE}}{{UNIT}};',
					$icon_element_selector => 'width: {{SIZE}}{{UNIT}}; height: auto;',
				],
			]
		);

		$this->add_responsive_control(
			'icon_padding',
			[
				'label' => esc_html__( 'Padding', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					$icon_container_selector => 'display: inline-flex; align-items: center; justify-content: center; padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'icon_rotation',
			[
				'label' => esc_html__( 'Rotate', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'deg' => [ 'min' => -360, 'max' => 360 ] ],
				'selectors' => [ $icon_container_selector => 'transform: rotate({{SIZE}}deg);' ],
			]
		);

		$this->add_responsive_control(
			'icon_border_radius',
			[
				'label' => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [ $icon_container_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
			]
		);

		$this->start_controls_tabs( 'icon_style_tabs' );
			$this->start_controls_tab( 'icon_normal_tab', [ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ] );
			$this->add_control( 'icon_color', [ 'label' => esc_html__( 'Icon Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $icon_element_selector => 'color: {{VALUE}}; fill: {{VALUE}};' ] ] );
			$this->add_control( 'icon_bg_color', [ 'label' => esc_html__( 'Background Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $icon_container_selector => 'background-color: {{VALUE}};' ] ] );
			$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_border', 'selector' => $icon_container_selector ] );
			$this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'icon_box_shadow', 'selector' => $icon_container_selector ] );
			$this->end_controls_tab();

			$this->start_controls_tab( 'icon_hover_tab', [ 'label' => esc_html__( 'Hover', 'sifency-addons' ) ] );
			$this->add_control( 'icon_hover_color', [ 'label' => esc_html__( 'Icon Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $hover_trigger_selector . ' ' . $icon_element_selector => 'color: {{VALUE}}; fill: {{VALUE}};' ] ] );
			$this->add_control( 'icon_hover_bg_color', [ 'label' => esc_html__( 'Background Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ $hover_trigger_selector . ' ' . $icon_container_selector => 'background-color: {{VALUE}};' ] ] );
			$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'icon_hover_border', 'selector' => $hover_trigger_selector . ' ' . $icon_container_selector ] );
			$this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'icon_hover_box_shadow', 'selector' => $hover_trigger_selector . ' ' . $icon_container_selector ] );
			$this->add_control( 'hover_animation', [ 'label' => esc_html__( 'Hover Animation', 'sifency-addons' ), 'type' => Controls_Manager::HOVER_ANIMATION ] );
			$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();
	}

	/**
	 * Registers the style controls for the "Read More" button's icon.
	 */
	protected function add_sifency_iconbox_button_icon_style_controls() {
		$this->start_controls_section(
			'section_style_button_icon',
			[
				'label' => esc_html__( 'Button Icon', 'sifency-addons' ),
				'tab' => Controls_Manager::TAB_STYLE,
				'condition' => [
					'show_read_more' => 'yes',
					'read_more_icon[value]!' => '',
				],
			]
		);

		$this->add_responsive_control(
			'button_icon_size',
			[
				'label' => esc_html__( 'Size', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 6, 'max' => 100 ] ],
				'selectors' => [
					'{{WRAPPER}} .sifency-button-icon i' => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-button-icon svg' => 'width: {{SIZE}}{{UNIT}}; height: auto;',
				],
			]
		);

		$this->add_responsive_control(
			'button_icon_spacing',
			[
				'label' => esc_html__( 'Spacing', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors' => [
					'{{WRAPPER}} .sifency-button-icon--position-before' => 'margin-right: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .sifency-button-icon--position-after' => 'margin-left: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->start_controls_tabs( 'button_icon_style_tabs' );
			$this->start_controls_tab( 'button_icon_normal_tab', [ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ] );
			$this->add_control(
				'button_icon_color',
				[
					'label' => esc_html__( 'Color', 'sifency-addons' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .sifency-button-icon' => 'color: {{VALUE}};',
						'{{WRAPPER}} .sifency-button-icon svg' => 'fill: {{VALUE}};',
					],
				]
			);
			$this->end_controls_tab();

			$this->start_controls_tab( 'button_icon_hover_tab', [ 'label' => esc_html__( 'Hover', 'sifency-addons' ) ] );
			$this->add_control(
				'button_icon_hover_color',
				[
					'label' => esc_html__( 'Color', 'sifency-addons' ),
					'type' => Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .sifency-button-style:hover .sifency-button-icon' => 'color: {{VALUE}};',
						'{{WRAPPER}} .sifency-button-style:hover .sifency-button-icon svg' => 'fill: {{VALUE}};',
					],
				]
			);
			$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->end_controls_section();
	}
}