<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Border;
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_Master_Typography;
use Sifency\Traits\Sifency_Addons_Combine_Button_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls; 
use Sifency\Traits\Sifency_Addons_Image_Style_Controls; 
use Sifency\Traits\Sifency_Addons_Help_Controls;  
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Call_To_Action extends Sifency_Addons_Widget_Base {
    use Sifency_Addons_Animation_Controls;
    use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Combine_Button_Controls;
    use Sifency_Addons_Item_Style_Controls;
    use Sifency_Addons_Flex_Controls;
    use Sifency_Addons_Image_Style_Controls;
    use Sifency_Addons_Help_Controls;
	public function __construct( $data = [], $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = [
        'name'       => 'sifency_cta',
        'title'      => 'Sifency Call to Action',
        'icon'       => 'eicon-call-to-action',
        'category' => [ 'sifency-business-widgets' ],
        'style_depends'     => [ 'sifency-cta-style', 'sifency-typo-style', 'sifency-button-style', 'sifency-icon' ],
    ];}
	protected function register_controls() {
		$this->start_controls_section(
			'section_layout',
			[ 'label' => esc_html__( 'Layout & Skin', 'sifency-addons' ) ]
		);
		$this->add_control(
			'skin',
			[
				'label'   => esc_html__( 'Skin', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'classic',
				'options' => [
					'classic'    => 'Classic (Side/Top)',
					// 'cover'      => 'Cover (Background)',
					// 'overlap'    => 'Overlap (Floating)',
					// 'horizoncta' => 'Horizontal',
					// 'inline'     => 'Inline (Content & Button)',
				],
				'prefix_class' => 'sifency-cta-skin-',
			]
		);
		$this->add_control(
			'inline_reverse',
			[
				'label' => esc_html__( 'Reverse Direction', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => 'Yes',
				'label_off' => 'No',
				'return_value' => 'yes',
				'default' => '',
				'prefix_class' => 'sifency-cta-inline-reverse-',
				'condition' => [
					'skin' => 'inline',
				],
			]
		);
		$this->add_responsive_control(
			'inline_gap',
			[
				'label' => esc_html__( 'Content & Button Gap', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em', '%' ],
				'range' => [
					'px' => [ 'min' => 0, 'max' => 100 ],
				],
				'selectors' => [
					'{{WRAPPER}}.sifency-cta-skin-inline .sifency-cta-content-box' => 'gap: {{SIZE}}{{UNIT}};',
				],
				'condition' => [
					'skin' => 'inline',
				],
			]
		);
		$this->add_control(
			'image_position',
			[
				'label'   => esc_html__( 'Image Position', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'default' => 'right',
				'options' => [
					'left'  => [ 'title' => 'Left', 'icon' => 'eicon-h-align-left' ],
					'top'   => [ 'title' => 'Top', 'icon' => 'eicon-v-align-top' ],
					'right' => [ 'title' => 'Right', 'icon' => 'eicon-h-align-right' ],
				],
				'condition' => [ 'skin!' => 'cover' ],
				'prefix_class' => 'sifency-cta-pos-',
			]
		);
		$this->add_responsive_control(
			'content_height',
			[
				'label' => esc_html__( 'Min Height', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 100, 'max' => 800 ] ],
				'selectors' => [ '{{WRAPPER}} .sifency-cta-wrapper' => 'min-height: {{SIZE}}px;' ],
				'condition' => [ 'skin' => 'cover' ],
			]
		);
		$this->add_responsive_control(
			'container_width',
			[
				'label' => esc_html__( 'Content Box Max Width', 'sifency-addons' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%', 'vw', 'custom' ],
				'range' => [
					'%' => [
						'min' => 0,
						'max' => 100,
					],
					'px' => [
						'min' => 0,
						'max' => 1200,
					],
					'vw' => [
						'min' => 0,
						'max' => 100,
					],
				],
				'default' => [
					'unit' => '%',
					'size' => 100,
				],
				'selectors' => [
					'{{WRAPPER}} .sifency-cta-content-box' => 'width: {{SIZE}}{{UNIT}}; max-width: {{SIZE}}{{UNIT}}',
				],
			]
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_content',
			[ 'label' => esc_html__( 'Content', 'sifency-addons' ) ]
		);
		$this->add_control(
			'cta_image',
			[
				'label' => esc_html__( 'Image', 'sifency-addons' ),
				'type' => Controls_Manager::MEDIA,
			]
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			[
				'name' => 'cta_image_size',
				'default' => 'large',
				'condition' => [ 'skin!' => 'cover' ],
			]
		);
		$this->add_control( 'subtitle', [
			'label' => 'Subtitle',
			'type' => Controls_Manager::TEXTAREA,
		]);

		$this->add_control( 'title', [
			'label' => 'Title',
			'type' => Controls_Manager::TEXTAREA,
			'default' => 'Get Started Today',
			'label_block' => true,
		]);

		$this->add_control( 'description', [
			'label' => 'Description',
			'type' => Controls_Manager::TEXTAREA,
			'default' => 'Grow your business with our modern solutions. Click below to learn more.',
		]);

		$this->add_control( 'show_ribbon', [
			'label' => 'Show Ribbon',
			'type' => Controls_Manager::SWITCHER,
			'separator' => 'before',
		]);
		$this->add_control( 'ribbon_text', [
			'label' => 'Ribbon Text',
			'type' => Controls_Manager::TEXTAREA,
			'default' => 'SALE',
			'condition' => [ 'show_ribbon' => 'yes' ],
		]);

		$this->end_controls_section();
		$this->add_sifency_addons_combine_button_content_controls([ 'prefix' => 'primary_btn', 'label' => 'Primary Button' ]);
		
        $this->start_controls_section(
            'alignment_layout',
            [ 'label' => esc_html__( 'Alignment', 'sifency-addons' ) ] 
        );
        $this->add_sifency_addons_flex_alignment_controls( [
			'prefix'    => 'wrapper_flex_',
			'label'     => esc_html__( 'Wrapper Layout', 'sifency-addons' ),
			'selector'  => '.sifency-cta-wrapper',
			'condition' => [ 'cta_image!' => '' ],
        ]);
        $this->add_sifency_addons_flex_alignment_controls( [
			'prefix'    => 'flex_',
			'label'     => esc_html__( 'Flex Layout', 'sifency-addons' ),
			'selector'  => '.sifency-cta-content-box',
        ]);
        $this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls([
            'prefix'   => 'wrapper_style',
            'label'    => 'Wrapper',
            'selector' => '.sifency-cta-wrapper',
        ]);
		$this->add_sifency_addons_master_typography_controls([
            'prefix'   => 'label_style',
            'label'    => 'Subtitle',
            'selector' => '.sifency-label-style',
			'exclude'         => ['shadow','stroke','transition', 'tabs', 'bg', 'radius' ],
			'condition' => [ 'subtitle!' => '' ],
        ]);
        $this->add_sifency_addons_master_typography_controls([
            'prefix'   => 'heading_style',
            'label'    => 'Heading',
            'selector' => '.sifency-heading-style',
			'show_max_width' => true,
			'exclude'         => ['shadow','stroke','transition', 'tabs', 'bg', 'radius' ],
			'condition' => [ 'title!' => '' ],
        ]);
        $this->add_sifency_addons_master_typography_controls([
            'prefix'   => 'content_style',
            'label'    => 'Description',
            'selector' => '.sifency-content-style',
			'show_max_width' => true,
			'color_only'=> true,
			'exclude'         => ['shadow','stroke','transition', 'tabs', 'bg', 'radius' ],
			'condition' => [ 'description!' => '' ],
        ]);
		$this->add_Sifency_Addons_Combine_Button_Controls([ 'prefix' => 'primary_btn', 'selector' => '.sifency-addons-primary-button' ]);
        $this->add_sifency_addons_image_style_controls([
            'prefix'   => 'image_style_',
            'label'    => 'Image',
            'selector' => '.sifency-cta-wrapper img',
			'selectors' => [
				'image'=> '.sifency-cta-wrapper img',
				'container'=> '.sifency-cta-wrapper img',
				'dimensions'=> '.sifency-cta-wrapper img',
			],
			'condition' => [ 'cta_image[url]!' => '' ],
        ]);
        $this->add_sifency_addons_master_typography_controls([
            'prefix'   => 'caption_style',
            'label'    => 'Ribbon Text',
            'selector' => '.sifency-cta-ribbon',
			'color_only'      => true,
			'show_max_width'  => true,
			'show_rotate'     => true,
			'exclude'         => ['alignment', 'transition', 'tabs'],
			'condition' => [ 'show_ribbon' => 'yes' ],
        ]);
		$this->add_sifency_addons_item_style_controls([
            'prefix'   => 'header_style',
            'label'    => 'Content Area',
            'selector' => '.sifency-cta-content-box',
        ]);
		$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();
		$skin = $settings['skin'];
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_attrs = $animation_manager->get_wrapper_attributes_array();
		if ( ! empty( $wrapper_attrs ) ) {
			$this->add_render_attribute( 'sifency_cta_wrapper', $wrapper_attrs );
		}
		$this->add_render_attribute( 'sifency_cta_wrapper', 'class', 'sifency-cta-content-box' );

		if ( ! empty( $settings['subtitle'] ) ) {
			$subtitle_attrs = $animation_manager->get_item_attributes_array();
			if ( ! empty( $subtitle_attrs ) ) {
				$this->add_render_attribute( 'sifency_cta_subtitle', $subtitle_attrs );
			}
			$this->add_render_attribute( 'sifency_cta_subtitle', 'class', ['sifency-cta-subtitle', 'sifency-label-style'] );
		}

		if ( ! empty( $settings['title'] ) ) {
			$title_attrs = $animation_manager->get_item_attributes_array();
			if ( ! empty( $title_attrs ) ) {
				$this->add_render_attribute( 'sifency_cta_title', $title_attrs );
			}
			$this->add_render_attribute( 'sifency_cta_title', 'class', ['sifency-cta-title', 'sifency-heading-style'] );
		}

		if ( ! empty( $settings['description'] ) ) {
			$desc_attrs = $animation_manager->get_item_attributes_array();
			if ( ! empty( $desc_attrs ) ) {
				$this->add_render_attribute( 'sifency_cta_desc', $desc_attrs );
			}
			$this->add_render_attribute( 'sifency_cta_desc', 'class', ['sifency-cta-desc', 'sifency-content-style'] );
		}

		$btn_attrs = $animation_manager->get_item_attributes_array();
		if ( ! empty( $btn_attrs ) ) {
			$this->add_render_attribute( 'sifency_cta_btn_wrapper', $btn_attrs );
		}
		$this->add_render_attribute( 'sifency_cta_btn_wrapper', 'class', 'sifency-cta-btn-wrapper' );
		?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-cta-wrapper">
				<?php 
				if ( 'cover' === $skin ) {
					
					echo '<div class="sifency-cta-image-box"><div class="sifency-cta-bg-cover" style="background-image: url(' . esc_url( $settings['cta_image']['url'] ) . ')"></div>';
					echo '<div class="sifency-cta-overlay"></div></div>';
				} else {
					echo wp_kses_post(
						\Elementor\Group_Control_Image_Size::get_attachment_image_html(
							$settings,
							'cta_image_size',
							'cta_image'
						)
					);
				}
				?>

			<?php if ( 'yes' === $settings['show_ribbon'] ) : ?>
				<div class="sifency-cta-ribbon"><span><?php echo esc_html( $settings['ribbon_text'] ); ?></span></div>
			<?php endif; ?>

			<div <?php $this->print_render_attribute_string( 'sifency_cta_wrapper' ); ?>>

				<div class="cta-left-content">

				<?php if ( $settings['subtitle'] ) : ?>
					<span <?php $this->print_render_attribute_string( 'sifency_cta_subtitle' ); ?>><?php echo esc_html( $settings['subtitle'] ); ?></span>
				<?php endif; ?>

				<?php if ( $settings['title'] ) : ?>
					<h2 <?php $this->print_render_attribute_string( 'sifency_cta_title' ); ?>><?php echo esc_html( $settings['title'] ); ?></h2>
				<?php endif; ?>

				<?php if ( $settings['description'] ) : ?>
					<p <?php $this->print_render_attribute_string( 'sifency_cta_desc' ); ?>><?php echo esc_html( $settings['description'] ); ?></p>
				<?php endif; ?>

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

		</div>
		<?php
	}
}