<?php

namespace DynamicContentForElementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Scheme_Color;
use Elementor\Scheme_Typography;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Utils;
use DynamicContentForElementor\DCE_Helper;
use DynamicContentForElementor\Group_Control_Outline;
use DynamicContentForElementor\Controls\DCE_Group_Control_Filters_CSS;
use DynamicContentForElementor\Controls\DCE_Group_Control_Transform_Element;

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

/**
 * Elementor ACF-Gallery
 *
 * Elementor widget for Dynamic Content for Elementor
 *
 */
class DCE_Widget_Gallery extends DCE_Widget_Prototype {

	public function get_name() {
		return 'dyncontel-acfgallery';
	}

	public static function is_enabled() {
		return true;
	}

	public function get_title() {
		return __('ACF Gallery', 'dynamic-content-for-elementor');
	}

	public function get_description() {
		return __('Use a list of elements generated by an ACF field Gallery considering different ways to use it', 'dynamic-content-for-elementor');
	}

	public function get_docs() {
		return 'https://www.dynamic.ooo/widget/acf-gallery/';
	}

	public function get_icon() {
		return 'icon-dyn-grid';
	}

	public function get_plugin_depends() {
		return array( 'advanced-custom-fields-pro' => 'acf' );
	}

	public function get_script_depends() {
		return [ 'imagesloaded', 'jquery-masonry', 'wow', 'photoswipe', 'photoswipe-ui', 'diamonds', 'homeycombs', 'justifiedGallery-lib', 'dce-acfgallery' ];
	}

	public function get_style_depends() {
		return [ 'animatecss', 'dce-photoSwipe_default', 'dce-photoSwipe_skin', 'dce-justifiedGallery', 'animatecss', 'dce-acfGallery' ];
	}
	public static function get_position() {
		return 2;
	}

	/* public function get_style_depends() {
	  return [ 'dce-photoSwipe_default','dce-photoSwipe_skin','dce-acfGallery' ];
	  } */

	protected function _register_controls() {
		$post_type_object = get_post_type_object(get_post_type());
		$acf_group = '';
		// ********************************************************************************* Section BASE
		$this->start_controls_section(
				'section_content', [
					'label' => 'ACF',
					'dynamic-content-for-elementor',
				]
		);

		/*$this->add_control(
				'acf_field_list', [
			'label' => __('ACF Fields', 'dynamic-content-for-elementor'),
			'type' => Controls_Manager::SELECT,
			'groups' => DCE_Helper::get_acf_fields('gallery', true),
			//'options' => $this->get_acf_field(true),
			'default' => 'Select the Field',
				]
		);*/
		$this->add_control(
				'acf_field_list',
				[
					'label' => __('ACF Fields', 'dynamic-content-for-elementor'),
					'type' => 'ooo_query',
					'placeholder' => __('Select the Field', 'dynamic-content-for-elementor'),
					'label_block' => true,
					'query_type' => 'acf',
					'object_type' => 'gallery',
				]
		);

		/* $this->add_control(
		  'acf_text_before', [
		  'label' => __('Text before', 'dynamic-content-for-elementor'),
		  'description' => __('Un testo prima dell\'elemento', 'dynamic-content-for-elementor'),
		  'type' => Controls_Manager::TEXT,
		  'default' => '',
		  ]
		  ); */
		$this->add_control(
				'enabled_wow', [
					'label' => __('Enable WOW Animation', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SWITCHER,
					'default' => '',
					'label_on' => __('Yes', 'dynamic-content-for-elementor'),
					'label_off' => __('No', 'dynamic-content-for-elementor'),
					'return_value' => 'yes',
					'frontend_available' => true,
				]
		);

		$this->end_controls_section();

		// ********************************************************************************* Section GALLERY
		$this->start_controls_section(
			'section_settings_gallery', [
				'label' => 'Gallery',
				'dynamic-content-for-elementor',
				'condition' => [
				//'acf_type' => ['gallery'],
				],
			]
		);
		$this->add_control(
			'gallery_type', [
				'label' => __('Gallery Type', 'dynamic-content-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'row' => __('Row', 'dynamic-content-for-elementor'),
					'grid' => __('Grid', 'dynamic-content-for-elementor'),
					'masonry' => __('Masonry', 'dynamic-content-for-elementor'),
					'justified' => __('Justified', 'dynamic-content-for-elementor'),
					'single_image' => __('Single image', 'dynamic-content-for-elementor'),
					'diamond' => __('Diamond', 'dynamic-content-for-elementor'),
					'hexagon' => __('Hexagon', 'dynamic-content-for-elementor'),
				],
				'default' => 'masonry',
				//'prefix_class' => 'gallerytype-',
				'frontend_available' => true,
			]
		);
		// ***************** SINGLE iMAGE
		$this->add_control(
			'single_image_type', [
				'label' => __('Type of single image', 'dynamic-content-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'first' => __('First', 'dynamic-content-for-elementor'),
					'random' => __('Random', 'dynamic-content-for-elementor'),
				],
				'default' => 'first',
				'frontend_available' => true,
				'condition' => [
					'gallery_type' => 'single_image',
				],
			]
		);
		$this->add_responsive_control(
			'columns_grid', [
				'label' => __('Items per row', 'dynamic-content-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'default' => '2',
				'tablet_default' => '3',
				'mobile_default' => '1',
				'options' => [
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				],
				'render_type' => 'template',
				//'frontend_available' => true,
				'selectors' => [
					'{{WRAPPER}} .acfgallery-item' => 'width: calc( 100% / {{VALUE}} );',
				],
				'condition' => [
					'gallery_type' => [ 'grid', 'masonry' ],
				],
			]
		);

		// ***************** JUSTIFIED
		$this->add_control(
			'justified_rowHeight', [
				'label' => __('Row Height', 'dynamic-content-for-elementor'),
				'type' => Controls_Manager::SLIDER,
				'default' => [
					'size' => '',
				],
				'range' => [
					'px' => [
						'min' => 10,
						'max' => 800,
						'step' => 1,
					],
				],
				'frontend_available' => true,
				'condition' => [
					'gallery_type' => 'justified',
				],
			]
		);
		$this->add_control(
			'justified_margin', [
				'label' => __('Images space', 'dynamic-content-for-elementor'),
				'type' => Controls_Manager::SLIDER,
				'default' => [
					'size' => '',
				],
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 100,
						'step' => 1,
					],
				],
				'frontend_available' => true,
				'condition' => [
					'gallery_type' => 'justified',
				],
			]
		);
		$this->add_control(
			'justified_lastRow', [
				'label' => __('Last row', 'dynamic-content-for-elementor'),
				'type' => Controls_Manager::SELECT,
				'default' => 'nojustify',
				'options' => [
					'justify' => 'Justify',
					'nojustify' => 'Left',
					'center' => 'Center',
					'right' => 'Right',
					'hide' => 'Hide',
				],
				'frontend_available' => true,
				'condition' => [
					'gallery_type' => 'justified',
				],
			]
		);
		// ***************** DIAMOND
		$this->add_control(
				'column_diamond', [
					'label' => __('Min Diamond per Row', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SELECT,
					'default' => '4',
					'options' => [
						'1' => '1',
						'2' => '2',
						'3' => '3',
						'4' => '4',
						'5' => '5',
						'6' => '6',
					],
					'frontend_available' => true,
					/* 'selectors' => [
					  '{{WRAPPER}} .acfgallery-item' => 'width: calc( 100% / {{VALUE}} );',
					  ], */
					'condition' => [
						'gallery_type' => 'diamond',
					],
				]
		);
		$this->add_responsive_control(
				'size_diamond', [
					'label' => __('Size', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SLIDER,
					'default' => [
						'size' => '',
					],
					//'size_units' => [ 'px','%'],
					'range' => [
						'px' => [
							'min' => 20,
							'max' => 800,
							'step' => 1,
						],
					],
					'frontend_available' => true,
					'selectors' => [
						'{{WRAPPER}} .diamonds .diamond-box-wrap' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
					],
					'condition' => [
						'gallery_type' => 'diamond',
					],
				]
		);
		/* $this->add_responsive_control(
		  'gap_diamond', [
		  'label' => __('Gap Diamond', 'dynamic-content-for-elementor'),
		  'type' => Controls_Manager::SLIDER,
		  'default' => [
		  'size' => '',
		  'unit' => 'px',
		  ],
		  'size_units' => [ 'px'],
		  'range' => [
		  'px' => [
		  'min' => 0,
		  'max' => 100,
		  ],
		  ],
		  'selectors' => [
		  '{{WRAPPER}} .diamonds .diamond-box-wrap' => 'padding: {{SIZE}}{{UNIT}};',
		  ],
		  'condition' => [
		  'gallery_type' => 'diamond',
		  ]
		  ]
		  ); */
		$this->add_control(
				'gap_diamond', [
					'label' => __('Gap Diamond', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::NUMBER,
					'default' => 0,
					'min' => 0,
					'max' => 100,
					'step' => 1,
					'frontend_available' => true,
					'condition' => [
						'gallery_type' => 'diamond',
					],
				]
		);
		$this->add_control(
				'hideIncompleteRow', [
					'label' => __('Hide Incomplete Row', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SWITCHER,
					'frontend_available' => true,
					'condition' => [
						'gallery_type' => 'diamond',
					],
				]
		);
		$this->add_responsive_control(
				'size_honeycombs', [
					'label' => __('Size Hexagon', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::NUMBER,
					'default' => 250,
					'tablet_default' => 150,
					'mobile_default' => 100,
					'min' => 20,
					'max' => 800,
					'step' => 1,
					'frontend_available' => true,
					'condition' => [
						'gallery_type' => 'hexagon',
					],
				]
		);
		$this->add_control(
				'gap_honeycombs', [
					'label' => __('Gap Hexagon', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::NUMBER,
					'default' => 10,
					'min' => 0,
					'max' => 100,
					'step' => 1,
					'frontend_available' => true,
					'condition' => [
						'gallery_type' => 'hexagon',
					],
				]
		);
		$this->end_controls_section();
		/* $this->add_control(
		  'fitrow_enable', [
		  'label'         => __( 'Fit Row', 'dynamic-content-for-elementor' ),
		  'type'          => Controls_Manager::SWITCHER,
		  'default'       => '',
		  'label_on'      => __( 'Yes', 'dynamic-content-for-elementor' ),
		  'label_off'     => __( 'No', 'dynamic-content-for-elementor' ),
		  'return_value'  => 'yes',
		  //'frontend_available' => true,
		  // 'condition' => [
		  //     'posts_style' => 'grid',
		  // ],
		  ]
		  );
		  $this->add_control(
		  'sameheight_enable', [
		  'label'         => __( 'Same Height', 'dynamic-content-for-elementor' ),
		  'type'          => Controls_Manager::SWITCHER,
		  'default'       => '',
		  'label_on'      => __( 'Yes', 'dynamic-content-for-elementor' ),
		  'label_off'     => __( 'No', 'dynamic-content-for-elementor' ),
		  'return_value'  => 'yes',
		  //'frontend_available' => true,
		  // 'condition' => [
		  //     'posts_style' => 'grid',
		  // ],
		  ]
		  );
		 */
		$this->start_controls_section(
				'section_style_gallery', [
					'label' => 'Gallery',
					'dynamic-content-for-elementor',
					'tab' => Controls_Manager::TAB_STYLE,
				]
		);
		/*$this->add_control(
				'style_heading',
				[
					'label' => __('Style', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::HEADING,
					'separator' => 'before',
				]
		);*/
		$this->add_responsive_control(
				'align', [
					'label' => __('Alignment', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::CHOOSE,
					'options' => [
						'left' => [
							'title' => __('Left', 'dynamic-content-for-elementor'),
							'icon' => 'fa fa-align-left',
						],
						'center' => [
							'title' => __('Center', 'dynamic-content-for-elementor'),
							'icon' => 'fa fa-align-center',
						],
						'right' => [
							'title' => __('Right', 'dynamic-content-for-elementor'),
							'icon' => 'fa fa-align-right',
						],
					],
					'default' => '',
					'prefix_class' => 'align-',
					'selectors' => [
						'{{WRAPPER}} .dynamic_acfgallery' => 'text-align: {{VALUE}};',
					],
				]
		);

		$this->add_responsive_control(
				'v_align', [
					'label' => __('Vertical Alignment', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::CHOOSE,
					'options' => [
						'top' => [
							'title' => __('Top', 'dynamic-content-for-elementor'),
							'icon' => 'eicon-v-align-top',
						],
						'middle' => [
							'title' => __('Middle', 'dynamic-content-for-elementor'),
							'icon' => 'eicon-v-align-middle',
						],
						'down' => [
							'title' => __('Down', 'dynamic-content-for-elementor'),
							'icon' => 'eicon-v-align-bottom',
						],
					],
					'default' => 'top',
					'selectors' => [
						'{{WRAPPER}} .dynamic_acfgallery  .acfgallery-item' => 'vertical-align: {{VALUE}};',
					],
					'condition' => [
						'gallery_type' => [ 'grid' ],
					],
				]
		);
		$this->add_responsive_control(
				'items_padding', [
					'label' => __('Paddings Items', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .acfgallery-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
					'condition' => [
						'gallery_type!' => [ 'hexagon' ],
					],
				]
		);
		$this->add_control(
				'image_border_radius', [
					'label' => __('Border Radius', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} .wrap-item-acfgallery' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
					'condition' => [
						'gallery_type!' => [ 'hexagon' ],
					],
				]
		);
		$this->add_group_control(
				Group_Control_Border::get_type(), [
					'name' => 'image_border',
					'label' => __('Image Border', 'dynamic-content-for-elementor'),
					'selector' => '{{WRAPPER}} .wrap-item-acfgallery',
					'condition' => [
						'gallery_type!' => [ 'diamond', 'hexagon' ],
					],
				]
		);

		$this->add_group_control(
				Group_Control_Box_Shadow::get_type(), [
					'name' => 'image_box_shadow',
					'selector' => '{{WRAPPER}} .dynamic_acfgallery-masonry .wrap-item-acfgallery, {{WRAPPER}} .dynamic_acfgallery-diamond .diamond-box',
					'condition' => [
						'gallery_type!' => [ 'hexagon' ],
					],
				]
		);
		$this->end_controls_section();

		// ********************************************************************************* Section IMAGE SETTINGS
		$this->start_controls_section(
				'section_settings', [
					'label' => 'Images',
					'condition' => [
					//'gallery_type!' => 'hexagon',
					],
				]
		);
		/* $this->add_control(
		  'html_tag', [
		  'label' => __( 'HTML Tag', 'dynamic-content-for-elementor' ),
		  'type' => Controls_Manager::SELECT,
		  'options' => [
		  'h1' => __( 'H1', 'dynamic-content-for-elementor' ),
		  'h2' => __( 'H2', 'dynamic-content-for-elementor' ),
		  'h3' => __( 'H3', 'dynamic-content-for-elementor' ),
		  'h4' => __( 'H4', 'dynamic-content-for-elementor' ),
		  'h5' => __( 'H5', 'dynamic-content-for-elementor' ),
		  'h6' => __( 'H6', 'dynamic-content-for-elementor' ),
		  'p'  => __( 'p', 'dynamic-content-for-elementor' ),
		  'div' => __( 'div', 'dynamic-content-for-elementor' ),
		  'span' => __( 'span', 'dynamic-content-for-elementor' ),
		  ],
		  'default' => 'h2',
		  'condition' => [
		  ////'acf_type' => ['text','textarea'],
		  ]]
		  );

		  $this->add_control(
		  'link_to', [
		  'label' => __( 'Link to', 'dynamic-content-for-elementor' ),
		  'type' => Controls_Manager::SELECT,
		  'default' => 'none',
		  'options' => [
		  'none' => __( 'None', 'dynamic-content-for-elementor' ),
		  'home' => __( 'Home URL', 'dynamic-content-for-elementor' ),
		  'post' => sprintf(
		  __( '%s URL', 'dynamic-content-for-elementor' ),
		  $post_type_object->labels->singular_name ),
		  'custom' => __( 'Custom URL', 'dynamic-content-for-elementor' ),
		  ],
		  'condition' => [
		  ////'acf_type' => ['text','textarea','image'],
		  ]
		  ]
		  );

		  $this->add_control(
		  'link', [
		  'label' => __( 'Link', 'dynamic-content-for-elementor' ),
		  'type' => Controls_Manager::URL,
		  'placeholder' => __( 'http://your-link.com', 'dynamic-content-for-elementor' ),
		  'default' => [
		  'url' => '',
		  ],
		  'show_label' => false,
		  'condition' => [
		  //'acf_type' => ['text','textarea','image'],
		  'link_to' => 'custom',
		  ]
		  ]
		  );

		  $this->add_control(
		  'preview', [
		  'type' => Controls_Manager::RAW_HTML,
		  'raw' => get_the_post_thumbnail(),
		  'separator' => 'none',
		  'condition' => [
		  //'acf_type' => 'image',
		  ]
		  ]
		  );
		 */

		$this->add_group_control(
				Group_Control_Image_Size::get_type(), [
					'name' => 'size',
					'label' => __('Image Size', 'dynamic-content-for-elementor'),
					'default' => 'large',
					'condition' => [
					//'acf_type' => 'image',
					],
				]
		);

		$this->add_control(
				'use_desc', [
					'label' => __('Description', 'dynamic-content-for-elementor'),
					'description' => __('Select what to use in the description below the image', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SELECT2,
					'multiple' => true,
					'options' => [
						'title' => 'Title',
						'caption' => 'Caption',
						'description' => 'Description',
					],
					'default' => '',
					//'frontend_available' => true,
					'condition' => [
						'gallery_type!' => [ 'diamond', 'hexagon' ],
					],
				]
		);
		$this->end_controls_section();

		$this->start_controls_section(
				'section_style_images', [
					'label' => 'Images',
					'dynamic-content-for-elementor',
					'tab' => Controls_Manager::TAB_STYLE,
				]
		);
		$this->add_control(
				'force_width', [
					'label' => __('Force Width', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SWITCHER,
					'prefix_class' => 'forcewidth-',
				//'frontend_available' => true,
				/* 'selectors' => [
				  '{{WRAPPER}} .acfgallery-item img' => 'max-width: inherit;',
				  ],
				 */
				]
		);
		// ***************** IMAGE
		$this->add_responsive_control(
				'size_img', [
					'label' => __('Size (%)', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SLIDER,
					'default' => [
						'size' => 100,
						'unit' => '%',
					],
					'size_units' => [ '%' ],
					'range' => [
						'%' => [
							'min' => 1,
							'max' => 100,
						],
					],
					'selectors' => [
						'{{WRAPPER}} .wrap-item-acfgallery' => 'width: {{SIZE}}{{UNIT}};',
					],
					'condition' => [
						'force_width' => 'yes',
					],
				]
		);

		/* $this->add_control(
		  'enable_image_effects', [
		  'label' => __('Enable Effects', 'dynamic-content-for-elementor'),
		  'type' => Controls_Manager::SWITCHER,
		  'default' => '',
		  'label_on' => __('Yes', 'dynamic-content-for-elementor'),
		  'label_off' => __('No', 'dynamic-content-for-elementor'),
		  'return_value' => 'yes',
		  ]
		  );
		  $this->add_responsive_control(
		  'opacity', [
		  'label' => __('Opacity (%)', 'dynamic-content-for-elementor'),
		  'type' => Controls_Manager::SLIDER,
		  'default' => [
		  'size' => '',
		  ],
		  'range' => [
		  'px' => [
		  'max' => 1,
		  'min' => 0.10,
		  'step' => 0.01,
		  ],
		  ],
		  'selectors' => [
		  '{{WRAPPER}} .wrap-item-acfgallery' => 'opacity: {{SIZE}};',
		  ],
		  'condition' => [
		  'enable_image_effects' => 'yes',
		  ]
		  ]
		  );

		  $this->add_control(
		  'angle', [
		  'label' => __('Angle (deg)', 'dynamic-content-for-elementor'),
		  'type' => Controls_Manager::SLIDER,
		  'size_units' => [ 'deg'],
		  'default' => [
		  'unit' => 'deg',
		  'size' => '',
		  ],
		  'range' => [
		  'deg' => [
		  'max' => 360,
		  'min' => -360,
		  'step' => 1,
		  ],
		  ],
		  'selectors' => [
		  '{{WRAPPER}} .wrap-item-acfgallery' => '-webkit-transform: rotate({{SIZE}}{{UNIT}}); -moz-transform: rotate({{SIZE}}{{UNIT}}); -ms-transform: rotate({{SIZE}}{{UNIT}}); -o-transform: rotate({{SIZE}}{{UNIT}}); transform: rotate({{SIZE}}deg);',
		  ],
		  'condition' => [
		  'enable_image_effects' => 'yes',
		  ]
		  ]
		  ); */

		$this->add_control(
				'popover-toggle',
				[
					'label' => __('Transform image', 'plugin-name'),
					'type' => \Elementor\Controls_Manager::POPOVER_TOGGLE,
					'return_value' => 'yes',
				]
		);
		$this->start_popover();

		$this->add_group_control(
				DCE_Group_Control_Transform_Element::get_type(),
				[
					'name' => 'transform_image',
					'label' => 'Transform image',
					'selector' => '{{WRAPPER}} .dynamic_acfgallery',
					'separator' => 'before',
				]
		);
		$this->end_popover();

		$this->add_group_control(
				DCE_Group_Control_Filters_CSS::get_type(),
				[
					'name' => 'filters_image',
					'label' => 'Filters image',
					'selector' => '{{WRAPPER}} .acfgallery-item img',
				]
		);
		$this->add_responsive_control(
				'desc_margin', [
					'label' => __('space', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%' ],
					'selectors' => [
						'{{WRAPPER}} figcaption' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
					'condition' => [
						'use_desc!' => '',
					],
				]
		);

		// ---------------------------------- CAPTION
		$this->add_control(
				'figure_title_heading',
				[
					'label' => __('Title', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::HEADING,
					'separator' => 'before',
					'condition' => [
						'use_desc' => 'title',
					],
				]
		);
		$this->add_control(
				'acf_space', [
					'label' => __('Space', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SLIDER,
					'default' => [
						'size' => 0,
					],
					'range' => [
						'px' => [
							'max' => 100,
							'min' => 0,
							'step' => 1,
						],
					],
					'selectors' => [
						'{{WRAPPER}} figcaption .title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
					],
					'condition' => [
						'use_desc' => 'title',
					],
				]
		);
		$this->add_control(
				'desc_color', [
					'label' => __('Color', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::COLOR,
					'default' => '',
					'selectors' => [
						'{{WRAPPER}} figcaption .title' => 'color: {{VALUE}};',
					],
					'condition' => [
						'use_desc' => 'title',
					],
				]
		);
		$this->add_group_control(
				Group_Control_Typography::get_type(), [
					'name' => 'desc_typography',
					'label' => __('Typography', 'dynamic-content-for-elementor'),
					'selector' => '{{WRAPPER}} figcaption .title',
					'condition' => [
						'use_desc' => 'title',
					],
				]
		);

		// ---------------------------------- CAPTION
		$this->add_control(
				'figure_caption_heading',
				[
					'label' => __('Caption', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::HEADING,
					'separator' => 'before',
					'condition' => [
						'use_desc' => 'caption',
					],
				]
		);
		$this->add_control(
				'space_caption', [
					'label' => __('Space', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SLIDER,
					'default' => [
						'size' => 0,
					],
					'range' => [
						'px' => [
							'max' => 100,
							'min' => 0,
							'step' => 1,
						],
					],
					'selectors' => [
						'{{WRAPPER}} figcaption .caption' => 'margin-bottom: {{SIZE}}{{UNIT}};',
					],
					'condition' => [
						'use_desc' => 'caption',
					],
				]
		);
		$this->add_control(
				'caption_color', [
					'label' => __('Color', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::COLOR,
					'default' => '',
					'selectors' => [
						'{{WRAPPER}} figcaption .caption' => 'color: {{VALUE}};',
					],
					'condition' => [
						'use_desc' => 'caption',
					],
				]
		);
		$this->add_group_control(
				Group_Control_Typography::get_type(), [
					'name' => 'caption_typography',
					'label' => __('Typography', 'dynamic-content-for-elementor'),
					'selector' => '{{WRAPPER}} figcaption .caption',
					'condition' => [
						'use_desc' => 'caption',
					],
				]
		);

		// ---------------------------------- DESCRIPTION
		$this->add_control(
				'figure_description_heading',
				[
					'label' => __('Description', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::HEADING,
					'separator' => 'before',
					'condition' => [
						'use_desc' => 'description',
					],
				]
		);
		$this->add_control(
				'space_description', [
					'label' => __('Space', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SLIDER,
					'default' => [
						'size' => 0,
					],
					'range' => [
						'px' => [
							'max' => 100,
							'min' => 0,
							'step' => 1,
						],
					],
					'selectors' => [
						'{{WRAPPER}} figcaption .description' => 'margin-bottom: {{SIZE}}{{UNIT}};',
					],
					'condition' => [
						'use_desc' => 'description',
					],
				]
		);
		$this->add_control(
				'description_color', [
					'label' => __('Color', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::COLOR,
					'default' => '',
					'selectors' => [
						'{{WRAPPER}} figcaption .description' => 'color: {{VALUE}};',
					],
					'condition' => [
						'use_desc' => 'description',
					],
				]
		);
		$this->add_group_control(
				Group_Control_Typography::get_type(), [
					'name' => 'description_typography',
					'label' => __('Typography', 'dynamic-content-for-elementor'),
					'selector' => '{{WRAPPER}} figcaption .description',
					'condition' => [
						'use_desc' => 'description',
					],
				]
		);

		$this->end_controls_section();

		// ********************************************************************************* Section WOW
		$this->start_controls_section(
				'section_wow', [
					'label' => 'Wow animation',
					'condition' => [
						'enabled_wow' => 'yes',
					],
				]
		);
		// coefficiente per default
		$this->add_control(
				'wow_coef', [
					'label' => __('Delay', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::NUMBER,
					'default' => 0,
					'min' => 0.05,
					'max' => 1,
					'step' => 0.05,
					'condition' => [
						'enabled_wow' => 'yes',
					],
				]
		);
		$this->add_control(
				'wow_animations', [
					'label' => __('Wow Animation Effect', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SELECT,
					'options' => [
						'fadeIn' => 'Fade In',
						'fadeInDown' => 'Fade In Down',
						'fadeInLeft' => 'Fade In Left',
						'fadeInRight' => 'Fade In Right',
						'fadeInUp' => 'Fade In Up',
						'zoomIn' => 'Zoom In',
						'zoomInDown' => 'Zoom In Down',
						'zoomInLeft' => 'Zoom In Left',
						'zoomInRight' => 'Zoom In Right',
						'zoomInUp' => 'Zoom In Up',
						'bounceIn' => 'Bounce In',
						'bounceInDown' => 'Bounce In Down',
						'bounceInLeft' => 'Bounce In Left',
						'bounceInRight' => 'Bounce In Right',
						'bounceInUp' => 'Bounce In Up',
						'slideInDown' => 'Slide In Down',
						'slideInLeft' => 'Slide In Left',
						'slideInRight' => 'Slide In Right',
						'slideInUp' => 'Slide In Up',
						'rotateIn' => 'Rotate In',
						'rotateInDownLeft' => 'Rotate In Down Left',
						'rotateInDownRight' => 'Rotate In Down Right',
						'rotateInUpLeft' => 'Rotate In Up Left',
						'rotateInUpRight' => 'Rotate In Up Right',
						'bounce' => 'Bounce',
						'flash' => 'Flash',
						'pulse' => 'Pulse',
						'rubberBand' => 'Rubber Band',
						'shake' => 'Shake',
						'headShake' => 'Head Shake',
						'swing' => 'Swing',
						'tada' => 'Tada',
						'wobble' => 'Wobble',
						'jello' => 'Jello',
						'lightSpeedIn' => 'Light Speed In',
						'rollIn' => 'Roll In',
					],
					'default' => 'fadeInUp',
					//'frontend_available' => true,
					'condition' => [
						'enabled_wow' => 'yes',
					],
				]
		);
		$this->end_controls_section();

		// ********************************************************************************* Section GALLERY
		$this->start_controls_section(
				'section_lightbox_effects', [
					'label' => 'Lightbox',
					'dynamic-content-for-elementor',
					'condition' => [
					//'acf_type' => ['gallery'],
					],
				]
		);
		$this->add_control(
				'enable_lightbox', [
					'label' => __('Enable LightBox', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SWITCHER,
					'default' => 'yes',
					'frontend_available' => true,
				]
		);
		$this->add_control(
				'enable_lightbox_link', [
					'label' => __('Image link', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SWITCHER,
					'condition' => [
						'enable_lightbox' => '',
					],
				]
		);
		$this->add_control(
				'lightbox_type', [
					'label' => __('Lightbox Type', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SELECT,
					'options' => [
						'' => __('Default', 'dynamic-content-for-elementor'),
						'photoswipe' => 'Photoswipe',
					],
					'default' => '',
					'condition' => [
						'enable_lightbox!' => '',
					],
				]
		);

		$this->end_controls_section();

		/* $this->add_control(
		  'enable_overlay_hover', [
		  'label' => __('Overlay Hover', 'dynamic-content-for-elementor'),
		  'type' => Controls_Manager::SWITCHER,
		  'default' => '',
		  'label_on' => __('Yes', 'dynamic-content-for-elementor'),
		  'label_off' => __('No', 'dynamic-content-for-elementor'),
		  'return_value' => 'yes',
		  //'prefix_class' => 'overlay-',
		  //'frontend_available' => true,

		  'condition' => [
		  'enable_lightbox' => 'yes',
		  ]
		  ]
		  ); */

		$this->start_controls_section(
				'section_style_image_link', [
					'label' => 'Rollover',
					'dynamic-content-for-elementor',
					'tab' => Controls_Manager::TAB_STYLE,
				]
		);

		$this->add_group_control(
				Group_Control_Background::get_type(),
				[
					'name' => 'background',
					'types' => [ 'classic', 'gradient' ],
					'label' => 'Overlay',
					'selector' => '{{WRAPPER}} .acfgallery-overlay_hover, {{WRAPPER}} .inner_span',
					'popover' => true,
				/* 'condition' => [
				  'enable_overlay_hover' => 'yes',
				  ] */
				]
		);
		$this->add_control(
				'hover_effects', [
					'label' => __('Hover Effects', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SELECT,
					'options' => [
						'' => __('None', 'dynamic-content-for-elementor'),
						'zoom' => __('Zoom', 'dynamic-content-for-elementor'),
					],
					'default' => '',
					'separator' => 'before',
					'prefix_class' => 'hovereffect-',
					'condition' => [
						'enable_lightbox!' => '',
					],
				]
		);
		$this->end_controls_section();

		$this->start_controls_section(
				'section_dce_settings', [
					'label' => __('Dynamic content', 'dynamic-content-for-elementor'),
					'tab' => Controls_Manager::TAB_SETTINGS,
				]
		);
		$this->add_control(
				'data_source',
				[
					'label' => __('Source', 'dynamic-content-for-elementor'),
					'description' => __('Select the data source', 'dynamic-content-for-elementor'),
					'type' => Controls_Manager::SWITCHER,
					'default' => 'yes',
					'label_on' => __('Same', 'dynamic-content-for-elementor'),
					'label_off' => __('Other', 'dynamic-content-for-elementor'),
					'return_value' => 'yes',
				]
		);
		/*$this->add_control(
				'other_post_source', [
			'label' => __('Select from other source post', 'dynamic-content-for-elementor'),
			'type' => Controls_Manager::SELECT,
			'groups' => DCE_Helper::get_all_posts(get_the_ID(), true),
			//'options' => DCE_Helper::get_all_posts(),
			'default' => '',
			'condition' => [
				'data_source' => '',
			],
				]
		);*/
		$this->add_control(
				'other_post_source',
				[
					'label' => __('Select from other source post', 'dynamic-content-for-elementor'),
					'type'      => 'ooo_query',
					'placeholder'   => __( 'Post Title', 'dynamic-content-for-elementor' ),
					'label_block'   => true,
					'query_type'    => 'posts',
					'condition' => [
						'data_source' => '',
					],
				]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty($settings) ) {
			return;
		}
		//
		// ------------------------------------------
		$id_page = DCE_Helper::get_the_id($settings['other_post_source']);
		// ------------------------------------------

		$idFields = '';
		$idFields = $settings['acf_field_list'];

		$galleria = DCE_Helper::get_acf_field_value( $idFields, $id_page );

		if ( ! empty($galleria) ) {
			$first_image = reset($galleria);
			if ( is_array($first_image) ) {
				// Array format
			} else {
				if ( filter_var(reset($galleria), FILTER_VALIDATE_URL) ) {
					// URL format
					$tmp = array();
					foreach ( $galleria as $image_url ) {
						$tmp[] = DCE_Helper::get_image_id($image_url);
					}
					$galleria = $tmp;
				}
				if ( is_numeric(reset($galleria)) ) {
					// ID format
					$args = array(
						'post_type' => 'attachment',
						'post__in' => $galleria,
						'post_status' => 'inherit',
						'post_mime_type' => 'image',
						'numberposts'      => 0,
						'posts_per_page' => -1,
					);
					$galleria = get_posts($args);
				}
				if ( is_object(reset($galleria)) ) {
					// URL format
					$tmp = array();
					foreach ( $galleria as $image ) {
						$tmp[] = $this->dce_get_attachment($image->ID);
					}
					$galleria = $tmp;
				}
			}
		}

		if ( $galleria ) :
			//
			$counter = 0;
			$title = '';
			//
			// ------------------------------------------
			//
			$image_size = $settings['size_size'];
			// ------------------------------------------
			$enable_lightbox = '';
			$lightbox_type = '';

			$elementor_lightbox = '';
			$data_elementor_open_lightbox = '';
			$data_elementor_slideshow = '';

			if ( $settings['enable_lightbox'] ) {
				$enable_lightbox = ' is-lightbox';
			}
			if ( $settings['lightbox_type'] == 'photoswipe' ) {
				$lightbox_type = ' ' . $settings['lightbox_type'];
				$data_elementor_open_lightbox = 'data-elementor-open-lightbox="no"';
			} else {
				$lightbox_type = ' dce-gallery';
				$data_elementor_slideshow = ' data-elementor-lightbox-slideshow="' . $this->get_id() . '"';
				$elementor_lightbox = ' gallery-lightbox';
				$data_elementor_open_lightbox = 'data-elementor-open-lightbox="yes"';
			}

			// ------------------------------------------
			$type_gallery = '';
			if ( ! empty($settings['gallery_type']) ) {
				$type_gallery = $settings['gallery_type'];
			}
			$type_gallery_item = '';
			$type_gallery_item_a = '';
			if ( $settings['gallery_type'] == 'hexagon' ) {
				$type_gallery = 'honeycombs';
				if ( $enable_lightbox != '' ) {
					$type_gallery_item_a = 'comb';
				} else {
					$type_gallery_item = ' comb';
				}
			}
			// ---------------------- Overlay Hover -----------------
			$overlay_hover_block = '';
			$overlay_hover_class = '';
			//if( $settings['enable_overlay_hover'] == 'yes'){
			//
			if ( $settings['gallery_type'] == 'hexagon' ) {
				$overlay_hover_block = '<span><span>';
			} else {
				$overlay_hover_block = '<span class="acfgallery-overlay_hover"></span>';
			}
			//
			$overlay_hover_class = ' is-overlay ';
			//}
			?>
			<div id="<?php echo $type_gallery; ?>-grid" class="<?php echo $type_gallery; ?>-grid dynamic_acfgallery dynamic_acfgallery-<?php echo $type_gallery . $enable_lightbox . $elementor_lightbox . $lightbox_type . $overlay_hover_class; ?> column-<?php echo $settings['columns_grid']; ?>" itemscope itemtype="http://schema.org/ImageGallery">
				<?php
				foreach ( $galleria as $image ) :

					$single_image = '';
					if ( $settings['gallery_type'] == 'single_image' && $counter >= 1 ) {
						$single_image = ' hidden';
					}

					if ( ! isset($image['id']) ) {
						$img_id = $image;
						$image = $this->dce_get_attachment($img_id);
					}
					$image_url = Group_Control_Image_Size::get_attachment_image_src($image['id'], 'size', $settings);

					// ------------------------------------------ wow
					$wow_enable = $settings['enabled_wow'];
					if ( $wow_enable == 'yes' ) {
						$wow_coeff = ( $settings['wow_coef'] ) ? $settings['wow_coef'] : 0;
						$wow_delay = ' data-wow-delay="' . ( $counter * $wow_coeff ) . 's"';
						$wow_animations = $settings['wow_animations'];
						$wow_string = ' wow ' . $wow_animations;
					} else {
						$wow_string = '';
						$wow_delay = '';
					}
					// ---------------------------------------------
					// $image['sizes'][$image_size]
					if ( $settings['gallery_type'] != 'hexagon' ) {
						echo '<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"  class="acfgallery-item grid-item' . $type_gallery_item . $single_image . $wow_string . '"' . $wow_delay . '>';
						echo '<div class="wrap-item-acfgallery">';
					}
					// ---------------------------------------------------------
					if ( $enable_lightbox ) {
						echo '<a class="' . $type_gallery_item_a . $enable_lightbox . $elementor_lightbox . '" href="' . $image['url'] . '" itemprop="contentUrl" data-size="' . $image['width'] . 'x' . $image['height'] . '"' . $data_elementor_open_lightbox . $data_elementor_slideshow . '>';
					} elseif ( $settings['enable_lightbox_link'] ) {
						echo '<a class="' . $type_gallery_item_a . '" href="' . $image['url'] . '" itemprop="contentUrl" >';
					}
					echo '<img src="' . $image_url . '" itemprop="thumbnail" alt="' . $image['alt'] . '" />';
					echo $overlay_hover_block;

					if ( $enable_lightbox || $settings['enable_lightbox_link'] ) {
						echo '</a>';
					}

					// ---------------------------------------------------------
					if ( $settings['gallery_type'] != 'hexagon' ) {
						echo '</div>';

						if ( $settings['use_desc'] != '' && ( $settings['gallery_type'] != 'diamond' && $settings['gallery_type'] != 'hexagon' ) ) {
							echo '<figcaption itemprop="description caption">';
							foreach ( $settings['use_desc'] as $value ) {

								if ( $value == 'caption' ) {
									echo '          <p class="' . $value . '" >' . $image[ $value ] . '</p>';
								} elseif ( $value == 'description' ) {
									echo '           <p class="' . $value . '">' . $image[ $value ] . '</p>';
								} elseif ( $value == 'title' ) {
									echo '            <h3 class="' . $value . '">' . $image[ $value ] . '</h3>';
								}
							}
							echo '</figcaption>';
						}
						echo '</figure>';
					}
					/* $html .= '<div class="grid-item'.$wow_string.'"'.$wow_delay.'>';
					  $html .= '<a href="'.$image['url'].'" class="gallery-lightbox">';
					  $html .= '<img src="'.$image_url.'" alt="'.$image['alt'].'" />';
					  $html .= '</a>';
					  if( $settings['use_desc'] != '' ) $html .= '<p>'.$image['caption'].'</p>';
					  $html .= '</div>'; */
					$counter ++;
				endforeach;
				?>
			</div>
			<!-- Root element of PhotoSwipe. Must have class pswp. -->
			<?php
		endif;
	}

	protected function _content_template() {

	}

	protected function dce_get_attachment( $attachment_id ) {

		$attachment = get_post($attachment_id);
		$img_src = wp_get_attachment_image_src($attachment_id, 'full');

		return array(
			'ID' => $attachment_id,
			'id' => $attachment_id,
			'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
			'caption' => $attachment->post_excerpt,
			'description' => $attachment->post_content,
			'href' => get_permalink($attachment->ID),
			'src' => $attachment->guid,
			'title' => $attachment->post_title,
			'url' => $img_src[0],
			'width' => $img_width = $img_src[1],
			'height' => $img_height = $img_src[2],
		);
	}

}
