<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

use EcafeAddons\EcafeElementLoad;

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

class Ecafe_QRCode extends Widget_Base {

	public function get_name() {
		return 'ecafe-qr-code';
	}

	public function get_title() {
		return esc_html__( 'QR Code', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-qr-code-icon';
	}

	public function get_categories() {
		return array( 'essential' );
	}

	public function get_keywords() {
		return array( 'qrcode', 'code', 'qr code', 'scanner code', 'scan' );
	}

	public function get_script_depends() {
		return array( 'ecafe-qrcode' );
	}

	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}

	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	protected function register_controls() {

		$this->start_controls_section(
			'contentSection',
			array(
				'label' => esc_html__( 'QR Code', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'qrcontent',
			array(
				'label'       => esc_html__( 'Content', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => esc_html__( 'EC Addons For Elementor', 'essential-classy-addons-for-elementor' ),
				'placeholder' => esc_html__( 'Enter Content', 'essential-classy-addons-for-elementor' ),
				'dynamic'     => array( 'active' => true ),
			)
		);
		$this->add_control(
			'colordark',
			array(
				'label'   => esc_html__( 'QR Color', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#000000',
			)
		);
		$this->add_control(
			'colorLight',
			array(
				'label'   => esc_html__( 'Background Color', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#ffffff',
			)
		);
		$this->add_responsive_control(
			'qrwidth',
			array(
				'type'       => Controls_Manager::SLIDER,
				'label'      => esc_html__( 'Width', 'essential-classy-addons-for-elementor' ),
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min'  => 0,
						'max'  => 800,
						'step' => 1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 200,
				),
				'separator'  => 'before',
			)
		);
		$this->add_responsive_control(
			'qrheight',
			array(
				'type'       => Controls_Manager::SLIDER,
				'label'      => esc_html__( 'Height', 'essential-classy-addons-for-elementor' ),
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min'  => 0,
						'max'  => 800,
						'step' => 1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 200,
				),
			)
		);
		$this->add_responsive_control(
			'qrAlign',
			array(
				'label'     => esc_html__( 'Alignment', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .ec-qr-code-wrapper' => 'justify-content:{{VALUE}};',
				),
				'separator' => 'before',
			)
		);
		$this->end_controls_section();

		/*Tilt Effect Options*/
		include ECAFE_PATH . 'element/widgets/ecafe-tilt-options.php';

		/*Tippy Tooltip Options*/
		include ECAFE_PATH . 'element/widgets/ecafe-tippy-options.php';

		/*Notice panel*/
		$ecwidgetdemolink = esc_url( 'https://ecaddons.com/elements/qr-code-for-elementor/' );
		$ecwidgetdoclink  = esc_url( 'https://ecaddons.com/docs/qr-code-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';
	}

	protected function render() {

		$settings             = $this->get_settings_for_display();
		$uid                  = uniqid( 'ecqrcode' );
		$qrdata               = array();
		$qrdata['qrcontent']  = ! empty( $settings['qrcontent'] ) ? esc_attr( $settings['qrcontent'] ) : '';
		$qrdata['qrwidth']    = ! empty( $settings['qrwidth']['size'] ) ? esc_attr( $settings['qrwidth']['size'] ) : '200';
		$qrdata['qrheight']   = ! empty( $settings['qrheight']['size'] ) ? esc_attr( $settings['qrheight']['size'] ) : '200';
		$qrdata['colordark']  = ! empty( $settings['colordark'] ) ? esc_attr( $settings['colordark'] ) : '#000000';
		$qrdata['colorLight'] = ! empty( $settings['colorLight'] ) ? esc_attr( $settings['colorLight'] ) : '#ffffff';

		$tiltclass  = '';
		$tiltrender = '';
		if ( isset( $settings['tiltEffect'] ) && $settings['tiltEffect'] === 'yes' ) {
			/*Tilt Effect Render*/
			include ECAFE_PATH . 'element/widgets/ecafe-tilt-render.php';
		}
		$tippyclass  = '';
		$tippyrender = '';
		if ( isset( $settings['tippyTooltip'] ) && $settings['tippyTooltip'] === 'yes' ) {
			/*Tippy Tooltip Render*/
			include ECAFE_PATH . 'element/widgets/ecafe-tippy-render.php';
		}
		$output  = '<div id="' . esc_attr( $uid ) . '" class="ec-qr-code-wrapper ' . esc_attr( $uid ) . ' ' . esc_attr( $tiltclass ) . ' ' . esc_attr( $tippyclass ) . '" data-qrdatas="' . htmlspecialchars( json_encode( $qrdata ), ENT_QUOTES, 'UTF-8' ) . '" ' . $tiltrender . ' ' . $tippyrender . '>';
		$output .= '</div>';
		echo wp_kses_normalize_entities( $output );
	}
}
