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

class Food_Menu extends \Elementor\Widget_Base {

	public function get_style_depends() {

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

		return [
			'ignition-elementor-food-menu',
		];

	}

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

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

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

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

	protected function register_controls() {
		$this->start_controls_section(
			'section_menu_content',
			[
				'label' => esc_html__( 'Menu', 'ignition-elementor-widgets' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_responsive_control(
			'columns',
			[
				'label' => esc_html__( 'Menu Columns', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min'  => 1,
						'max'  => 3,
						'step' => 1,
					],
				],
				'default' => [
					'unit' => 'px',
					'size' => 2,
				],
				'tablet_default' => [
					'unit' => 'px',
					'size' => 1,
				],
				'mobile_default' => [
					'unit' => 'px',
					'size' => 1,
				],
			]
		);

		$this->add_responsive_control(
			'gutter',
			[
				'label' => esc_html__( 'Menu Gutter', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min'  => 1,
						'max'  => 250,
						'step' => 1,
					],
				],
				'default' => [
					'unit' => 'px',
					'size' => 30,
				],
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-food-menu' => 'grid-gap: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'image_visibility',
			[
				'type' => \Elementor\Controls_Manager::SELECT,
				'label' => esc_html__( 'Image Visibility', 'ignition-elementor-widgets' ),
				'options' => [
					'block' => esc_html__( 'Show', 'ignition-elementor-widgets' ),
					'none'  => esc_html__( 'Hide', 'ignition-elementor-widgets' ),
				],
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-food-menu-item-image' => 'display: {{VALUE}}',
				],
				'default'        => 'block',
				'tablet_default' => 'block',
				'mobile_default' => 'block',
			]
		);

		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'image',
			[
				'label' => esc_html__( 'Choose Image', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::MEDIA,
				'dynamic' => [
					'active' => true,
				],
				'default' => [
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				],
			]
		);

		$repeater->add_group_control(
			\Elementor\Group_Control_Image_Size::get_type(),
			[
				'name' => 'image', // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `image_size` and `image_custom_dimension`.
				'default' => 'thumbnail',
				'separator' => 'none',
			]
		);

		$repeater->add_responsive_control(
			'width',
			[
				'type' => \Elementor\Controls_Manager::SLIDER,
				'label' => esc_html__( 'Image width', 'ignition-elementor-widgets' ),
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 1000,
					],
				],
				'default' => [
					'size' => 90,
					'unit' => 'px',
				],
				'selectors' => [
					'{{WRAPPER}} {{CURRENT_ITEM}} .ignition-elementor-food-menu-item-image img' => 'width: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$repeater->add_control(
			'border_radius',
			[
				'type' => \Elementor\Controls_Manager::SLIDER,
				'label' => esc_html__( 'Image border radius (%)', 'ignition-elementor-widgets' ),
				'range' => [
					'%' => [
						'min' => 0,
						'max' => 50,
					],
				],
				'default' => [
					'size' => 0,
					'unit' => '%',
				],
				'selectors' => [
					'{{WRAPPER}} {{CURRENT_ITEM}} .ignition-elementor-food-menu-item-image img' => 'border-radius: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$repeater->add_control(
			'link_to',
			[
				'label' => esc_html__( 'Link', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'none',
				'options' => [
					'none' => esc_html__( 'None', 'ignition-elementor-widgets' ),
					'file' => esc_html__( 'Media File', 'ignition-elementor-widgets' ),
					'custom' => esc_html__( 'Custom URL', 'ignition-elementor-widgets' ),
				],
			]
		);

		$repeater->add_control(
			'image_link',
			[
				'label'       => esc_html__( 'Link', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::URL,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => 'https://your-link.com',
				'condition'   => [
					'link_to' => 'custom',
				],
				'show_label'  => false,
			]
		);

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

		$repeater->add_control(
			'name_heading',
			[
				'label' => esc_html__( 'Menu Item Name', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$repeater->add_control(
			'name',
			[
				'label' => esc_html__( 'Name', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::TEXTAREA,
				'dynamic' => [
					'active' => true,
				],
				'placeholder' => esc_html__( 'Item name', 'ignition-elementor-widgets' ),
			]
		);

		$repeater->add_control(
			'name_link',
			[
				'label' => esc_html__( 'Link', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::URL,
				'dynamic' => [
					'active' => true,
				],
				'default' => [
					'url' => '',
				],
			]
		);

		$repeater->add_control(
			'header_size',
			[
				'label' => esc_html__( 'HTML Tag', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'options' => [
					'h1' => 'H1',
					'h2' => 'H2',
					'h3' => 'H3',
					'h4' => 'H4',
					'h5' => 'H5',
					'h6' => 'H6',
					'div' => 'div',
					'span' => 'span',
					'p' => 'p',
				],
				'default' => 'h2',
			]
		);

		$repeater->add_control(
			'price_heading',
			[
				'label' => esc_html__( 'Menu Item Price', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$repeater->add_control(
			'price',
			[
				'label' => esc_html__( 'Price', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Item price', 'ignition-elementor-widgets' ),
			]
		);

		$repeater->add_control(
			'description_heading',
			[
				'label' => esc_html__( 'Menu Item Description', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);

		$repeater->add_control(
			'description',
			[
				'label' => esc_html__( 'Description', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::TEXTAREA,
				'placeholder' => esc_html__( 'Item description', 'ignition-elementor-widgets' ),
			]
		);

		$repeater->add_responsive_control(
			'description_align',
			[
				'label' => esc_html__( 'Alignment', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => esc_html__( 'Left', 'ignition-elementor-widgets' ),
						'icon' => 'eicon-text-align-left',
					],
					'center' => [
						'title' => esc_html__( 'Center', 'ignition-elementor-widgets' ),
						'icon' => 'eicon-text-align-center',
					],
					'right' => [
						'title' => esc_html__( 'Right', 'ignition-elementor-widgets' ),
						'icon' => 'eicon-text-align-right',
					],
					'justify' => [
						'title' => esc_html__( 'Justified', 'ignition-elementor-widgets' ),
						'icon' => 'eicon-text-align-justify',
					],
				],
				'default' => '',
				'selectors' => [
					'{{WRAPPER}} {{CURRENT_ITEM}} .ignition-elementor-food-menu-item-description' => 'text-align: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'menu',
			[
				'label' => esc_html__( 'Menu Items', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[
						'image_size'    => 'thumbnail',
						'border_radius' => [
							'size' => 0,
							'unit' => '%',
						],
						'name'          => 'Grand Seafood Platter',
						'header_size'   => 'h4',
						'price'         => '$13.95',
						'description'   => 'Etiam at risus et justo dignissim congue. Donec congue lacinia dui, a porttitor lectus condimentum laoreet',
					],
					[
						'image_size'    => 'thumbnail',
						'border_radius' => [
							'size' => 0,
							'unit' => '%',
						],
						'name'          => 'Escargots Petits Gris',
						'header_size'   => 'h4',
						'price'         => '$3.95',
						'description'   => 'Etiam at risus et justo dignissim congue. Donec congue lacinia dui, a porttitor lectus condimentum laoreet',
					],
				],
				'title_field' => '{{{ name }}}',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_menu_name',
			[
				'label' => esc_html__( 'Menu Name', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'name_color',
			[
				'label' => esc_html__( 'Text Color', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-food-menu-item-name' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'name_typography',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-name',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Text_Stroke::get_type(),
			[
				'name' => 'name_text_stroke',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-name',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Text_Shadow::get_type(),
			[
				'name' => 'name_text_shadow',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-name',
			]
		);

		$this->add_control(
			'name_blend_mode',
			[
				'label' => esc_html__( 'Blend Mode', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'options' => [
					'' => esc_html__( 'Normal', 'ignition-elementor-widgets' ),
					'multiply' => 'Multiply',
					'screen' => 'Screen',
					'overlay' => 'Overlay',
					'darken' => 'Darken',
					'lighten' => 'Lighten',
					'color-dodge' => 'Color Dodge',
					'saturation' => 'Saturation',
					'color' => 'Color',
					'difference' => 'Difference',
					'exclusion' => 'Exclusion',
					'hue' => 'Hue',
					'luminosity' => 'Luminosity',
				],
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-food-menu-item-name' => 'mix-blend-mode: {{VALUE}}',
				],
				'separator' => 'none',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_menu_price',
			[
				'label' => esc_html__( 'Menu Price', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'price_color',
			[
				'label' => esc_html__( 'Text Color', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-food-menu-item-price' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'price_typography',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-price',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Text_Stroke::get_type(),
			[
				'name' => 'price_text_stroke',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-price',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Text_Shadow::get_type(),
			[
				'name' => 'price_text_shadow',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-price',
			]
		);

		$this->add_control(
			'price_blend_mode',
			[
				'label' => esc_html__( 'Blend Mode', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'options' => [
					'' => esc_html__( 'Normal', 'ignition-elementor-widgets' ),
					'multiply' => 'Multiply',
					'screen' => 'Screen',
					'overlay' => 'Overlay',
					'darken' => 'Darken',
					'lighten' => 'Lighten',
					'color-dodge' => 'Color Dodge',
					'saturation' => 'Saturation',
					'color' => 'Color',
					'difference' => 'Difference',
					'exclusion' => 'Exclusion',
					'hue' => 'Hue',
					'luminosity' => 'Luminosity',
				],
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-food-menu-item-price' => 'mix-blend-mode: {{VALUE}}',
				],
				'separator' => 'none',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_menu_description',
			[
				'label' => esc_html__( 'Menu Description', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'description_color',
			[
				'label' => esc_html__( 'Text Color', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-food-menu-item-description' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'description_typography',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-description',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Text_Stroke::get_type(),
			[
				'name' => 'description_text_stroke',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-description',
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Text_Shadow::get_type(),
			[
				'name' => 'description_text_shadow',
				'selector' => '{{WRAPPER}} .ignition-elementor-food-menu-item-description',
			]
		);

		$this->add_control(
			'description_blend_mode',
			[
				'label' => esc_html__( 'Blend Mode', 'ignition-elementor-widgets' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'options' => [
					'' => esc_html__( 'Normal', 'ignition-elementor-widgets' ),
					'multiply' => 'Multiply',
					'screen' => 'Screen',
					'overlay' => 'Overlay',
					'darken' => 'Darken',
					'lighten' => 'Lighten',
					'color-dodge' => 'Color Dodge',
					'saturation' => 'Saturation',
					'color' => 'Color',
					'difference' => 'Difference',
					'exclusion' => 'Exclusion',
					'hue' => 'Hue',
					'luminosity' => 'Luminosity',
				],
				'selectors' => [
					'{{WRAPPER}} .ignition-elementor-food-menu-item-description' => 'mix-blend-mode: {{VALUE}}',
				],
				'separator' => 'none',
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Check if the current widget has caption
	 *
	 * @access private
	 * @since 1.2.0
	 *
	 * @param array $settings
	 *
	 * @return boolean
	 */
	private function has_caption( $settings ) {
		return ( ! empty( $settings['caption_source'] ) && 'none' !== $settings['caption_source'] );
	}

	/**
	 * Get the caption for current widget.
	 *
	 * @access private
	 * @since 1.2.0
	 * @param $settings
	 *
	 * @return string
	 */
	private function get_caption( $settings ) {
		$caption = '';
		if ( ! empty( $settings['caption_source'] ) ) {
			switch ( $settings['caption_source'] ) {
				case 'attachment':
					$caption = wp_get_attachment_caption( $settings['image']['id'] );
					break;
				case 'custom':
					$caption = ! \Elementor\Utils::is_empty( $settings['caption'] ) ? $settings['caption'] : '';
			}
		}
		return $caption;
	}

	/**
	 * Render icon box widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.0
	 * @access protected
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();

		$columns_tablet = isset( $settings['columns_tablet']['size'] ) ? $settings['columns_tablet']['size'] : 1;
		$columns_mobile = isset( $settings['columns_mobile']['size'] ) ? $settings['columns_mobile']['size'] : 1;

		?>
		<div class="ignition-elementor-food-menu ignition-elementor-food-menu-columns-desktop-<?php echo intval( $settings['columns']['size'] );?> ignition-elementor-food-menu-columns-tablet-<?php echo intval( $columns_tablet );?> ignition-elementor-food-menu-columns-mobile-<?php echo intval( $columns_mobile );?>">
			<?php foreach ( $settings['menu'] as $index => $item ) :

				$has_caption = $this->has_caption( $item );

				$link = $this->get_link_url( $item );

				if ( $link ) {
					$this->remove_render_attribute( 'image_link' );
					$this->add_link_attributes( 'image_link', $link );

					if ( \Elementor\Plugin::instance()->editor->is_edit_mode() ) {
						$this->add_render_attribute( 'image_link', [
							'class' => 'elementor-clickable',
						] );
					}

					if ( 'custom' !== $item['link_to'] ) {
						$this->add_lightbox_data_attributes( $item['image_link'], $item['image']['id'], $item['open_lightbox'] );
					}
				}

				$name_link = !empty ( $item['name_link']['url'] )  ? $item['name_link'] : false;

				if ( $name_link ) {
					$this->remove_render_attribute( 'name_link' );
					$this->add_link_attributes( 'name_link', $name_link );

					if ( \Elementor\Plugin::instance()->editor->is_edit_mode() ) {
						$this->add_render_attribute( 'name_link', [
							'class' => 'elementor-clickable',
						] );
					}
				}

				?>
				<div class="ignition-elementor-food-menu-item elementor-repeater-item-<?php echo esc_attr( $item['_id'] ); ?>">
					<figure class="ignition-elementor-food-menu-item-image">
						<?php if ( $link ) : ?>
							<a <?php $this->print_render_attribute_string( 'image_link' ); ?>>
						<?php endif; ?>
						<?php \Elementor\Group_Control_Image_Size::print_attachment_image_html( $item ); ?>
						<?php if ( $link ) : ?>
							</a>
						<?php endif; ?>
						<?php if ( $has_caption ) : ?>
							<figcaption class="widget-image-caption wp-caption-text"><?php
								echo wp_kses_post( $this->get_caption( $item ) );
							?></figcaption>
						<?php endif; ?>
					</figure>
				<div class="ignition-elementor-food-menu-wrapper ignition-elementor-food-menu-item-wrapper">
					<?php if ( ! \Elementor\Utils::is_empty( $item['name'] ) ) : ?>
						<<?php \Elementor\Utils::print_validated_html_tag( $item['header_size'] ); ?> class="ignition-elementor-food-menu-item-name">
						<?php if ( $name_link ) : ?>
							<a <?php $this->print_render_attribute_string( 'name_link' ); ?>>
						<?php endif; ?>
							<?php $this->print_unescaped_setting( 'name', 'menu', $index ); ?>
						<?php if ( $name_link ) : ?>
						</a>
						<?php endif; ?>
						</<?php \Elementor\Utils::print_validated_html_tag( $item['header_size'] ); ?>>
					<?php endif; ?>
					<?php if ( ! \Elementor\Utils::is_empty( $item['price'] ) ) : ?>
						<p class="ignition-elementor-food-menu-item-price"><?php $this->print_unescaped_setting( 'price', 'menu', $index ); ?></p>
					<?php endif; ?>
					<?php if ( ! \Elementor\Utils::is_empty( $item['description'] ) ) : ?>
						<p class="ignition-elementor-food-menu-item-description"><?php $this->print_unescaped_setting( 'description', 'menu', $index ); ?></p>
					<?php endif; ?>
				</div>
			</div>
			<?php endforeach; ?>
		</div>
		<?php
	}

	/**
	 * Retrieve image widget link URL.
	 *
	 * @since 1.2.0
	 * @access private
	 *
	 * @param array $settings
	 *
	 * @return array|string|false An array/string containing the link URL, or false if no link.
	 */
	private function get_link_url( $settings ) {

		if ( 'none' === $settings['link_to'] ) {
			return false;
		}

		if ( 'custom' === $settings['link_to'] ) {
			if ( empty( $settings['image_link']['url'] ) ) {
				return false;
			}

			return $settings['image_link'];
		}

		return [
			'url' => $settings['image']['url'],
		];
	}
}
