<?php
class TFbutton_Widget extends \Elementor\Widget_Base {

	public function get_name() {
        return 'tf-button';
    }
    
    public function get_title() {
        return esc_html__( 'TF Button', 'norstar-core' );
    }

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

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

	protected function register_controls() {
		// Start List Setting        
			$this->start_controls_section( 'section_setting',
	            [
	                'label' => esc_html__('Setting', 'norstar-core'),
	            ]
	        );

			$this->add_control( 
				'style',
				[
					'label' => esc_html__( 'Style', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SELECT,
					'default' => 'style1',
					'options' => [
						'style1' => esc_html__( 'Style 1', 'norstar-core' ),
						'style2' => esc_html__( 'Style 2', 'norstar-core' ),	
						'style3' => esc_html__( 'Style 3', 'norstar-core' ),	
						'style4' => esc_html__( 'Style 4', 'norstar-core' ),	
					]
				]
			);

			$this->add_control( 
                'button_text',
                [
                    'label' => esc_html__( 'Button Text', 'norstar-core' ),
                    'type' => \Elementor\Controls_Manager::TEXT,
                    'default' => esc_html__( 'Get Started', 'norstar-core' ),
                ]
            );	
            
            $this->add_control(
                'post_icon_readmore',
                [
                    'label' => esc_html__( 'Post Icon ', 'norstar-core' ),
                    'type' => \Elementor\Controls_Manager::ICONS,
                    'default' => [
                        'value' => 'icon-up-right-arrow',
                        'library' => 'theme_icon',
                    ],
                ]
            );	

			$this->add_control( 
	        	'position_icon',
				[
					'label' => esc_html__( 'Position icon', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SELECT,
					'default' => 'after',
					'options' => [
						'before' => esc_html__( 'Before', 'norstar-core' ),
						'after' => esc_html__( 'After', 'norstar-core' ),
					],
				]
			);
	        
            $this->add_control(
				'link',
				[
					'label' => esc_html__( 'Link', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::URL,
					'placeholder' => esc_html__( 'https://your-link.com', 'norstar-core' ),
					'default' => [
						'url' => '#',
						'is_external' => false,
						'nofollow' => false,
					],
				]
			);
	        
			$this->end_controls_section();
        // /.End List Setting  

	    // Start Style Style
			$this->start_controls_section(
				'section_style',
				[
					'label' => esc_html__( 'Style', 'norstar-core' ),
					'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
				]
			);
			$this->add_control(
				'h_general',
				[
					'label' => esc_html__( 'General', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::HEADING,
					'separator' => 'before',
				]
			);
			$this->add_responsive_control(
				'align_btn',
				[
					'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}} .wrap-tf-button' => 'text-align: {{VALUE}}',
					],
				]
			);
			$this->add_responsive_control( 'padding',
	            [
	                'label' => esc_html__( 'Padding', 'norstar-core' ),
	                'type' => \Elementor\Controls_Manager::DIMENSIONS,
	                'size_units' => ['px', 'em', '%'],
	                'selectors' => [
	                    '{{WRAPPER}} .wrap-tf-button .tf-btn' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
	                ],
	            ]
	        );		
			
			$this->add_responsive_control( 
				'margin',
				[
					'label' => esc_html__( 'Margin', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'selectors' => [
						'{{WRAPPER}} .wrap-tf-button .tf-btn' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->add_group_control(
				\Elementor\Group_Control_Typography::get_type(),
				[
					'label' => esc_html__( 'Typo Button', 'norstar-core' ),
					'name' => 'typography_number_df',
					'selector' => '{{WRAPPER}} .wrap-tf-button .tf-btn',
				]
			);

			$this->add_control( 
	        	'icon_size',
				[
					'label' => esc_html__( 'Icon Size', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SLIDER,
					'size_units' => [ 'px' ],
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 300,
							'step' => 1,
						],
					],
					'selectors' => [
						'{{WRAPPER}} .wrap-tf-button .tf-btn i' => 'font-size: {{SIZE}}{{UNIT}};',
					],
				]
			);

			$this->add_control( 
	        	'icon_size_lh',
				[
					'label' => esc_html__( 'Icon Line Height', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SLIDER,
					'size_units' => [ 'px' ],
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 300,
							'step' => 1,
						],
					],
					'selectors' => [
						'{{WRAPPER}} .wrap-tf-button .tf-btn i' => 'line-height: {{SIZE}}{{UNIT}};',
					],
				]
			);

			$this->add_control( 
	        	'icon_size_mg',
				[
					'label' => esc_html__( 'Icon Gap', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SLIDER,
					'size_units' => [ 'px' ],
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 300,
							'step' => 1,
						],
					],
					'selectors' => [
						'{{WRAPPER}} .wrap-tf-button .tf-btn i' => 'margin-left: {{SIZE}}{{UNIT}};',
					],
					'condition' => [
						'position_icon' => 'after',
					]
				]
			);

			$this->add_control( 
	        	'icon_size_mg_2',
				[
					'label' => esc_html__( 'Icon Gap', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SLIDER,
					'size_units' => [ 'px' ],
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 300,
							'step' => 1,
						],
					],
					'selectors' => [
						'{{WRAPPER}} .wrap-tf-button .tf-btn i' => 'margin-right: {{SIZE}}{{UNIT}};',
					],
					'condition' => [
						'position_icon' => 'before',
					]
				]
			);

			$this->add_control( 
				'vertical_icon',
				[
					'label' => esc_html__( 'Vertical Align Icon', 'norstar-core' ),
					'type' => \Elementor\Controls_Manager::SELECT,
					'default' => 'middle',
					'options' => [
						'bottom' => esc_html__( 'Bottom', 'norstar-core' ),
						'middle' => esc_html__( 'Middle', 'norstar-core' ),
						'sub' => esc_html__( 'Sub', 'norstar-core' ),
						'top' => esc_html__( 'Top', 'norstar-core' ),
						'text-bottom' => esc_html__( 'Text Bottom', 'norstar-core' ),
					],
					'selectors' => [
						'{{WRAPPER}} .wrap-tf-button .tf-btn i' => 'vertical-align: {{VALUE}};',
					],
				]
			);	

			$this->start_controls_tabs( 
				'button_style_tabs' 
				);

	        	$this->start_controls_tab( 'button_style_normal_tab',
					[
						'label' => esc_html__( 'Normal', 'themesflat-core' ),
					] );	

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

					$this->add_control( 
						'bg_content',
						[
							'label' => esc_html__( 'Background', 'norstar-core' ),
							'type' => \Elementor\Controls_Manager::COLOR,
							'selectors' => [
								'{{WRAPPER}} .wrap-tf-button .tf-btn' => 'background: {{VALUE}}',
							],
						]
					);
	        
					$this->add_group_control( 
						\Elementor\Group_Control_Border::get_type(),
						[
							'name' => 'border',
							'label' => esc_html__( 'Border', 'norstar-core' ),
							'selector' => '{{WRAPPER}} .wrap-tf-button .tf-btn',
						]
					);
					$this->add_responsive_control( 
						'border_radius',
						[
							'label' => esc_html__( 'Border Radius', 'norstar-core' ),
							'type' => \Elementor\Controls_Manager::DIMENSIONS,
							'size_units' => [ 'px' , '%' ],
							'selectors' => [
								'{{WRAPPER}} .wrap-tf-button .tf-btn' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
							],
						]
					);
					$this->add_group_control( 
						\Elementor\Group_Control_Box_Shadow::get_type(),
						[
							'name' => 'box_shadow',
							'label' => esc_html__( 'Box Shadow', 'norstar-core' ),
							'selector' => '{{WRAPPER}} .wrap-tf-button .tf-btn',
						]
					);

					$this->add_control( 
						'bg_content_after_ps',
						[
							'label' => esc_html__( 'Position Box After', 'themesflat-core' ),
							'type' => \Elementor\Controls_Manager::SLIDER,
							'size_units' => [ 'px' ],
							'range' => [
								'px' => [
									'min' => -100,
									'max' => 100,
									'step' => 1,
								],
							],
							'selectors' => [
								'{{WRAPPER}} .wrap-tf-button .tf-btn::after' => 'right: {{SIZE}}{{UNIT}};bottom: {{SIZE}}{{UNIT}};',
							],
						]
					);

					$this->add_control( 
						'bg_content_after',
						[
							'label' => esc_html__( 'Background Box After', 'norstar-core' ),
							'type' => \Elementor\Controls_Manager::COLOR,
							'selectors' => [
								'{{WRAPPER}} .wrap-tf-button .tf-btn::after' => 'background: {{VALUE}}',
							],
						]
					);

					
					$this->add_group_control( 
						\Elementor\Group_Control_Border::get_type(),
						[
							'name' => 'border_after',
							'label' => esc_html__( 'Border', 'norstar-core' ),
							'selector' => '{{WRAPPER}} .wrap-tf-button .tf-btn::after',
						]
					);
					
				$this->end_controls_tab();

				$this->start_controls_tab( 'button_style_hover_tab',
					[
						'label' => esc_html__( 'Hover', 'themesflat-core' ),
					] );

					$this->add_control( 
						'color_content_hover',
						[
							'label' => esc_html__( 'Color Hover', 'norstar-core' ),
							'type' => \Elementor\Controls_Manager::COLOR,
							'selectors' => [
								'{{WRAPPER}} .wrap-tf-button .tf-btn:hover' => 'color: {{VALUE}}',
							],
						]
					);
					$this->add_control( 
						'bg_content_hover',
						[
							'label' => esc_html__( 'Background Hover', 'norstar-core' ),
							'type' => \Elementor\Controls_Manager::COLOR,
							'selectors' => [
								'{{WRAPPER}} .wrap-tf-button .tf-btn:hover' => 'background: {{VALUE}}',
							],
						]
					);
					
					$this->add_group_control( 
						\Elementor\Group_Control_Box_Shadow::get_type(),
						[
							'name' => 'box_shadow_hover',
							'label' => esc_html__( 'Box Shadow Hover', 'norstar-core' ),
							'selector' => '{{WRAPPER}} .wrap-tf-button .tf-btn:hover',
						]
					);
					$this->add_control( 
						'style_hover',
						[
							'label' => esc_html__( 'Style Hover', 'norstar-core' ),
							'type' => \Elementor\Controls_Manager::SELECT,
							'default' => '',
							'options' => [
								'' => esc_html__( 'Default', 'norstar-core' ),
								'slide-right' => esc_html__( 'Slide Right', 'norstar-core' ),
								'slide-left' => esc_html__( 'Slide Left', 'norstar-core' ),
								'slide-up' => esc_html__( 'Slide Up', 'norstar-core' ),
								'slide-down' => esc_html__( 'Slide Down', 'norstar-core' ),
							],
						]
					);	
				$this->end_controls_tab();

			$this->end_controls_tabs();

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

	protected function render($instance = []) {
		$settings = $this->get_settings_for_display();
		
		$target = $settings['link']['is_external'] ? ' target="_blank"' : '';
		$nofollow = $settings['link']['nofollow'] ? ' rel="nofollow"' : '';

		$this->add_render_attribute( 'wrap_tf_button', ['id' => "tf-step-{$this->get_id()}", 'class' => ['wrap-tf-button',$settings['style'],$settings['style_hover']], 'data-tabid' => $this->get_id() ] );
		
        ?>
        <div <?php echo $this->get_render_attribute_string('wrap_tf_button') ?>>
			<div class="wrap-inner-tf-button">
				<a href="<?php echo esc_url( $settings['link']['url'] ) ?>" class="tf-btn <?php echo $settings['position_icon']; ?>" <?php echo $target; echo $nofollow; ?>>
					<?php if ( $settings['position_icon'] == 'before' ):?>
							<?php echo \Elementor\Addon_Elementor_Icon_manager_norstar::render_icon( $settings['post_icon_readmore'], [ 'aria-hidden' => 'true' ] );?>
					<?php endif; ?>
					<?php echo esc_attr( $settings['button_text'] ); ?>
					<?php if ( $settings['position_icon'] == 'after' ):?>
							<?php echo \Elementor\Addon_Elementor_Icon_manager_norstar::render_icon( $settings['post_icon_readmore'], [ 'aria-hidden' => 'true' ] );?>
					<?php endif; ?>
				</a>
			</div>
	    </div>
        <?php 		
	}

}