<?php
/**
 * Elementor widget: Square Payment Link.
 *
 * @package SHC4WC
 */

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

use Elementor\Controls_Manager;
use Elementor\Widget_Base;

class SHC4WC_Elementor_Payment_Link extends Widget_Base {

	public function get_name() {
		return 'shc4wc_payment_link';
	}

	public function get_title() {
		return __( 'Square Payment Link', 'payments-connect-square' );
	}

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

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

	public function get_keywords() {
		return array( 'square', 'payment', 'pay', 'checkout', 'hosted' );
	}

	public function get_custom_help_url() {
		return 'https://wordpress.org/plugins/payments-connect-square/';
	}

	protected function register_controls() {
		$this->start_controls_section(
			'content_section',
			array(
				'label' => __( 'Payment link', 'payments-connect-square' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'amount',
			array(
				'label'       => __( 'Amount', 'payments-connect-square' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '25.00',
				'placeholder' => '25.00',
				'description' => __( 'Example: 25.00', 'payments-connect-square' ),
			)
		);

		$this->add_control(
			'label',
			array(
				'label'   => __( 'Button label', 'payments-connect-square' ),
				'type'    => Controls_Manager::TEXT,
				'default' => __( 'Pay Now', 'payments-connect-square' ),
			)
		);

		$this->add_control(
			'description',
			array(
				'label'       => __( 'Description (optional)', 'payments-connect-square' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'description' => __( 'Shown on the Square Hosted Checkout page.', 'payments-connect-square' ),
			)
		);

		$this->add_control(
			'currency',
			array(
				'label'       => __( 'Currency (optional)', 'payments-connect-square' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => 'USD',
				'description' => __( 'Leave empty to use WooCommerce store currency or USD.', 'payments-connect-square' ),
			)
		);

		$this->end_controls_section();
	}

	protected function render() {
		if ( ! class_exists( 'SHC4WC_Payment_Link' ) ) {
			return;
		}

		$settings = $this->get_settings_for_display();

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_button returns escaped HTML.
		echo SHC4WC_Payment_Link::instance()->render_button(
			array(
				'amount'      => isset( $settings['amount'] ) ? (string) $settings['amount'] : '',
				'label'       => isset( $settings['label'] ) ? (string) $settings['label'] : '',
				'description' => isset( $settings['description'] ) ? (string) $settings['description'] : '',
				'currency'    => isset( $settings['currency'] ) ? (string) $settings['currency'] : '',
			)
		);
	}
}
