<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
use Elementor\Core\Schemes\Typography;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Image_Size;

use EcafeAddons\EcafeElementLoad;

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

class Ecafe_Jetengine_Media extends Widget_Base {

	public function get_name() {
		return 'ecafe-jetengine-media';
	}

	public function get_title() {
		return esc_html__( 'JetEngine : Media', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-jet-icon';
	}

	public function get_categories() {
		return array( 'ecjetengine' );
	}

	public function get_keywords() {
		return array( 'jet', 'jetengine', 'jet media', 'jetengine media', 'media', 'image' );
	}
	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__( 'JetEngine : Media', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'jetImageType',
			array(
				'label'   => esc_html__( 'Type', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'id',
				'options' => array(
					'id'    => esc_html__( 'ID', 'essential-classy-addons-for-elementor' ),
					'url'   => esc_html__( 'URL', 'essential-classy-addons-for-elementor' ),
					'array' => esc_html__( 'Array', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'jetImage',
			array(
				'label'       => esc_html__( 'Select Field', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::SELECT,
				'label_block' => true,
				'multiple'    => false,
				'groups'      => \EcafeHelper::ecGetJetEngineFields( 'media' ),
			)
		);
		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'      => 'jetImageSize',
				'default'   => 'full',
				'exclude'   => array( 'custom' ),
				'condition' => array(
					'jetImageType' => 'id',
				),
			)
		);
		$this->add_control(
			'ecPrefixText',
			array(
				'label'       => esc_html__( 'Prefix', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'Enter Prefix Text', 'essential-classy-addons-for-elementor' ),
				'separator'   => 'before',
			)
		);
		$this->add_control(
			'ecPostfixText',
			array(
				'label'       => esc_html__( 'Postfix', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'Enter Postfix Text', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdoclink = esc_url( 'https://ecaddons.com/docs/jetengine-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		$this->start_controls_section(
			'jetMediaStyling',
			array(
				'label' => esc_html__( 'Jet Image', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'jetImageAlign',
			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',
					),
					'space-between' => array(
						'title' => esc_html__( 'Space Between', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-h-align-stretch',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .eca-jet-image' => 'justify-content:{{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'jetMediaSize',
			array(
				'label'      => esc_html__( 'Size', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 1000,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .eca-jet-image img' => 'width:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'jetMediaPadding',
			array(
				'label'      => esc_html__( 'Padding', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-jet-image img' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->start_controls_tabs( 'tabsjetMediaStyle' );
		$this->start_controls_tab(
			'tabjetMediaNorm',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'jetMediaBgNorm',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .eca-jet-image img',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'jetMediaBorderNorm',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .eca-jet-image img',
			)
		);
		$this->add_responsive_control(
			'jetMediaBrNorm',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-jet-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'jetMediaShadowNorm',
				'selector' => '{{WRAPPER}} .eca-jet-image img',
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabjetMediaHover',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'jetMediaBgH',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .eca-jet-image img:hover',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'jetMediaBorderH',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .eca-jet-image img:hover',
			)
		);
		$this->add_responsive_control(
			'jetMediaBrH',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-jet-image img:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'jetMediaShadowH',
				'selector' => '{{WRAPPER}} .eca-jet-image img:hover',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

		$this->start_controls_section(
			'jetPrefixStyling',
			array(
				'label' => esc_html__( 'Prefix', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'jetPrefixMinWidth',
			array(
				'label'      => esc_html__( 'Min. Width', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 500,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .eca-jet-prefix' => 'min-width:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'jetPrefixMargin',
			array(
				'label'      => esc_html__( 'Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-jet-prefix' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'jetPrefixTypo',
				'selector' => '{{WRAPPER}} .eca-jet-prefix',
			)
		);
		$this->add_control(
			'jetPrefixColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-jet-prefix' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'jetPostfixStyling',
			array(
				'label' => esc_html__( 'Postfix', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'jetPostfixMinWidth',
			array(
				'label'      => esc_html__( 'Min. Width', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 500,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .eca-jet-postfix' => 'min-width:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'jetPostfixMargin',
			array(
				'label'      => esc_html__( 'Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-jet-postfix' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'jetPostfixTypo',
				'selector' => '{{WRAPPER}} .eca-jet-postfix',
			)
		);
		$this->add_control(
			'jetPostfixColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-jet-postfix' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
	}

	protected function render() {

		$settings = $this->get_settings_for_display();
		if ( class_exists( 'Jet_Engine' ) ) {
			$postData = array();
			if ( ! isset( $GLOBALS['post'] ) ) {
				return $postData;
			}
			$postData = $GLOBALS['post'];

			if ( empty( $postData ) ) {
				$postData = get_post( 0 );
			}
			$id           = $postData->ID;
			$jetImageType = ! empty( $settings['jetImageType'] ) ? $settings['jetImageType'] : 'id';
			$jetImage     = ! empty( $settings['jetImage'] ) ? $settings['jetImage'] : '';
			if ( ! empty( $jetImage ) ) {
				$image     = get_post_meta( $id, $jetImage, true );
				$image_alt = '';
				if ( $image ) {
					echo '<div class="eca-jet-image">';
						$ecPrefixText  = ! empty( $settings['ecPrefixText'] ) ? $settings['ecPrefixText'] : '';
						$ecPostfixText = ! empty( $settings['ecPostfixText'] ) ? $settings['ecPostfixText'] : '';

					if ( ! empty( $ecPrefixText ) ) {
						echo '<div class="eca-jet-prefix">' . esc_html( $ecPrefixText ) . '</div>';
					}
					if ( ! empty( $jetImageType ) && $jetImageType == 'id' ) {
						$size = ! empty( $settings['jetImageSize_size'] ) ? $settings['jetImageSize_size'] : 'full';
						echo wp_get_attachment_image( $image, $size );
					} elseif ( ! empty( $jetImageType ) && $jetImageType == 'url' ) {
						echo '<img src="' . esc_url( $image ) . '" />';
					} elseif ( ! empty( $jetImageType ) && $jetImageType == 'array' && is_array( $image ) && isset( $image['id'] ) && $image['url'] ) {
						$image_alt = get_post_meta( $image['id'], '_wp_attachment_image_alt', true );
						echo '<img src="' . esc_url( $image['url'] ) . '" alt="' . esc_attr( $image_alt ) . '" />';
					}
					if ( ! empty( $ecPostfixText ) ) {
						echo '<div class="eca-jet-postfix">' . esc_html( $ecPostfixText ) . '</div>';
					}
					echo '</div>';
				}
			}
		}
	}
}
