<?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_Background;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Combine_Button_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Banner extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Combine_Button_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency_banner',
			'title'         => 'Promo Banner',
			'icon'          => 'eicon-banner',
			'category'      => array( 'sifency-business-widgets' ),
			'keywords'      => array( 'banner', 'promo', 'call to action', 'advertisement', 'sale', 'offer' ),
			'style_depends' => array( 'sifency-banner-style', 'sifency-typo-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content_layout',
			array(
				'label' => esc_html__( 'Layout & Image', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'banner_style',
			array(
				'label'        => esc_html__( 'Banner Style', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'style-1',
				'options'      => array(
					'style-1' => esc_html__( 'Style 1: Classic Zoom (Center)', 'sifency-addons' ),
					'style-2' => esc_html__( 'Style 2: Slide-Up Info (Minimal)', 'sifency-addons' ),
					'style-3' => esc_html__( 'Style 3: Border Frame Reveal', 'sifency-addons' ),
					'style-4' => esc_html__( 'Style 4: Side Content (Left)', 'sifency-addons' ),
					'style-5' => esc_html__( 'Style 5: Glassmorphism Card', 'sifency-addons' ),
				),
				'prefix_class' => 'sifency-banner-',
			)
		);
		$this->add_control(
			'banner_image',
			array(
				'label'   => esc_html__( 'Banner Image', 'sifency-addons' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array(
					'url' => Utils::get_placeholder_image_src(),
				),
			)
		);
		$this->add_responsive_control(
			'banner_height',
			array(
				'label'     => esc_html__( 'Height', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 200,
						'max' => 800,
					),
				),
				'default'   => array(
					'size' => 400,
					'unit' => 'px',
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-banner-wrapper' => 'height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		// Link
		$this->add_control(
			'link',
			array(
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => 'https://your-link.com',
				'separator'   => 'before',
			)
		);
		$this->add_control(
			'apply_link_on',
			array(
				'label'     => esc_html__( 'Apply Link On', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'box'    => 'Whole Banner',
					'button' => 'Button Only',
				),
				'default'   => 'button',
				'condition' => array( 'link[url]!' => '' ),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_content_texts',
			array(
				'label' => esc_html__( 'Content', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'badge_text',
			array(
				'label'       => esc_html__( 'Badge / Tag', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'NEW ARRIVAL',
			)
		);
		$this->add_control(
			'subtitle',
			array(
				'label'   => esc_html__( 'Subtitle', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Best Collection',
			)
		);
		$this->add_control(
			'title',
			array(
				'label'   => esc_html__( 'Title', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => 'Summer Sale',
			)
		);
		$this->add_control(
			'description',
			array(
				'label'     => esc_html__( 'Description', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'default'   => 'Up to 50% Off selected items.',
				'condition' => array( 'banner_style!' => 'style-3' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_combine_button_content_controls(
			array(
				'prefix' => 'primary_btn',
				'label'  => 'Primary Button',
			)
		);
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style_general',
			array(
				'label' => esc_html__( 'General & Overlay', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'overlay_color',
			array(
				'label'     => esc_html__( 'Overlay Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => 'rgba(0,0,0,0.3)',
				'selectors' => array(
					'{{WRAPPER}} .sifency-banner-overlay' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'overlay_hover_color',
			array(
				'label'     => esc_html__( 'Hover Overlay Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-banner-wrapper:hover .sifency-banner-overlay' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-banner-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'item_wrapper_',
				'label'    => 'Banner Wrapper',
				'selector' => '.sifency-banner-wrapper',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'content_wrapper_',
				'label'    => 'Content Container',
				'selector' => '.sifency-banner-inner',
				'exclude'  => array( 'transition', 'tabs' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'content_wrapper_inner_',
				'label'    => 'Content Container',
				'selector' => '.sifency-banner-inner',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'badge_style',
				'label'     => 'Badge',
				'selector'  => '.sifency-label-style',
				'condition' => array( 'badge_text!' => '' ),
				'exclude'   => array( 'alignment', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'subtitle_style',
				'label'     => 'Subtitle',
				'selector'  => '.sifency-meta-style',
				'condition' => array( 'subtitle!' => '' ),
				'exclude'   => array( 'alignment', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'heading_style',
				'label'     => 'Title',
				'selector'  => '.sifency-heading-style',
				'condition' => array( 'title!' => '' ),
				'exclude'   => array( 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'description_style',
				'label'          => 'Description',
				'selector'       => '.sifency-content-style',
				'color_only'     => true,
				'show_max_width' => true,
				'condition'      => array( 'description!' => '' ),
				'exclude'        => array( 'alignment', 'transition', 'tabs', 'bg', 'radius' ),
			)
		);
		$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();
		$has_link           = ! empty( $settings['link']['url'] );
		$link_wrapper_start = '';
		$link_wrapper_end   = '';
		$button_tag         = 'div';
		$button_attrs       = 'class="sifency-banner-btn"';

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

			if ( 'box' === $settings['apply_link_on'] ) {
				$link_wrapper_start = '<a ' . $this->get_render_attribute_string( 'banner_link' ) . ' class="sifency-banner-link-overlay"></a>';
			} else {
				$button_tag   = 'a';
				$button_attrs = 'class="sifency-banner-btn sifency-button-style" ' . $this->get_render_attribute_string( 'banner_link' );
			}
		}
		?>

		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-banner-wrapper">
			<?php echo wp_kses_post( $link_wrapper_start ); ?>
			<div class="sifency-banner-bg" style="background-image: url('<?php echo esc_url( $settings['banner_image']['url'] ); ?>');"></div>
			<div class="sifency-banner-overlay"></div>

			<div class="sifency-banner-content">
				<div class="sifency-banner-inner">
					
					<?php if ( ! empty( $settings['badge_text'] ) ) : ?>
						<span class="sifency-banner-badge sifency-label-style"><?php echo esc_html( $settings['badge_text'] ); ?></span>
					<?php endif; ?>

					<?php if ( ! empty( $settings['subtitle'] ) ) : ?>
						<h4 class="sifency-banner-subtitle sifency-meta-style"><?php echo esc_html( $settings['subtitle'] ); ?></h4>
					<?php endif; ?>

					<?php if ( ! empty( $settings['title'] ) ) : ?>
						<h2 class="sifency-banner-title sifency-heading-style"><?php echo wp_kses_post( nl2br( $settings['title'] ) ); ?></h2>
					<?php endif; ?>

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

					<?php $this->add_sifency_addons_combine_button_render( $settings, 'primary_btn', 'sifency-addons-primary-button' ); ?>
				</div>
			</div>

		</div>
		<?php
	}
}
