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

class Gallery extends \Elementor\Widget_Base {

	public function get_style_depends() {

		wp_register_style( 'justified-gallery', plugin_dir_url( __DIR__ ) . 'assets/vendor/justifiedGallery/justifiedGallery.min.css', array(), '3.6.3' );

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

		return [
			'ignition-elementor-justified-gallery',
		];

	}

	public function get_script_depends() {

		wp_register_script( 'justified-gallery', plugin_dir_url( __DIR__ ) . 'assets/vendor/justifiedGallery/jquery.justifiedGallery.min.js', array( 'jquery' ), '3.6.3', true );

		wp_register_script( 'ignition-elementor-justified-gallery', plugin_dir_url( __DIR__ ) . 'assets/js/gallery.min.js', array( 'justified-gallery' ), IGNITION_ELEMENTOR_WIDGETS_VERSION, true );

		return [
			'ignition-elementor-justified-gallery',
		];

	}

	public function get_name() {
		return 'ignition-gallery';
	}

	public function get_title() {
		return __( 'Gallery', 'ignition-elementor-widgets' );
	}

	public function get_icon() {
		return 'eicon-gallery-justified';
	}

	public function get_categories() {
		return [ 'ignition-elementor-widgets' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_gallery',
			[
				'label' => __( 'Gallery', 'ignition-elementor-widgets' ),
			]
		);

		$this->add_control(
			'gallery',
			[
				'label' => __( 'Add Images', 'ignition-elementor-widgets' ),
				'type'  => \Elementor\Controls_Manager::GALLERY,
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Image_Size::get_type(),
			[
				'name'    => 'thumbnail',
				'exclude' => [ 'custom' ],
			]
		);

		$this->add_control(
			'gallery_link',
			[
				'label'   => __( 'Link to', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'file',
				'options' => [
					'file'       => __( 'Media File', 'ignition-elementor-widgets' ),
					'attachment' => __( 'Attachment Page', 'ignition-elementor-widgets' ),
					'none'       => __( 'None', 'ignition-elementor-widgets' ),
				],
			]
		);

		$this->add_control(
			'open_lightbox',
			[
				'label'     => __( 'Lightbox', 'elementor' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => 'no',
				'options'   => [
					'yes'     => __( 'Yes', 'ignition-elementor-widgets' ),
					'no'      => __( 'No', 'ignition-elementor-widgets' ),
				],
				'condition' => [
					'gallery_link' => 'file',
				],
			]
		);

		$this->add_control(
			'row-height',
			[
				'label'   => __( 'Row Height', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'default' => 120,
				'min'     => 50,
				'max'     => 600,
				'step'    => 10,
			]
		);

		$this->add_control(
			'gallery-margins',
			[
				'label'   => __( 'Margins', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'default' => 1,
				'min'     => 0,
				'max'     => 50,
				'step'    => 1,
			]
		);

		$this->add_control(
			'last_row',
			[
				'label'   => __( 'Last Row', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'nojustify',
				'options' => [
					'nojustify' => __( 'No Justify', 'ignition-elementor-widgets' ),
					'justify'   => __( 'Justify', 'ignition-elementor-widgets' ),
					'hide'      => __( 'Hide', 'ignition-elementor-widgets' ),
					'left'      => __( 'Left', 'ignition-elementor-widgets' ),
					'right'     => __( 'Right', 'ignition-elementor-widgets' ),
				],
			]
		);

		$this->add_control(
			'randomize',
			[
				'label'        => __( 'Randomize', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'default'      => false,
				'label_on'     => __( 'On', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'Off', 'ignition-elementor-widgets' ),
				'return_value' => true,
			]
		);

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

		$this->end_controls_section();

		$this->start_controls_section(
			'section_gallery_images',
			[
				'label' => __( 'Images', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			[
				'name'      => 'image_border',
				'label'     => __( 'Image Border', 'ignition-elementor-widgets' ),
				'selector'  => '{{WRAPPER}} .ignition-elementor-justified-gallery img',
				'separator' => 'before',
			]
		);

		$this->add_control(
			'image_border_radius',
			[
				'label'      => __( 'Border Radius', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .ignition-elementor-justified-gallery img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_caption',
			[
				'label' => __( 'Caption', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'gallery_display_caption',
			[
				'label'     => __( 'Display', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => '',
				'options'   => [
					''     => __( 'Show', 'ignition-elementor-widgets' ),
					'none' => __( 'Hide', 'ignition-elementor-widgets' ),
				],
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-justified-gallery .caption' => 'display: {{VALUE}};',
				],
			]
		);

		$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',
					],
					'justify' => [
						'title' => __( 'Justified', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-justify',
					],
				],
				'default'   => 'center',
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-justified-gallery .caption' => 'text-align: {{VALUE}};',
				],
				'condition' => [
					'gallery_display_caption' => '',
				],
			]
		);

		$this->add_control(
			'text_color',
			[
				'label'     => __( 'Text Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-justified-gallery .caption' => 'color: {{VALUE}};',
				],
				'condition' => [
					'gallery_display_caption' => '',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'      => 'typography',
				'label'     => __( 'Typography', 'ignition-elementor-widgets' ),
				'scheme'    => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_4,
				'selector'  => '{{WRAPPER}} .ignition-elementor-justified-gallery .caption',
				'condition' => [
					'gallery_display_caption' => '',
				],
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings();

		if ( ! $settings['gallery'] ) {
			return;
		}

		$ids = wp_list_pluck( $settings['gallery'], 'id' );

		$this->add_render_attribute( 'shortcode', 'ids', implode( ',', $ids ) );
		$this->add_render_attribute( 'shortcode', 'size', $settings['thumbnail_size'] );
		?>

		<div id="jg-<?php echo esc_attr( $this->get_id() ); ?>" class="hide-gallery ignition-elementor-justified-gallery" data-row-height="<?php echo intval( $settings['row-height'] ); ?>" data-margins="<?php echo intval( $settings['gallery-margins'] ); ?>" data-last-row="<?php echo esc_attr( $settings['last_row'] ); ?>" data-randomize="<?php echo esc_attr( $settings['randomize'] ? 'true' : 'false' ); ?>" data-selector="<?php echo esc_attr( 'none' === $settings['gallery_link'] ? 'div' : 'a' ); ?>">
			<?php foreach ( $ids as $id ) : ?>
				<?php
					$img      = wp_get_attachment_image_src( $id, $settings['thumbnail_size'] );
					$img_full = wp_get_attachment_url( $id );
					$att_link = get_attachment_link( $id );
					$link     = $settings['gallery_link'];
					$link_to  = 'file' === $link ? $img_full : $att_link;
					$caption  = wp_get_attachment_caption( $id );
				?>

				<?php if ( 'none' !== $link ) : ?>
					<a href="<?php echo esc_url( $link_to ); ?>" class="elementor-clickable" data-elementor-open-lightbox="<?php echo esc_attr( $settings['open_lightbox'] ); ?>" data-elementor-lightbox-slideshow="<?php echo esc_attr( $this->get_id() ); ?>">
				<?php else : ?>
					<div>
				<?php endif; ?>

				<img src="<?php echo esc_url( $img['0'] ); ?>" alt="<?php echo esc_attr( get_post_meta( $id , '_wp_attachment_image_alt', true ) ); ?>">
				<?php if ( $caption ) : ?>
					<div class="caption"><?php echo esc_html( $caption ); ?></div>
				<?php endif; ?>
				<?php if ( 'none' !== $link ) : ?>
				</a>
				<?php else : ?>
				</div>
				<?php endif; ?>
			<?php endforeach; ?>
		</div>
		<?php
	}

	protected function content_template() {}
}
