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

class Image_Accordion extends \Elementor\Widget_Base {
	public function get_style_depends() {
		wp_register_style( 'ignition-elementor-image-accordion', plugin_dir_url( __DIR__ ) . 'assets/css/accordion.min.css', array(), IGNITION_ELEMENTOR_WIDGETS_VERSION );

		return [
			'ignition-elementor-image-accordion',
		];

	}

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

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

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

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

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

		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'accordion_image', [
				'label'   => __( 'Choose Image', 'plugin-domain' ),
				'type'    => \Elementor\Controls_Manager::MEDIA,
				'default' => [
					'url' => \Elementor\Utils::get_placeholder_image_src(),
				],
			]
		);

		$repeater->add_control(
			'accordion_item_is_active', [
				'label'        => __( 'Active Item', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Yes', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'No', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => '',
			]
		);

		$repeater->add_control(
			'accordion_item_title', [
				'label'       => __( 'Item Title', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'label_block' => true,
				'placeholder' => __( 'Item Title', 'ignition-elementor-widgets' ),
			]
		);

		$repeater->add_control(
			'accordion_item_text', [
				'label'       => __( 'Item Text', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'label_block' => true,
				'placeholder' => __( 'Text', 'ignition-elementor-widgets' ),
			]
		);

		$repeater->add_control(
			'accordion_item_url', [
				'label'         => __( 'Link', 'plugin-domain' ),
				'type'          => \Elementor\Controls_Manager::URL,
				'placeholder'   => __( 'https://your-link.com', 'plugin-domain' ),
				'show_external' => true,
				'default'       => [
					'url'         => '',
					'is_external' => true,
					'nofollow'    => true,
				],
			]
		);

		$this->add_control(
			'accordion',
			[
				'label'       => __( 'Accordion', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => __( 'Accordion Item', 'ignition-elementor-widgets' ),
			]
		);

		$this->add_responsive_control(
			'height',
			[
				'label'           => __( 'Accordion height', 'ignition-elementor-widgets' ),
				'type'            => \Elementor\Controls_Manager::SLIDER,
				'size_units'      => [ 'px' ],
				'range'           => [
					'px' => [
						'min'  => 50,
						'max'  => 1000,
						'step' => 1,
					],
				],
				'devices'         => [ 'desktop', 'tablet', 'mobile' ],
				'default' => [
					'size' => 400,
					'unit' => 'px',
				],
				'tablet_default'  => [
					'size' => 250,
					'unit' => 'px',
				],
				'mobile_default'  => [
					'size' => 150,
					'unit' => 'px',
				],
				'selectors'       => [
					'{{WRAPPER}} .ignition-accordion-container, {{WRAPPER}} .ignition-accordion-container ul li' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'expanded_height',
			[
				'label'          => __( 'Accordion hover height', 'ignition-elementor-widgets' ),
				'type'           => \Elementor\Controls_Manager::SLIDER,
				'size_units'     => [ 'px' ],
				'range'          => [
					'px' => [
						'min'  => 50,
						'max'  => 1000,
						'step' => 1,
					],
				],
				'devices'        => [ 'mobile' ],
				'mobile_default' => [
					'size' => 250,
					'unit' => 'px',
				],
				'selectors'      => [
					'{{WRAPPER}} .ignition-accordion-container ul li:hover' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'show_text',
			[
				'label'        => __( 'Show title & caption', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'ignition-elementor-widgets' ),
				'label_off'    => __( 'Hide', 'ignition-elementor-widgets' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'text_align_horizontal',
			[
				'label'     => __( 'Horizontal text alignment', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => 'left',
				'options'   => [
					'left'    => __( 'Left', 'ignition-elementor-widgets' ),
					'center'  => __( 'Center', 'ignition-elementor-widgets' ),
					'right'   => __( 'Right', 'ignition-elementor-widgets' ),
					'justify' => __( 'Justify', 'ignition-elementor-widgets' ),
				],
				'selectors' => [
					'{{WRAPPER}} .ignition-accordion-container ul li div span .ignition-accordion-content' => 'text-align: {{OPTION}};',
				],
				'condition' => [
					'show_text' => 'yes',
				],
			]
		);

		$this->add_control(
			'text_align_vertical',
			[
				'label'     => __( 'Vertical text alignment', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => 'bottom',
				'options'   => [
					'top'    => __( 'Top', 'ignition-elementor-widgets' ),
					'middle' => __( 'Middle', 'ignition-elementor-widgets' ),
					'bottom' => __( 'Bottom', 'ignition-elementor-widgets' ),
				],
				'selectors' => [
					'{{WRAPPER}} .ignition-accordion-container ul li div span .ignition-accordion-content' => 'vertical-align: {{OPTION}};',
				],
				'condition' => [
					'show_text' => 'yes',
				],
			]
		);

		$this->end_controls_section();

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

		$this->add_control(
			'title_color',
			[
				'label'     => __( 'Title Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#FFF',
				'selectors' => [
					'{{WRAPPER}} .ignition-accordion-container h2' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'     => 'title_typography',
				'label'    => __( 'Title Typography', 'ignition-elementor-widgets' ),
				'selector' => '{{WRAPPER}} .ignition-accordion-container h2',
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_3,
			]
		);

		$this->add_control(
			'caption_color',
			[
				'label'     => __( 'Caption Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#FFF',
				'selectors' => [
					'{{WRAPPER}} .ignition-accordion-container p' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'     => 'caption_typography',
				'label'    => __( 'Caption Typography', 'ignition-elementor-widgets' ),
				'selector' => '{{WRAPPER}} .ignition-accordion-container p',
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_3,
			]
		);

		$this->add_control(
			'overlay_color',
			[
				'label'     => __( 'Image overlay hover color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'alpha'     => true,
				'default'   => 'rgba(0,0,0,.4)',
				'selectors' => [
					'{{WRAPPER}} .ignition-accordion-container ul:hover li.active span' => 'background-color: transparent;',
					'{{WRAPPER}} .ignition-accordion-container ul li.active span,{{WRAPPER}} .ignition-accordion-container ul:hover li.active:hover span,{{WRAPPER}} .ignition-accordion-container ul:focus li.active:focus span,{{WRAPPER}} .ignition-accordion-container ul li:hover span,{{WRAPPER}} .ignition-accordion-container ul li:focus span' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings  = $this->get_settings();
		$accordion = $settings['accordion'];
		$show_text = $settings['show_text'];

		if ( ! $accordion ) {
			return;
		}

		?>
			<div class="ignition-accordion-container">
				<ul>
					<?php foreach ( $accordion as $accordion_item ) : ?>
						<?php
							$img          = wp_get_attachment_url( $accordion_item['accordion_image']['id'] );
							$title        = $accordion_item['accordion_item_title'];
							$text         = $accordion_item['accordion_item_text'];
							$active_class = 'yes' === $accordion_item['accordion_item_is_active'] ? 'active' : '';
							$url          = $accordion_item['accordion_item_url'];
							$target       = $url['is_external'] ? ' target="_blank"' : '';
							$nofollow     = $url['nofollow'] ? ' rel="nofollow"' : '';
						?>
						<li class="<?php echo esc_attr( $active_class ); ?>" style="background-image:url('<?php echo esc_url_raw( $img ); ?>');">
							<div>
								<span>
									<?php if ( 'yes' === $show_text && ( $title || $text ) ) : ?>
										<div class="ignition-accordion-content">
											<?php if ( $url['url'] ) : ?>
												<a href="<?php echo esc_url_raw( $url['url'] ); ?>" <?php echo $target . ' ' . $nofollow; ?>>
											<?php endif; ?>
												<?php if ( $title ) : ?>
													<h2><?php echo esc_html( $title ); ?></h2>
												<?php endif; ?>
												<?php if ( $text ) : ?>
													<p><?php echo esc_html( $text ); ?></p>
												<?php endif; ?>
											<?php if ( $url['url'] ) : ?>
												</a>
											<?php endif; ?>
										</div>
									<?php endif; ?>
								</span>
							</div>
						</li>
					<?php endforeach; ?>
				</ul>
			</div>
		<?php
	}

	protected function content_template() {}
}
