<?php
/**
 * Elementor widget wrapper around the existing shortcode renderer.
 *
 * @package Vareon_Calculator_Suite_Lite
 */

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

/**
 * Elementor widget wrapper around the existing shortcode renderer.
 */
class VareonCalc_Lite_Elementor_Widget extends \Elementor\Widget_Base {
	/**
	 * Get widget name.
	 *
	 * @return string
	 */
	public function get_name() {
		return 'vareoncalc-lite';
	}

	/**
	 * Get widget title.
	 *
	 * @return string
	 */
	public function get_title() {
		return esc_html__( 'Vareon Calculator', 'vareon-calculator-suite-lite' );
	}

	/**
	 * Get widget icon.
	 *
	 * @return string
	 */
	public function get_icon() {
		return 'eicon-shortcode';
	}

	/**
	 * Get widget categories.
	 *
	 * @return array
	 */
	public function get_categories() {
		return array( 'general' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @return array
	 */
	public function get_keywords() {
		return array( 'vareon', 'calculator', 'bmi', 'loan', 'mortgage', 'finance' );
	}

	/**
	 * Force editor preview reload because output is server-rendered shortcode HTML.
	 *
	 * @return bool
	 */
	public function is_reload_preview_required() {
		return true;
	}

	/**
	 * Register widget controls.
	 *
	 * @return void
	 */
	protected function register_controls() {
		$this->start_controls_section(
			'section_vareoncalc',
			array(
				'label' => esc_html__( 'Calculator', 'vareon-calculator-suite-lite' ),
			)
		);

		$this->add_control(
			'type',
			array(
				'label'   => esc_html__( 'Calculator Type', 'vareon-calculator-suite-lite' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'bmi',
				'options' => vareoncalc_lite_get_elementor_calculator_options(),
			)
		);

		$this->add_control(
			'theme',
			array(
				'label'   => esc_html__( 'Theme', 'vareon-calculator-suite-lite' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => '',
				'options' => array(
					''       => esc_html__( 'Use Global Default', 'vareon-calculator-suite-lite' ),
					'light'  => esc_html__( 'Light', 'vareon-calculator-suite-lite' ),
					'dark'   => esc_html__( 'Dark', 'vareon-calculator-suite-lite' ),
					'modern' => esc_html__( 'Modern', 'vareon-calculator-suite-lite' ),
				),
			)
		);

		$this->add_control(
			'width',
			array(
				'label'       => esc_html__( 'Width', 'vareon-calculator-suite-lite' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'default'     => '100%',
				'placeholder' => '100%, 620px, 40rem',
			)
		);

		$this->add_control(
			'branding',
			array(
				'label'       => esc_html__( 'Branding', 'vareon-calculator-suite-lite' ),
				'type'        => \Elementor\Controls_Manager::SELECT,
				'default'     => '',
				'options'     => array(
					''  => esc_html__( 'Use Global Setting', 'vareon-calculator-suite-lite' ),
					'1' => esc_html__( 'Show', 'vareon-calculator-suite-lite' ),
					'0' => esc_html__( 'Hide', 'vareon-calculator-suite-lite' ),
				),
				'description' => esc_html__( 'Override the plugin-wide branding setting for this embed.', 'vareon-calculator-suite-lite' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render widget output.
	 *
	 * @return void
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();
		$type     = isset( $settings['type'] ) ? sanitize_key( (string) $settings['type'] ) : 'bmi';
		$width    = isset( $settings['width'] ) ? vareoncalc_sanitize_css_width( (string) $settings['width'] ) : '100%';
		$theme    = isset( $settings['theme'] ) ? sanitize_key( (string) $settings['theme'] ) : '';
		$branding = isset( $settings['branding'] ) ? (string) $settings['branding'] : '';

		if ( ! vareoncalc_is_calculator_available( $type ) ) {
			return;
		}

		$atts = array(
			'type'  => $type,
			'width' => $width,
		);

		if ( in_array( $theme, vareoncalc_get_available_themes(), true ) ) {
			$atts['theme'] = $theme;
		}

		if ( '' !== $branding ) {
			$atts['branding'] = '1' === $branding ? '1' : '0';
		}

		$output = do_shortcode( shortcode_unautop( vareoncalc_lite_build_elementor_shortcode( $atts ) ) );
		echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Shortcode output is sanitized by the plugin render path.
	}

	/**
	 * Render plain content for editor exports.
	 *
	 * @return void
	 */
	public function render_plain_content() {
		$settings = $this->get_settings_for_display();
		$type     = isset( $settings['type'] ) ? sanitize_key( (string) $settings['type'] ) : 'bmi';

		echo esc_html( '[vareon type="' . $type . '"]' );
	}
}
