<?php
class TFTitle_Section_Widget extends \Elementor\Widget_Base {

	public function get_name() {
        return 'tf-title-section';
    }
    
    public function get_title() {
        return esc_html__( 'TF Title Section', 'norstar-core' );
    }

    public function get_icon() {
        return 'eicon-t-letter';
    }
    
    public function get_categories() {
        return [ 'themesflat_addons' ];
    }

    public function get_style_depends() {
		return ['tf-title-section'];
	}

	public function get_script_depends() {
		return ['split', 'tf-title'];
	}

	protected function register_controls() {
		// Start Tab Setting        
			$this->start_controls_section( 'section_tabs',
	            [
	                'label' => esc_html__('Title Section', 'norstar-core'),
	            ]
	        );	 
			
			$this->add_control( 
				'style',
				[
					'label' => esc_html__( 'Styles', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SELECT,
					'default' => 'style1',
					'options' => [
						'style1' => esc_html__( 'Style 1', 'norstar-core' ),
						'style2' => esc_html__( 'Style 2', 'norstar-core' ),
					],
				]
			);	

			$this->add_control( 
				'animation_heading',
				[
					'label' => esc_html__( 'Enable Animation Heading', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SWITCHER,
					'label_on' => esc_html__( 'Enable', 'norstar-core' ),
					'label_off' => esc_html__( 'Disable', 'norstar-core' ),
					'return_value' => 'yes',
					'default' => 'yes',
				]
			);

			$this->add_control( 
	        	'html_tag',
				[
					'label' => esc_html__( 'HTML Tag', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SELECT,
					'default' => 'h3',
					'options' => [
						'h1' => esc_html__( 'H1', 'norstar-core' ),
						'h2' => esc_html__( 'H2', 'norstar-core' ),
						'h3' => esc_html__( 'H3', 'norstar-core' ),
						'h4' => esc_html__( 'H4', 'norstar-core' ),
						'h5' => esc_html__( 'H5', 'norstar-core' ),
						'h6' => esc_html__( 'H6', 'norstar-core' ),
						'span' => esc_html__( 'span', 'norstar-core' ),
						'p' => esc_html__( 'p', 'norstar-core' ),
						'div' => esc_html__( 'div', 'norstar-core' ),
					],
				]
			);		

			$this->add_control(
				'sub_title',
				[
					'label' => esc_html__( 'Sub Title', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::TEXT,
					'default' => esc_html__( 'Services', 'norstar-core' ),
					'label_block' => true,
				]
			);			

			$this->add_control(
				'heading',
				[
					'label' => esc_html__( 'Heading', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::TEXT,					
					'default' => esc_html__( 'A highly trusted wealth management firm', 'norstar-core' ),
					'label_block' => true,
				]
			);

			$this->add_control(
				'description',
				[
					'label' => esc_html__( 'Description', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::WYSIWYG,					
					'default' => esc_html__( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate libero et velit interdum, ac aliquet odio mattis.', 'norstar-core' ),
					'label_block' => true,
				]
			);	

			$this->add_responsive_control(
				'align',
				[
					'label' => esc_html__( 'Alignment', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::CHOOSE,
					'default' => 'left',
					'options' => [
						'left'    => [
							'title' => esc_html__( 'Left', 'norstar-core' ),
							'icon' => 'eicon-text-align-left',
						],
						'center' => [
							'title' => esc_html__( 'Center', 'norstar-core' ),
							'icon' => 'eicon-text-align-center',
						],
						'right' => [
							'title' => esc_html__( 'Right', 'norstar-core' ),
							'icon' => 'eicon-text-align-right',
						]
					],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section' => 'text-align: {{VALUE}}',
					],
				]
			);
	        
			$this->end_controls_section();
        // /.End Tab Setting         

	    // Start Style
	        $this->start_controls_section( 'section_style',
	            [
	                'label' => esc_html__( 'Style', 'norstar-core' ),
	                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
	            ]
	        );	

			$this->add_control(
				'h_sub_title',
				[
					'label' => esc_html__( 'Sub Title', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::HEADING,
					'separator' => 'before',
				]
			);

			$this->add_group_control( 
	        	\Elementor\Group_Control_Typography::get_type(),
				[
					'name' => 'typography_sub_title',
					'label' => esc_html__( 'Typography', 'norstar-core' ),
					'selector' => '{{WRAPPER}} .tf-title-section .title-section .sub-title',
				]
			); 

			$this->add_control( 
				'color_sub_title',
				[
					'label' => esc_html__( 'Color', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .sub-title' => 'color: {{VALUE}}',					
					],
					'condition' => [
						'style' => 'style1',
					],
				]
			);

			$this->add_control( 
				'color_sub_title2',
				[
					'label' => esc_html__( 'Background Color', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .sub-title' => 'background-color: {{VALUE}}',					
					],
				]
			);

			$this->add_responsive_control( 
				'border_sub_title',
				[
					'label' => esc_html__( 'Border Radius', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .sub-title' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->add_group_control( 
				\Elementor\Group_Control_Box_Shadow::get_type(),
				[
					'name' => 'box_shadow_sub_title',
					'label' => esc_html__( 'Box Shadow', 'norstar-core' ),
					'selector' => '{{WRAPPER}} .tf-title-section .title-section .sub-title',
				]
			);

			$this->add_control( 
				'color_sub_title_2',
				[
					'label' => esc_html__( 'Color', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .sub-title' => 'color: {{VALUE}}',					
					],
					'condition' => [
						'style' => 'style2',
					],
				]
			);

			$this->add_responsive_control(
				'padding_sub_title',
				[
					'label' => esc_html__( 'Padding', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .sub-title' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
					'condition' => [
						'style' => ['style1','style2'],
					],
				]
			);

			$this->add_responsive_control(
				'margin_sub_title',
				[
					'label' => esc_html__( 'Margin', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .sub-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
					'condition' => [
						'style' => ['style1','style2'],
					],
				]
			);		

			$this->add_control(
				'h_heading',
				[
					'label' => esc_html__( 'Heading', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::HEADING,
					'separator' => 'before',
				]
			);

			

			$this->add_group_control( 
	        	\Elementor\Group_Control_Typography::get_type(),
				[
					'name' => 'typography',
					'label' => esc_html__( 'Typography', 'norstar-core' ),
					'selector' => '{{WRAPPER}} .tf-title-section .title-section .heading',
				]
			); 

			$this->add_control( 
				'heading_color',
				[
					'label' => esc_html__( 'Color', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .heading' => 'color: {{VALUE}}',					
					],
				]
			);			

			$this->add_control( 
				'heading_color_span',
				[
					'label' => esc_html__( 'Color Span', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .heading span' => 'color: {{VALUE}}',					
					],
				]
			);	

			$this->add_control( 
				'heading_before',
				[
					'label' => esc_html__( 'Line Before', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SELECT,
					'default' => '',
					'options' => [
						'' => esc_html__( 'Hide', 'norstar-core' ),
						'heading-before' => esc_html__( 'Show', 'norstar-core' ),
					],
				]
			);	

			$this->add_control(
				'heading_before_height',
				[
					'label' => esc_html__( 'Height', 'themesflat-addons-for-elementor' ),
					'type' => \Elementor\Controls_Manager::SLIDER,
					'size_units' => [ '%' ],
					'range' => [					
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section.heading-before .title-section .heading::before' => 'height: {{SIZE}}{{UNIT}};',
					],	
					'condition' => [
						'heading_before' => 'heading-before'
					],			
				]
			);

			$this->add_control(
				'heading_before_width',
				[
					'label' => esc_html__( 'width', 'themesflat-addons-for-elementor' ),
					'type' => \Elementor\Controls_Manager::SLIDER,
					'size_units' => [ '%','px' ],
					'range' => [					
						'%' => [
							'min' => 0,
							'max' => 100,
						],
						'px' => [
							'min' => 0,
							'max' => 1000,
						],
					],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section.heading-before .title-section .heading::before' => 'width: {{SIZE}}{{UNIT}};',
					],	
					'condition' => [
						'heading_before' => 'heading-before'
					],			
				]
			);

			$this->add_control( 
				'heading_before_bg',
				[
					'label' => esc_html__( 'Background Line', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .tf-title-section.heading-before .title-section .heading::before' => 'background: {{VALUE}}',					
					],
				]
			);

			$this->add_responsive_control(
				'heading_margin',
				[
					'label' => esc_html__( 'Margin', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .heading' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->add_responsive_control(
				'heading_padding',
				[
					'label' => esc_html__( 'Padding', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .heading' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->add_control(
				'h_description',
				[
					'label' => esc_html__( 'Description', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::HEADING,
					'separator' => 'before',
				]
			);

			

			$this->add_group_control( 
	        	\Elementor\Group_Control_Typography::get_type(),
				[
					'name' => 'typography_desc',
					'label' => esc_html__( 'Typography', 'norstar-core' ),
					'selector' => '{{WRAPPER}} .tf-title-section .title-section .description',
				]
			); 

			$this->add_control( 
				'description_color_desc',
				[
					'label' => esc_html__( 'Color', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .description' => 'color: {{VALUE}}',					
					],
				]
			);			

			$this->add_responsive_control(
				'description_margin_desc',
				[
					'label' => esc_html__( 'Margin', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .description' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->add_responsive_control(
				'description_padding_desc',
				[
					'label' => esc_html__( 'Padding', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .tf-title-section .title-section .description' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			        
        	$this->end_controls_section();    
	    // /.End Style 
	}

	protected function render($instance = []) {
		$settings = $this->get_settings_for_display();

		$this->add_render_attribute( 'tf_title_section', ['id' => "tf-title-section-{$this->get_id()}", 'class' => ['tf-title-section tf-split-text', $settings['style'], $settings['heading_before']], 'data-tabid' => $this->get_id()] );

		$animation = ! empty( $settings['hover_animation'] ) ? 'elementor-animation-' . esc_attr( $settings['hover_animation'] . ' inline-block' ) : '';

		$animation_heading = $settings['animation_heading'] == 'yes' ? 'data-splitting' : '';

		$heading = $description = $sub_title = '';		

		if ($settings['sub_title'] != '') {
			$sub_title = sprintf( '<div class="sub-title">%1$s</div>', $settings['sub_title'] );
		}

		if ($settings['heading'] != '') {
			$heading = sprintf( '<%1$s class="heading" %3$s>%2$s</%1$s>',$settings['html_tag'], $settings['heading'], $animation_heading );
		}		

		if ($settings['description'] != '') {
			$description = sprintf( '<div class="description">%1$s</div>', $settings['description'] );
		}
		

		if ($settings['style'] == 'style2') {
			$content = sprintf( '
				<div class="title-section">
					<div class="content-left">
						%1$s
						%2$s
					</div>
					<div class="content-right">
						%3$s	
					</div>
				</div>' , $sub_title, $heading, $description );
		}else {
			$content = sprintf( '
				<div class="title-section">
					%1$s
					%2$s
					%3$s
				</div>' , $sub_title, $heading, $description );
		}

		echo sprintf ( 
			'<div %1$s> 
				%2$s                
            </div>',
            $this->get_render_attribute_string('tf_title_section'),
            $content
        );	
		
	}

}

