<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Css_Filter;

use EcafeAddons\EcafeElementLoad;

// Prevent direct access to files
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Ecafe_ImageMaskHoverEffect extends Widget_Base {

	public function get_name() {
		return 'ecafe-image-mask-hover-effect';
	}

	public function get_title() {
		return esc_html__( 'Image Mask Hover Effect', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-image-mask-hover-effect-icon';
	}
	public function get_categories() {
		return array( 'essential' );
	}
	public function get_keywords() {
		return array( 'image mask hover', 'image hover', 'advanced image hover', 'image effect', 'hover card', 'card effect', 'hover effect' );
	}

	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}
	protected function register_controls() {
		$this->start_controls_section(
			'contentSection',
			array(
				'label' => esc_html__( 'Image Mask Hover Effect', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'imageMaskHE',
			array(
				'label'   => esc_html__( 'Image', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array(
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				),
				'dynamic'    => array(
					'active' => true,
				),
			)
		);
		$this->add_control(
			'imageMaskUrl',
			array(
				'label'         => esc_html__( 'URL', 'essential-classy-addons-for-elementor' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => esc_html__( 'https://www.demo-link.com', 'essential-classy-addons-for-elementor' ),
				'dynamic'       => array( 'active' => true ),
				'show_external' => true,
			)
		);
		$this->add_responsive_control(
			'imageMaskAlign',
			array(
				'label'     => esc_html__( 'Alignment', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .ec-image-mask-hover-effect-wrapper' => 'justify-content:{{VALUE}};',
				),
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdemolink = esc_url( 'https://ecaddons.com/elements/image-mask-hover-effect/' );
		$ecwidgetdoclink  = esc_url( 'https://ecaddons.com/docs/image-mask-hover-effect-widget-tutorials/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		$this->start_controls_section(
			'imagemaskHEStyling',
			array(
				'label' => esc_html__( 'Image Mask', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->start_controls_tabs( 'tabsimagemaskHEStyle' );
		$this->start_controls_tab(
			'tabimagemaskHEN',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Css_Filter::get_type(),
			array(
				'name'     => 'imagemaskHEFilters',
				'label'    => esc_html__( 'Filter', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-image-mask-hover-effect-wrapper .ec-imhe-img',
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabimagemaskHEH',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Css_Filter::get_type(),
			array(
				'name'     => 'imagemaskHEFiltersH',
				'label'    => esc_html__( 'Filter', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-image-mask-hover-effect-wrapper .ec-imhe-img:hover',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
	}

	protected function render() {
		$settings           = $this->get_settings_for_display();
		$imageMaskUrl       = ! empty( $settings['imageMaskUrl']['url'] ) ? esc_url( $settings['imageMaskUrl']['url'] ) : '';
		$imageMaskUrlTarget = $imageMaskUrlNoFollow = '';
		if ( ! empty( $imageMaskUrl ) ) {
			$imageMaskUrlTarget   = $settings['imageMaskUrl']['is_external'] ? ' target="_blank"' : '';
			$imageMaskUrlNoFollow = $settings['imageMaskUrl']['nofollow'] ? ' rel="nofollow"' : '';
		}
		if ( ! empty( $imageMaskUrl ) ) {
			$imageMaskUrlTarget   = ! empty( $settings['imageMaskUrl']['is_external'] ) ? ' target="_blank"' : '';
			$imageMaskUrlNoFollow = ! empty( $settings['imageMaskUrl']['nofollow'] ) ? ' rel="nofollow"' : '';
			$output               = '<a href="' . $imageMaskUrl . '"' . $imageMaskUrlTarget . $imageMaskUrlNoFollow . ' class="ec-image-mask-hover-effect-wrapper">';
		} else {
			$output = '<div class="ec-image-mask-hover-effect-wrapper">';
		}
		$imgAlt = $image_src = '';
		if ( ! empty( $settings['imageMaskHE']['id'] ) ) {
			$image_id  = $settings['imageMaskHE']['id'];
			$image_src = wp_get_attachment_image_src( $image_id, 'full' );
			$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
			if ( ! empty( $image_src[0] ) ) {
				$image_src = $image_src[0];
				if ( $image_alt ) {
					$imgAlt = 'alt="' . esc_attr( $image_alt ) . '"';
				}
			}
		} else {
			$image_src = ECAFE_ASSETS_PATH . 'images/placeholder.png';
		}
		$output .= '<img src="' . esc_url( $image_src ) . '" class="ec-imhe-img" ' . $imgAlt . '>';
		if ( ! empty( $imageMaskUrl ) ) {
			$output .= '</a>';
		} else {
			$output .= '</div>';
		}
		echo wp_kses_normalize_entities( $output );
	}
}
