<?php
namespace CSSIgniter\Ignition\Elementor\Widget;

class Heading extends \Elementor\Widget_Base {
	public function get_style_depends() {

		wp_register_style( 'ignition-elementor-heading', plugin_dir_url( __DIR__ ) . 'assets/css/heading.min.css', array(), IGNITION_ELEMENTOR_WIDGETS_VERSION );

		return [
			'ignition-elementor-heading',
		];

	}

	/**
	 * Get widget name.
	 *
	 * Retrieve heading widget name.
	 *
	 * @since 1.4.0
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'ignition-heading';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve heading widget title.
	 *
	 * @since 1.4.0
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return __( 'Heading', 'ignition-elementor-widgets' );
	}

	/**
	 * Get widget icon.
	 *
	 * Retrieve heading widget icon.
	 *
	 * @since 1.4.0
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-heading';
	}

	/**
	 * Get widget categories.
	 *
	 * Retrieve the list of categories the heading widget belongs to.
	 *
	 * Used to determine where to display the widget in the editor.
	 *
	 * @since 1.4.0
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'ignition-elementor-widgets' ];
	}

	/**
	 * Register heading widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.4.0
	 * @access protected
	 */
	protected function register_controls() {
		$this->start_controls_section(
			'section_title',
			[
				'label' => __( 'Heading', 'ignition-elementor-widgets' ),
			]
		);

		$this->add_control(
			'heading_part_1',
			[
				'label'       => __( 'Heading part 1', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => __( 'Heading part 1', 'ignition-elementor-widgets' ),
			]
		);

		$this->add_control(
			'heading_part_2',
			[
				'label'       => __( 'Heading part 2', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => __( 'Heading part 2', 'ignition-elementor-widgets' ),
			]
		);

		$this->add_control(
			'heading_part_3',
			[
				'label'       => __( 'Heading part 3', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => __( 'Heading part 3', 'ignition-elementor-widgets' ),
			]
		);

		$this->add_control(
			'link',
			[
				'label'     => __( 'Link', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::URL,
				'dynamic'   => [
					'active' => true,
				],
				'default'   => [
					'url' => '',
				],
				'separator' => 'before',
			]
		);

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

		$this->add_control(
			'align',
			[
				'label'     => __( 'Alignment', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => [
					'left'   => [
						'title' => __( 'Left', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-left',
					],
					'center' => [
						'title' => __( 'Center', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-center',
					],
					'right'  => [
						'title' => __( 'Right', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-right',
					],
				],
				'default'   => 'left',
				'selectors' => [
					'{{WRAPPER}} .elementor-heading-title' => 'justify-content: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'view',
			[
				'label'   => __( 'View', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::HIDDEN,
				'default' => 'traditional',
			]
		);

		$this->end_controls_section();

		$heading_parts = array(
			[
				'name'  => 'heading_part_1',
				'label' => __( 'Heading Part 1', 'ignition-elementor-widgets' ),
				'class' => '.ignition-heading-part-1',
			],
			[
				'name'  => 'heading_part_2',
				'label' => __( 'Heading Part 2', 'ignition-elementor-widgets' ),
				'class' => '.ignition-heading-part-2',
			],
			[
				'name'  => 'heading_part_3',
				'label' => __( 'Heading Part 3', 'ignition-elementor-widgets' ),
				'class' => '.ignition-heading-part-3',
			],
		);

		foreach ( $heading_parts as $heading_part ) {
			$this->start_controls_section(
				'section_' . $heading_part['name'],
				[
					'label' => $heading_part['label'],
					'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
				]
			);

			$this->add_control(
				'title_' . $heading_part['name'],
				[
					'label'     => __( 'Text Color', 'ignition-elementor-widgets' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'default'   => '#000',
					'scheme'    => [
						'type'  => \Elementor\Core\Schemes\Color::get_type(),
						'value' => \Elementor\Core\Schemes\Color::COLOR_1,
					],
					'selectors' => [
						'{{WRAPPER}} ' . $heading_part['class'] => 'color: {{VALUE}};',
					],
				]
			);

			$this->add_control(
				'title_bg_' . $heading_part['name'],
				[
					'label'     => __( 'Background Color', 'ignition-elementor-widgets' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'default'   => 'transparent',
					'scheme'    => [
						'type'  => \Elementor\Core\Schemes\Color::get_type(),
						'value' => \Elementor\Core\Schemes\Color::COLOR_1,
					],
					'selectors' => [
						'{{WRAPPER}} ' . $heading_part['class'] => 'background-color: {{VALUE}};',
					],
				]
			);

			$this->add_group_control(
				\Elementor\Group_Control_Typography::get_type(),
				[
					'name'     => 'typography_' . $heading_part['name'],
					'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_1,
					'selector' => '{{WRAPPER}} ' . $heading_part['class'],
				]
			);

			$this->add_group_control(
				\Elementor\Group_Control_Text_Shadow::get_type(),
				[
					'name'     => 'text_shadow_' . $heading_part['name'],
					'selector' => '{{WRAPPER}} ' . $heading_part['class'],
				]
			);

			$this->add_control(
				'blend_mode_' . $heading_part['name'],
				[
					'label'     => __( 'Blend Mode', 'ignition-elementor-widgets' ),
					'type'      => \Elementor\Controls_Manager::SELECT,
					'options'   => [
						''            => _x( 'Normal', 'blend mode', 'ignition-elementor-widgets' ),
						'multiply'    => _x( 'Multiply', 'blend mode', 'ignition-elementor-widgets' ),
						'screen'      => _x( 'Screen', 'blend mode', 'ignition-elementor-widgets' ),
						'overlay'     => _x( 'Overlay', 'blend mode', 'ignition-elementor-widgets' ),
						'darken'      => _x( 'Darken', 'blend mode', 'ignition-elementor-widgets' ),
						'lighten'     => _x( 'Lighten', 'blend mode', 'ignition-elementor-widgets' ),
						'color-dodge' => _x( 'Color Dodge', 'blend mode', 'ignition-elementor-widgets' ),
						'saturation'  => _x( 'Saturation', 'blend mode', 'ignition-elementor-widgets' ),
						'color'       => _x( 'Color', 'blend mode', 'ignition-elementor-widgets' ),
						'difference'  => _x( 'Difference', 'blend mode', 'ignition-elementor-widgets' ),
						'exclusion'   => _x( 'Exclusion', 'blend mode', 'ignition-elementor-widgets' ),
						'hue'         => _x( 'Hue', 'blend mode', 'ignition-elementor-widgets' ),
						'luminosity'  => _x( 'Luminosity', 'blend mode', 'ignition-elementor-widgets' ),
					],
					'selectors' => [
						'{{WRAPPER}} ' . $heading_part['class'] => 'mix-blend-mode: {{VALUE}}',
					],
					'separator' => 'none',
				]
			);

			$this->add_control(
				'padding_' . $heading_part['name'],
				[
					'label'      => __( 'Padding', 'ignition-elementor-widgets' ),
					'type'       => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', 'em', '%' ],
					'selectors'  => [
						'{{WRAPPER}} ' . $heading_part['class'] => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
					'separator'  => 'before',
				]
			);

			$this->add_control(
				'margin_' . $heading_part['name'],
				[
					'label'      => __( 'Margin', 'ignition-elementor-widgets' ),
					'type'       => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', 'em', '%' ],
					'selectors'  => [
						'{{WRAPPER}} ' . $heading_part['class'] => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->end_controls_section();
		}
	}

	/**
	 * Render heading widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.4.0
	 * @access protected
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();

		if ( ! $settings['heading_part_1'] && ! $settings['heading_part_2'] && ! $settings['heading_part_3'] ) {
			return;
		}

		$title = '<span class="ignition-heading-part-1">' . $settings['heading_part_1'] . '</span>
		<span class="ignition-heading-part-2">' . $settings['heading_part_2'] . '</span>
		<span class="ignition-heading-part-3">' . $settings['heading_part_3'] . '</span>';

		$this->add_render_attribute( 'title', 'class', 'elementor-heading-title' );

		if ( ! empty( $settings['size'] ) ) {
			$this->add_render_attribute( 'title', 'class', 'elementor-size-' . $settings['size'] );
		}

		if ( ! empty( $settings['link']['url'] ) ) {
			$this->add_render_attribute( 'url', 'href', $settings['link']['url'] );

			if ( $settings['link']['is_external'] ) {
				$this->add_render_attribute( 'url', 'target', '_blank' );
			}

			if ( ! empty( $settings['link']['nofollow'] ) ) {
				$this->add_render_attribute( 'url', 'rel', 'nofollow' );
			}

			$title = sprintf( '<a %1$s>%2$s</a>', $this->get_render_attribute_string( 'url' ), $title );
		}

		$title_html = sprintf( '<%1$s %2$s>%3$s</%1$s>', $settings['header_size'], $this->get_render_attribute_string( 'title' ), $title );

		echo $title_html;
	}
}
