<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
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\Group_Control_Background;
use Elementor\Group_Control_Image_Size;
use Elementor\Icons_Manager;
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_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Combine_Button_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Flip_Box extends Sifency_Addons_Widget_Base {

	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Combine_Button_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-flip-box',
			'title'          => 'Sifency Flip Box',
			'icon'           => 'eicon-flip-box',
			'category'       => array( 'sifency-business-widgets' ),
			'script_depends' => array( 'sifency-flip-box-script' ),
			'style_depends'  => array( 'sifency-flip-box-style', 'sifency-icons' ),
		);}

	protected function register_controls() {

		// --- CONFIG SECTION ---
		$this->start_controls_section(
			'section_settings',
			array(
				'label' => esc_html__( 'Settings & Effect', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'effect_type',
			array(
				'label'   => esc_html__( 'Animation Effect', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'flip',
				'options' => array(
					'flip'    => esc_html__( '3D Flip', 'sifency-addons' ),
					'slide'   => esc_html__( 'Slide', 'sifency-addons' ),
					'push'    => esc_html__( 'Push', 'sifency-addons' ),
					'zoom-in' => esc_html__( 'Zoom In', 'sifency-addons' ),
					'fade'    => esc_html__( 'Fade', 'sifency-addons' ),
					'overlay' => esc_html__( 'Overlay Reveal (Icon Click)', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'direction',
			array(
				'label'     => esc_html__( 'Direction', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'left',
				'options'   => array(
					'left'   => esc_html__( 'Left', 'sifency-addons' ),
					'right'  => esc_html__( 'Right', 'sifency-addons' ),
					'top'    => esc_html__( 'Top', 'sifency-addons' ),
					'bottom' => esc_html__( 'Bottom', 'sifency-addons' ),
				),
				'condition' => array( 'effect_type' => array( 'flip', 'slide', 'push', 'overlay' ) ),
			)
		);
		$this->add_control(
			'trigger_type',
			array(
				'label'     => esc_html__( 'Trigger By', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'hover',
				'options'   => array(
					'hover' => esc_html__( 'Hover', 'sifency-addons' ),
					'click' => esc_html__( 'Click Box', 'sifency-addons' ),
				),
				'condition' => array( 'effect_type!' => 'overlay' ),
			)
		);
		$this->add_control(
			'3d_depth',
			array(
				'label'     => esc_html__( '3D Depth', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'effect_type' => 'flip' ),
			)
		);
		$this->add_control(
			'height',
			array(
				'label'     => esc_html__( 'Height (px)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 200,
						'max' => 1000,
					),
				),
				'default'   => array( 'size' => 300 ),
				'selectors' => array( '{{WRAPPER}} .sifency-fb-wrapper' => 'height: {{SIZE}}px;' ),
			)
		);
		$this->end_controls_section();

		// --- FRONT CONTENT ---
		$this->start_controls_section( 'section_front', array( 'label' => esc_html__( 'Front Content', 'sifency-addons' ) ) );

		$this->add_control(
			'front_icon',
			array(
				'label' => 'Icon',
				'type'  => Controls_Manager::ICONS,
			)
		);
		$this->add_control(
			'front_title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Front Title',
			)
		);
		$this->add_control(
			'front_desc',
			array(
				'label'   => 'Description',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Hover me to see details.',
			)
		);

		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'    => 'front_content_flex_',
				'label'     => esc_html__( 'Front Content Alignment', 'sifency-addons' ),
				'selector'  => '.sifency-fb-front',
				'condition' => array(),
			)
		);
		$this->end_controls_section();

		// --- BACK CONTENT ---
		$this->start_controls_section( 'section_back', array( 'label' => esc_html__( 'Back / Overlay Content', 'sifency-addons' ) ) );
		$this->add_control(
			'back_icon',
			array(
				'label' => 'Icon',
				'type'  => Controls_Manager::ICONS,
			)
		);
		$this->add_control(
			'back_title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Back Title',
			)
		);
		$this->add_control(
			'back_desc',
			array(
				'label'   => 'Description',
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Here is the revealed content with more details.',
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'    => 'back_content_flex_',
				'label'     => esc_html__( 'Back Content Alignment', 'sifency-addons' ),
				'selector'  => '.sifency-fb-back',
				'condition' => array(),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_combine_button_content_controls(
			array(
				'prefix' => 'primary_btn',
				'label'  => 'Back Content Button',
			)
		);
		$this->add_sifency_addons_help_controls();
		// --- OVERLAY ICON (Special Mode) ---
		$this->start_controls_section(
			'section_overlay_icon',
			array(
				'label'     => esc_html__( 'Overlay Trigger Icon', 'sifency-addons' ),
				'condition' => array( 'effect_type' => 'overlay' ),
			)
		);

		$this->add_control(
			'trigger_icon',
			array(
				'label'   => 'Open Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-plus',
					'library' => 'sifency-icons',
				),
			)
		);
		$this->add_control(
			'close_icon',
			array(
				'label'   => 'Close Icon',
				'type'    => Controls_Manager::ICONS,
				'default' => array(
					'value'   => 'icon-minus',
					'library' => 'sifency-icons',
				),
			)
		);

		$this->add_control(
			'trigger_pos_v',
			array(
				'label'   => 'Vertical Position',
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'top'    => array(
						'title' => 'Top',
						'icon'  => 'eicon-v-align-top',
					),
					'bottom' => array(
						'title' => 'Bottom',
						'icon'  => 'eicon-v-align-bottom',
					),
				),
				'default' => 'bottom',
				'toggle'  => false,
			)
		);
		$this->add_control(
			'trigger_pos_h',
			array(
				'label'   => 'Horizontal Position',
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'left'  => array(
						'title' => 'Left',
						'icon'  => 'eicon-h-align-left',
					),
					'right' => array(
						'title' => 'Right',
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default' => 'right',
				'toggle'  => false,
			)
		);

		$this->add_control(
			'overlay_icon_color',
			array(
				'label'     => 'Icon Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-fb-overlay-trigger, {{WRAPPER}} .sifency-fb-overlay-close' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'overlay_icon_bg',
			array(
				'label'     => 'Background Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-fb-overlay-trigger, {{WRAPPER}} .sifency-fb-overlay-close' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'overlay_icon_size',
			array(
				'label'     => 'Icon Size',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 10,
						'max' => 60,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-fb-overlay-trigger, {{WRAPPER}} .sifency-fb-overlay-close' => 'font-size: {{SIZE}}px;' ),
			)
		);
		$this->add_control(
			'overlay_icon_box_size',
			array(
				'label'     => 'Box Size',
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 20,
						'max' => 100,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-fb-overlay-trigger, {{WRAPPER}} .sifency-fb-overlay-close' => 'width: {{SIZE}}px; height: {{SIZE}}px;' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'front_style_',
				'label'    => 'Front Wrapper',
				'selector' => '.sifency-fb-front',
				'exclude'  => array( 'dimensions', 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'front_content_wrapper_',
				'label'    => 'Front Content Wrapper',
				'selector' => '.sifency-fb-front .sifency-fb-content',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'back_style_',
				'label'    => 'Back Wrapper',
				'selector' => '.sifency-fb-back',
				'exclude'  => array( 'dimensions', 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'back_content_wrapper_',
				'label'    => 'Back Content Wrapper',
				'selector' => '.sifency-fb-back .sifency-fb-content',
				'exclude'  => array( 'tabs', 'transition', 'shadow', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'front_heading_style_',
				'label'     => 'Front Title Style',
				'selector'  => '.sifency-heading-style',
				'exclude'   => array( 'tabs', 'transition', 'shadow', 'bg', 'radius' ),
				'condition' => array( 'front_title!' => '' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'front_content_style_',
				'label'          => 'Front Content Style',
				'selector'       => '.sifency-content-style',
				'color_only'     => true,
				'show_max_width' => true,
				'exclude'        => array( 'tabs', 'transition', 'shadow', 'bg', 'radius' ),
				'condition'      => array( 'front_desc!' => '' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'front_icon_style_',
				'label'     => 'Front Icon Style',
				'selector'  => '.sifency-fb-front .sifency-fb-icon',
				'condition' => array( 'front_icon[value]!' => '' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'back_icon_style_',
				'label'     => 'Back Icon Style',
				'selector'  => '.sifency-fb-back .sifency-fb-icon',
				'condition' => array( 'back_icon[value]!' => '' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'back_heading_style_',
				'label'     => 'Back Title Style',
				'selector'  => '.sifency-label-style',
				'exclude'   => array( 'tabs', 'transition', 'shadow', 'bg', 'radius' ),
				'condition' => array( 'back_title!' => '' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'back_content_style_',
				'label'          => 'Back Content Style',
				'selector'       => '.sifency-meta-style',
				'show_max_width' => true,
				'color_only'     => true,
				'exclude'        => array( 'tabs', 'transition', 'shadow', 'bg', 'radius' ),
				'condition'      => array( 'back_desc!' => '' ),
			)
		);
		$this->add_Sifency_Addons_Combine_Button_Controls(
			array(
				'prefix'   => 'primary_btn',
				'selector' => '.sifency-addons-primary-button',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-fb-wrapper' );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-fb-effect-' . $settings['effect_type'] );
		$this->add_render_attribute( 'wrapper', 'class', 'sifency-fb-dir-' . $settings['direction'] );

		if ( 'overlay' !== $settings['effect_type'] ) {
			$this->add_render_attribute( 'wrapper', 'class', 'sifency-fb-trigger-' . $settings['trigger_type'] );
		}

		if ( 'yes' === $settings['3d_depth'] && 'flip' === $settings['effect_type'] ) {
			$this->add_render_attribute( 'wrapper', 'class', 'sifency-fb-3d' );
		}

		if ( 'click' === $settings['trigger_type'] || 'overlay' === $settings['effect_type'] ) {
			$this->add_render_attribute( 'wrapper', 'data-fb-settings', wp_json_encode( array( 'mode' => $settings['effect_type'] === 'overlay' ? 'overlay' : 'click' ) ) );
			$this->add_render_attribute( 'wrapper', 'id', 'sifency-fb-' . $this->get_id() );
		}

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) ); ?>>
			
			<div class="sifency-fb-inner">
				
				<div class="sifency-fb-front">
					<div class="sifency-fb-content">
						<?php if ( ! empty( $settings['front_icon']['value'] ) ) : ?>
							<div class="sifency-fb-icon">
								<?php Icons_Manager::render_icon( $settings['front_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</div>
						<?php endif; ?>
						
						<h3 class="sifency-fb-title sifency-heading-style"><?php echo esc_html( $settings['front_title'] ); ?></h3>
						<div class="sifency-fb-desc sifency-content-style"><?php echo wp_kses_post( $settings['front_desc'] ); ?></div>
					</div>

					<?php if ( 'overlay' === $settings['effect_type'] ) : ?>
						<div class="sifency-fb-overlay-trigger <?php echo esc_attr( $settings['trigger_pos_v'] . ' ' . $settings['trigger_pos_h'] ); ?>">
							<?php Icons_Manager::render_icon( $settings['trigger_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</div>
					<?php endif; ?>
				</div>

				<div class="sifency-fb-back">
					
					<?php if ( 'overlay' === $settings['effect_type'] ) : ?>
						<div class="sifency-fb-overlay-close <?php echo esc_attr( $settings['trigger_pos_v'] . ' ' . $settings['trigger_pos_h'] ); ?>">
							<?php Icons_Manager::render_icon( $settings['close_icon'], array( 'aria-hidden' => 'true' ) ); ?>
						</div>
					<?php endif; ?>

					<div class="sifency-fb-content">

						<?php if ( ! empty( $settings['back_icon']['value'] ) ) : ?>
							<div class="sifency-fb-icon">
								<?php Icons_Manager::render_icon( $settings['back_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</div>
						<?php endif; ?>

						<h3 class="sifency-fb-title sifency-label-style"><?php echo esc_html( $settings['back_title'] ); ?></h3>
						<div class="sifency-fb-desc sifency-meta-style"><?php echo wp_kses_post( $settings['back_desc'] ); ?></div>
						
						<?php $this->add_sifency_addons_combine_button_render( $settings, 'primary_btn', 'sifency-addons-primary-button' ); ?>
					</div>
				</div>

			</div>

		</div>
		<?php
	}
}
