<?php
/**
 * Class: Site_Tagline_Widget
 * Name: Site tagline
 * Slug: eac-addon-site-tagline
 *
 * Description: Création et affichage du slogan du site
 *
 * @since 2.1.0
 */

namespace EACCustomWidgets\Includes\TemplatesLib\Widgets;

if ( ! defined( 'ABSPATH' ) ) {
	exit;   // Exit if accessed directly.
}

use EACCustomWidgets\Core\Eac_Load_Config;

use Elementor\Controls_Manager;
use Elementor\Widget_Base;
use Elementor\Icons_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;

class Site_Tagline_Widget extends Widget_Base {

	/**
	 * Le nom de la clé du composant dans le fichier de configuration
	 *
	 * @var $slug
	 *
	 * @access private
	 */
	private $slug = 'site-tagline';

	/**
	 * Retrieve widget name.
	 *
	 * @access public
	 *
	 * @return string widget name.
	 */
	public function get_name(): string {
		return Eac_Load_Config::get_widget_name( $this->slug );
	}

	/**
	 * Retrieve widget title.
	 *
	 * @access public
	 *
	 * @return string widget title.
	 */
	public function get_title(): string {
		return Eac_Load_Config::get_widget_title( $this->slug );
	}

	/**
	 * Retrieve widget icon.
	 *
	 * @access public
	 *
	 * @return string widget icon.
	 */
	public function get_icon(): string {
		return Eac_Load_Config::get_widget_icon( $this->slug );
	}

	/**
	 * Affecte le composant à la catégorie définie dans plugin.php
	 *
	 * @access public
	 *
	 * @return widget category.
	 */
	public function get_categories(): array {
		return Eac_Load_Config::get_widget_categories( $this->slug );
	}

	/**
	 * Get widget keywords.
	 *
	 * Retrieve the list of keywords the widget belongs to.
	 *
	 * @access public
	 *
	 * @return array Widget keywords.
	 */
	public function get_keywords(): array {
		return Eac_Load_Config::get_widget_keywords( $this->slug );
	}

	/**
	 * Get help widget get_custom_help_url.
	 *
	 * @access public
	 *
	 * @return string URL help center
	 */
	public function get_custom_help_url(): string {
		return Eac_Load_Config::get_widget_help_url( $this->slug );
	}

	/**
	 * has_widget_inner_wrapper
	 *
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return false;
	}

	/**
	 * Register widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @access protected
	 */
	protected function register_controls(): void {

		$this->start_controls_section(
			'site_tagline_settings_fields',
			array(
				'label' => esc_html__( 'Settings', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

			$this->add_control(
				'site_tagline_icon',
				array(
					'label'       => esc_html__( 'Select pictogram', 'eac-components' ),
					'type'        => Controls_Manager::ICONS,
					'label_block' => 'true',
					'skin'        => 'inline',
				)
			);

			$this->add_control(
				'site_tagline_icon_marge',
				array(
					'label'     => esc_html__( 'Margin', 'eac-components' ),
					'type'      => Controls_Manager::SLIDER,
					'range'     => array(
						'px' => array(
							'max' => 50,
						),
					),
					'condition' => array( 'site_tagline_icon[value]!' => '' ),
					'selectors' => array(
						'{{WRAPPER}} .eac-site_tagline-icon' => 'margin-inline-end: {{SIZE}}{{UNIT}};',
					),
				)
			);

		$this->end_controls_section();

		/**
		 * Generale Style Section
		 */
		$this->start_controls_section(
			'site_tagline_style',
			array(
				'label' => esc_html__( 'Site tagline', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

			$start = is_rtl() ? 'right' : 'left';
			$end   = is_rtl() ? 'left' : 'right';
			$this->add_responsive_control(
				'site_tagline_alignment',
				array(
					'label'     => esc_html__( 'Alignment', 'eac-components' ),
					'type'      => Controls_Manager::CHOOSE,
					'options'   => array(
						'left'   => array(
							'title' => is_rtl() ? esc_html__( 'Right', 'eac-components' ) : esc_html__( 'Left', 'eac-components' ),
							'icon'  => "eicon-text-align-{$start}",
						),
						'center' => array(
							'title' => esc_html__( 'Center', 'eac-components' ),
							'icon'  => 'eicon-text-align-center',
						),
						'right'  => array(
							'title' => is_rtl() ? esc_html__( 'Left', 'eac-components' ) : esc_html__( 'Right', 'eac-components' ),
							'icon'  => "eicon-text-align-{$end}",
						),
					),
					'selectors_dictionary' => array(
						'left'  => 'start',
						'right' => 'end',
					),
					'default'   => 'left',
					'selectors' => array(
						'{{WRAPPER}} .eac-site_tagline-wrapper' => 'text-align: {{VALUE}};',
					),
				)
			);

			$this->add_control(
				'site_tagline_color',
				array(
					'label'     => esc_html__( 'Color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'global'    => array(
						'default' => Global_Colors::COLOR_TEXT,
					),
					'selectors' => array(
						'{{WRAPPER}} .eac-site_tagline' => 'color: {{VALUE}};',
					),
				)
			);

			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => 'site_tagline_typography',
					'label'    => esc_html__( 'Typography', 'eac-components' ),
					'global'   => array(
						'default' => Global_Typography::TYPOGRAPHY_PRIMARY,
					),
					'selector' => '{{WRAPPER}} .eac-site_tagline',
				)
			);

		$this->end_controls_section();
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @access protected
	 */
	protected function render(): void {

		$settings = $this->get_settings_for_display();
		$tagline  = get_bloginfo( 'description' );
		?>		
		<div class='eac-site_tagline eac-site_tagline-wrapper'>
			<?php if ( '' !== $settings['site_tagline_icon']['value'] ) { ?>
				<span class='eac-site_tagline-icon eac-icon-svg'>
					<?php Icons_Manager::render_icon( $settings['site_tagline_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				</span>
				<?php
			}
				echo esc_html( $tagline );
			?>
		</div>
		<?php
	}

	/**
	 * Render page title output in the editor.
	 *
	 * Written as a Backbone JavaScript template and used to generate the live preview.
	 *
	 * @access protected
	 */
	protected function content_template(): void {
		?>
		<#
		var iconHTML = elementor.helpers.renderIcon( view, settings.site_tagline_icon, { 'aria-hidden': true }, 'i' , 'object' );
		#>
		<div class='eac-site_tagline eac-site_tagline-wrapper'>
			<# if ( '' != settings.site_tagline_icon.value ) { #>
				<span class='eac-site_tagline-icon eac-icon-svg'>{{{iconHTML.value}}}</span>
			<# } #>
				<?php echo esc_html( get_bloginfo( 'description' ) ); ?>	
		</div>
		<?php
	}
}
