<?php
use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Elementor_Info_Box_Popup extends Widget_Base {
	public function get_name() {
		return 'info_box_popup';
	}

	public function get_title() {
		return esc_html__( 'Info Box Popup', 'elementor-addon' );
	}

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

	public function get_categories() {
		return [ 'general' ];
	}

	public function get_keywords() {
		return [ 'popup', 'modal', 'button', 'czytaj wiecej', 'content' ];
	}

	public function get_style_depends() {
		return [ 'elementor-addon-main-css' ];
	}

	public function get_script_depends() {
		return [ 'elementor-addon-main-js' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			[
				'label' => esc_html__( 'Content', 'elementor-addon' ),
			]
		);

		$this->add_control(
			'button_text',
			[
				'label'       => esc_html__( 'Button Text', 'elementor-addon' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Czytaj wiecej', 'elementor-addon' ),
				'placeholder' => esc_html__( 'Czytaj wiecej', 'elementor-addon' ),
				'label_block' => true,
			]
		);

		$this->add_control(
			'popup_content',
			[
				'label'      => esc_html__( 'Popup Content', 'elementor-addon' ),
				'type'       => Controls_Manager::WYSIWYG,
				'default'    => '<p>' . esc_html__( 'Write your popup content here.', 'elementor-addon' ) . '</p>',
				'show_label' => true,
			]
		);

		$this->add_control(
			'close_label',
			[
				'label'       => esc_html__( 'Close Button Label', 'elementor-addon' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Close popup', 'elementor-addon' ),
				'label_block' => true,
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_button_style',
			[
				'label' => esc_html__( 'Button', 'elementor-addon' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_responsive_control(
			'button_align',
			[
				'label'     => esc_html__( 'Alignment', 'elementor-addon' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'left'   => [
						'title' => esc_html__( 'Left', 'elementor-addon' ),
						'icon'  => 'eicon-text-align-left',
					],
					'center' => [
						'title' => esc_html__( 'Center', 'elementor-addon' ),
						'icon'  => 'eicon-text-align-center',
					],
					'right'  => [
						'title' => esc_html__( 'Right', 'elementor-addon' ),
						'icon'  => 'eicon-text-align-right',
					],
				],
				'default'   => 'left',
				'selectors' => [
					'{{WRAPPER}} .ea-popup-widget' => 'text-align: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'button_full_width',
			[
				'label'        => esc_html__( 'Full Width', 'elementor-addon' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'elementor-addon' ),
				'label_off'    => esc_html__( 'No', 'elementor-addon' ),
				'return_value' => 'yes',
				'selectors'    => [
					'{{WRAPPER}} .ea-popup-button' => 'width: 100%;',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'button_typography',
				'selector' => '{{WRAPPER}} .ea-popup-button',
			]
		);

		$this->add_responsive_control(
			'button_padding',
			[
				'label'      => esc_html__( 'Padding', 'elementor-addon' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', 'rem' ],
				'selectors'  => [
					'{{WRAPPER}} .ea-popup-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'button_border',
				'selector' => '{{WRAPPER}} .ea-popup-button',
			]
		);

		$this->add_responsive_control(
			'button_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'elementor-addon' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em', 'rem' ],
				'selectors'  => [
					'{{WRAPPER}} .ea-popup-button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'button_box_shadow',
				'selector' => '{{WRAPPER}} .ea-popup-button',
			]
		);

		$this->start_controls_tabs( 'button_style_tabs' );

		$this->start_controls_tab(
			'button_style_normal',
			[
				'label' => esc_html__( 'Normal', 'elementor-addon' ),
			]
		);

		$this->add_control(
			'button_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ea-popup-button' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'button_background_color',
			[
				'label'     => esc_html__( 'Background Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ea-popup-button' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'button_style_hover',
			[
				'label' => esc_html__( 'Hover', 'elementor-addon' ),
			]
		);

		$this->add_control(
			'button_hover_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ea-popup-button:hover, {{WRAPPER}} .ea-popup-button:focus' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'button_hover_background_color',
			[
				'label'     => esc_html__( 'Background Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ea-popup-button:hover, {{WRAPPER}} .ea-popup-button:focus' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'button_hover_border_color',
			[
				'label'     => esc_html__( 'Border Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ea-popup-button:hover, {{WRAPPER}} .ea-popup-button:focus' => 'border-color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

		$this->start_controls_section(
			'section_overlay_style',
			[
				'label' => esc_html__( 'Overlay', 'elementor-addon' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'overlay_background_color',
			[
				'label'     => esc_html__( 'Background Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => 'rgba(0, 0, 0, 0.55)',
				'selectors' => [
					'{{WRAPPER}} .ea-popup-modal' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_popup_style',
			[
				'label' => esc_html__( 'Popup Box', 'elementor-addon' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_responsive_control(
			'popup_width',
			[
				'label'      => esc_html__( 'Width', 'elementor-addon' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%', 'vw' ],
				'range'      => [
					'px' => [
						'min' => 260,
						'max' => 1200,
					],
					'%'  => [
						'min' => 20,
						'max' => 100,
					],
					'vw' => [
						'min' => 20,
						'max' => 100,
					],
				],
				'default'    => [
					'unit' => 'px',
					'size' => 720,
				],
				'selectors'  => [
					'{{WRAPPER}} .ea-popup-dialog' => 'max-width: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'popup_padding',
			[
				'label'      => esc_html__( 'Padding', 'elementor-addon' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', 'rem' ],
				'selectors'  => [
					'{{WRAPPER}} .ea-popup-dialog' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'popup_background',
				'types'    => [ 'classic', 'gradient' ],
				'selector' => '{{WRAPPER}} .ea-popup-dialog',
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'popup_border',
				'selector' => '{{WRAPPER}} .ea-popup-dialog',
			]
		);

		$this->add_responsive_control(
			'popup_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'elementor-addon' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em', 'rem' ],
				'selectors'  => [
					'{{WRAPPER}} .ea-popup-dialog' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'popup_box_shadow',
				'selector' => '{{WRAPPER}} .ea-popup-dialog',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_popup_content_style',
			[
				'label' => esc_html__( 'Popup Content', 'elementor-addon' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'content_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ea-popup-content' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'content_typography',
				'selector' => '{{WRAPPER}} .ea-popup-content',
			]
		);

		$this->add_responsive_control(
			'content_align',
			[
				'label'     => esc_html__( 'Alignment', 'elementor-addon' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'left'   => [
						'title' => esc_html__( 'Left', 'elementor-addon' ),
						'icon'  => 'eicon-text-align-left',
					],
					'center' => [
						'title' => esc_html__( 'Center', 'elementor-addon' ),
						'icon'  => 'eicon-text-align-center',
					],
					'right'  => [
						'title' => esc_html__( 'Right', 'elementor-addon' ),
						'icon'  => 'eicon-text-align-right',
					],
				],
				'selectors' => [
					'{{WRAPPER}} .ea-popup-content' => 'text-align: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_close_style',
			[
				'label' => esc_html__( 'Close Button', 'elementor-addon' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'close_color',
			[
				'label'     => esc_html__( 'Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ea-popup-close' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'close_background_color',
			[
				'label'     => esc_html__( 'Background Color', 'elementor-addon' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ea-popup-close' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'close_size',
			[
				'label'      => esc_html__( 'Size', 'elementor-addon' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em', 'rem' ],
				'range'      => [
					'px' => [
						'min' => 24,
						'max' => 80,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .ea-popup-close' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'close_icon_size',
			[
				'label'      => esc_html__( 'Icon Size', 'elementor-addon' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em', 'rem' ],
				'range'      => [
					'px' => [
						'min' => 12,
						'max' => 48,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .ea-popup-close' => 'font-size: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings   = $this->get_settings_for_display();
		$widget_id  = 'ea-popup-' . $this->get_id();
		$button_id  = $widget_id . '-button';
		$dialog_id  = $widget_id . '-dialog';
		$button_text = ! empty( $settings['button_text'] ) ? $settings['button_text'] : esc_html__( 'Czytaj wiecej', 'elementor-addon' );
		$close_label = ! empty( $settings['close_label'] ) ? $settings['close_label'] : esc_html__( 'Close popup', 'elementor-addon' );

		$this->add_render_attribute(
			'wrapper',
			[
				'class' => 'ea-popup-widget',
			]
		);

		$this->add_render_attribute(
			'button',
			[
				'id'            => $button_id,
				'class'         => 'ea-popup-button',
				'type'          => 'button',
				'aria-haspopup' => 'dialog',
				'aria-controls' => $dialog_id,
				'aria-expanded' => 'false',
			]
		);
		?>
		<div <?php $this->print_render_attribute_string( 'wrapper' ); ?>>
			<button <?php $this->print_render_attribute_string( 'button' ); ?>>
				<?php echo esc_html( $button_text ); ?>
			</button>

			<dialog class="ea-popup-modal" data-ea-popup-modal aria-hidden="true">
				<div class="ea-popup-dialog" id="<?php echo esc_attr( $dialog_id ); ?>" aria-labelledby="<?php echo esc_attr( $button_id ); ?>">
					<button class="ea-popup-close" type="button" data-ea-popup-close aria-label="<?php echo esc_attr( $close_label ); ?>">
						<span aria-hidden="true">&times;</span>
					</button>

					<div class="ea-popup-content">
						<?php echo wp_kses_post( $this->parse_text_editor( $settings['popup_content'] ) ); ?>
					</div>
				</div>
			</dialog>
		</div>
		<?php
	}
}
