<?php
namespace Sifency\Traits;

if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Background;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
trait Sifency_Addons_Section_Header {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Flex_Controls;

	/**
	 * Register Section Title Controls
	 */
	protected function add_sifency_addons_section_header_controls( $args = array() ) {
		$args = wp_parse_args(
			$args,
			array(
				'section_id' => 'section_header_content',
				'label'      => esc_html__( 'Section Header', 'sifency-addons' ),
			)
		);
		$this->start_controls_section(
			$args['section_id'],
			array(
				'label' => $args['label'],
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'st_enable',
			array(
				'label'        => esc_html__( 'Enable Section Header', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
			)
		);
		$this->add_control(
			'st_layout',
			array(
				'label'        => esc_html__( 'Header Layout', 'sifency-addons' ),
				'type'         => Controls_Manager::SELECT,
				'default'      => 'layout-center',
				'options'      => array(
					'layout-center'      => 'Layout 1: All Center (Default)',
					'layout-split-1'     => 'Layout 2: Left(Cap/Title) | Right(Sub/Btn)',
					'layout-split-2'     => 'Layout 3: Left(All) | Right(Btn)',
					'layout-three-col-1' => 'Layout 4: Left(Title) | Mid(Sub) | Right(Btn)',
					'layout-three-col-2' => 'Layout 5: Left(Title) | Mid(Btn) | Right(Sub)',
					'layout-mixed-1'     => 'Layout 6: Left(Cap/Title/Btn) | Right(Sub)',
				),
				'prefix_class' => 'sifency-st-',
				'condition'    => array( 'st_enable' => 'yes' ),
			)
		);
		$this->add_responsive_control(
			'st_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-st-center' => 'text-align: {{VALUE}};' ),
				'condition' => array(
					'st_enable' => 'yes',
					'st_layout' => array( 'layout-center' ),
				),
			)
		);
		$this->add_control(
			'st_show_caption',
			array(
				'label'     => 'Show Caption',
				'type'      => Controls_Manager::SWITCHER,
				'condition' => array( 'st_enable' => 'yes' ),
			)
		);
		$this->add_control(
			'st_caption',
			array(
				'label'     => 'Caption Text',
				'type'      => Controls_Manager::TEXT,
				'condition' => array(
					'st_enable'       => 'yes',
					'st_show_caption' => 'yes',
				),
			)
		);
		$this->add_control(
			'st_show_title',
			array(
				'label'     => 'Show Title',
				'type'      => Controls_Manager::SWITCHER,
				'condition' => array( 'st_enable' => 'yes' ),
			)
		);
		$this->add_control(
			'st_title',
			array(
				'label'     => 'Title Text',
				'type'      => Controls_Manager::TEXTAREA,
				'condition' => array(
					'st_enable'     => 'yes',
					'st_show_title' => 'yes',
				),
			)
		);
		$this->add_control(
			'st_title_tag',
			array(
				'label'     => 'Tag',
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'h1'  => 'H1',
					'h2'  => 'H2',
					'h3'  => 'H3',
					'h4'  => 'H4',
					'h5'  => 'H5',
					'h6'  => 'H6',
					'div' => 'div',
				),
				'default'   => 'h2',
				'condition' => array(
					'st_enable'     => 'yes',
					'st_show_title' => 'yes',
				),
			)
		);
		$this->add_control(
			'st_show_divider',
			array(
				'label'     => 'Show Divider',
				'type'      => Controls_Manager::SWITCHER,
				'condition' => array( 'st_enable' => 'yes' ),
			)
		);
		$this->add_control(
			'st_show_subtitle',
			array(
				'label'     => 'Show Subtitle',
				'type'      => Controls_Manager::SWITCHER,
				'condition' => array( 'st_enable' => 'yes' ),
			)
		);
		$this->add_control(
			'st_subtitle',
			array(
				'label'     => 'Subtitle Text',
				'type'      => Controls_Manager::WYSIWYG,
				'condition' => array(
					'st_enable'        => 'yes',
					'st_show_subtitle' => 'yes',
				),
			)
		);
		$this->add_control(
			'st_show_btn',
			array(
				'label'     => 'Show Button',
				'type'      => Controls_Manager::SWITCHER,
				'condition' => array( 'st_enable' => 'yes' ),
			)
		);
		$this->add_control(
			'st_btn_text',
			array(
				'label'     => 'Button Text',
				'type'      => Controls_Manager::TEXT,
				'condition' => array(
					'st_enable'   => 'yes',
					'st_show_btn' => 'yes',
				),
			)
		);
		$this->add_control(
			'st_btn_link',
			array(
				'label'     => 'Button Link',
				'type'      => Controls_Manager::URL,
				'condition' => array(
					'st_enable'   => 'yes',
					'st_show_btn' => 'yes',
				),
			)
		);

		// $this->add_sifency_addons_flex_alignment_controls([
		// 'prefix'    => 'section_header_flex_',
		// 'label'     => esc_html__( 'Flex Layout', 'sifency-addons' ),
		// 'selector'  => '',
		// 'condition' => ['st_enable' => 'yes' ],
		// ]);
		$this->end_controls_section();
		$this->start_controls_section(
			'st_section_header',
			array(
				'label'     => esc_html__( 'Header Divider', 'sifency-addons' ),
				'condition' => array(
					'st_enable'    => 'yes',
					'show_divider' => 'yes',
				),
				'tab'       => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'st_heading_div',
			array(
				'label'     => 'Divider',
				'type'      => Controls_Manager::HEADING,
				'condition' => array( 'st_enable' => 'yes' ),
				'condition' => array( 'st_show_divider' => 'yes' ),
			)
		);
		$this->add_responsive_control(
			'st_div_width',
			array(
				'label'      => 'Width',
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array( '{{WRAPPER}} .st-divider' => 'width: {{SIZE}}{{UNIT}};' ),
				'condition'  => array(
					'st_enable'       => 'yes',
					'st_show_divider' => 'yes',
				),
			)
		);
		$this->add_control(
			'st_div_color',
			array(
				'label'     => 'Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .st-divider' => 'background-color: {{VALUE}};' ),
				'condition' => array(
					'st_enable'       => 'yes',
					'st_show_divider' => 'yes',
				),
			)
		);
		$this->add_responsive_control(
			'st_div_height',
			array(
				'label'     => 'Height',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .st-divider' => 'height: {{SIZE}}{{UNIT}};' ),
				'condition' => array(
					'st_enable'       => 'yes',
					'st_show_divider' => 'yes',
				),
			)
		);
		$this->add_responsive_control(
			'st_div_spacing',
			array(
				'label'     => 'Spacing',
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .st-divider' => 'margin-bottom: {{SIZE}}{{UNIT}};' ),
				'condition' => array(
					'st_enable'       => 'yes',
					'st_show_divider' => 'yes',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'    => 'section_header_',
				'label'     => esc_html__( 'Header Container', 'sifency-addons' ),
				'selector'  => '.sifency-st-container',
				'exclude'   => array( 'tabs', 'transition', 'shadow' ),
				'condition' => array( 'st_enable' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'section_header_caption_',
				'label'          => esc_html__( 'Caption', 'sifency-addons' ),
				'selector'       => '.sifency-st-caption',
				'condition'      => array( 'st_show_caption' => 'yes' ),
				'show_max_width' => true,
				'color_only'     => true,
				'exclude'        => array( 'tabs', 'transition', 'shadow' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'section_header_title_',
				'label'          => esc_html__( 'Section Title', 'sifency-addons' ),
				'selector'       => '.sifency-st-title',
				'condition'      => array( 'st_show_title' => 'yes' ),
				'show_max_width' => true,
				'color_only'     => true,
				'exclude'        => array( 'tabs', 'transition', 'shadow', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'sifency-st-subtitle_',
				'label'          => esc_html__( 'Subtitle', 'sifency-addons' ),
				'selector'       => '.sifency-st-subtitle',
				'condition'      => array( 'st_show_subtitle' => 'yes' ),
				'show_max_width' => true,
				'color_only'     => true,
				'exclude'        => array( 'tabs', 'transition', 'shadow', 'bg', 'radius' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'         => 'sifency-st-button_',
				'label'          => esc_html__( 'Header Button', 'sifency-addons' ),
				'selector'       => '.sifency-st-button',
				'hover_selector' => '.sifency-st-container:hover .sifency-st-button',
				'condition'      => array( 'st_show_btn' => 'yes' ),
			)
		);
	}

	protected function render_sifency_section_header_elements( $settings ) {

		$settings = $this->get_settings_for_display();
		if ( empty( $settings['st_enable'] ) || 'yes' !== $settings['st_enable'] ) {
			return;
		}
		$layout        = $settings['st_layout'] ?? 'layout-center';
		$show_caption  = $settings['st_show_caption'] ?? '';
		$show_title    = $settings['st_show_title'] ?? '';
		$show_divider  = $settings['st_show_divider'] ?? '';
		$show_subtitle = $settings['st_show_subtitle'] ?? '';
		$show_btn      = $settings['st_show_btn'] ?? '';

		$st_caption   = $settings['st_caption'] ?? '';
		$st_title     = $settings['st_title'] ?? '';
		$st_title_tag = $settings['st_title_tag'] ?? 'h2';
		$st_subtitle  = $settings['st_subtitle'] ?? '';
		$st_btn_text  = $settings['st_btn_text'] ?? '';
		$st_btn_link  = $settings['st_btn_link'] ?? array();

		$caption  = ( 'yes' === $show_caption && ! empty( $st_caption ) ) ? '<div class="sifency-st-caption">' . wp_kses_post( $st_caption ) . '</div>' : '';
		$title    = ( 'yes' === $show_title && ! empty( $st_title ) ) ? '<' . esc_attr( $st_title_tag ) . ' class="sifency-st-title">' . wp_kses_post( $st_title ) . '</' . esc_attr( $st_title_tag ) . '>' : '';
		$divider  = ( 'yes' === $show_divider ) ? '<div class="sifency-st-divider-wrap"><span class="sifency-st-divider"></span></div>' : '';
		$subtitle = ( 'yes' === $show_subtitle && ! empty( $st_subtitle ) ) ? '<div class="sifency-st-subtitle">' . wp_kses_post( $st_subtitle ) . '</div>' : '';

		$btn = '';
		if ( 'yes' === $show_btn && ! empty( $st_btn_text ) ) {
			if ( ! empty( $st_btn_link['url'] ) ) {
				$this->add_link_attributes( 'st_btn_link', $st_btn_link );
				$btn = '<div class="sifency-st-btn-wrap"><a class="sifency-st-button sifency-button-style" ' . $this->get_render_attribute_string( 'st_btn_link' ) . '>' . esc_html( $st_btn_text ) . '</a></div>';
			}
		}

		if ( empty( $caption . $title . $divider . $subtitle . $btn ) ) {
			return;
		}

		$left = $center = $right = '';

		switch ( $layout ) {
			case 'layout-split-1':
				$left  = $caption . $title . $divider;
				$right = $subtitle . $btn;
				break;
			case 'layout-split-2':
				$left  = $caption . $title . $divider . $subtitle;
				$right = $btn;
				break;
			case 'layout-three-col-1':
				$left   = $caption . $title;
				$center = $subtitle;
				$right  = $btn;
				break;
			case 'layout-three-col-2':
				$left   = $caption . $title;
				$center = $btn;
				$right  = $subtitle;
				break;
			case 'layout-mixed-1':
				$left  = $caption . $title . $divider . $btn;
				$right = $subtitle;
				break;
			default:
				$center = $caption . $title . $divider . $subtitle . $btn;
				break;
		}

		echo '<div class="sifency-st-container sifency-st-' . esc_attr( $layout ) . '">';
		if ( ! empty( $left ) ) {
			echo '<div class="sifency-st-col sifency-st-left">' . wp_kses_post( $left ) . '</div>';
		}
		if ( ! empty( $center ) ) {
			echo '<div class="sifency-st-col sifency-st-center">' . wp_kses_post( $center ) . '</div>';
		}
		if ( ! empty( $right ) ) {
			echo '<div class="sifency-st-col sifency-st-right">' . wp_kses_post( $right ) . '</div>';
		}
		echo '</div>';
	}
}
