<?php
/**
 * Renders and enqueues the front-end chat widget.
 */

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

class CPECS_Widget {

	/** @var CPECS_Settings */
	private $settings;

	public function __construct( CPECS_Settings $settings ) {
		$this->settings = $settings;

		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
		add_action( 'wp_footer', array( $this, 'render' ) );
	}

	public function enqueue() {
		if ( ! $this->settings->get( 'enabled' ) ) {
			return;
		}

		wp_enqueue_style(
			'cpecs-widget',
			CPECS_PLUGIN_URL . 'assets/css/widget.css',
			array(),
			CPECS_VERSION
		);

		wp_enqueue_script(
			'cpecs-widget',
			CPECS_PLUGIN_URL . 'assets/js/widget.js',
			array(),
			CPECS_VERSION,
			true
		);

		$data = array(
			'restUrl'  => esc_url_raw( rest_url( 'cpecs/v1' ) ),
			'nonce'    => wp_create_nonce( 'cpecs_widget' ),
			'color'    => sanitize_hex_color( $this->settings->get( 'widget_color' ) ),
			'position' => $this->settings->get( 'widget_position' ),
			'pollMs'   => 4000,
			'branding' => array(
				'show' => (bool) $this->settings->get( 'show_branding' ),
				'name' => CPECS_BRAND_NAME,
				'url'  => CPECS_BRAND_URL,
			),
			'i18n'     => array(
				'headerTitle'  => get_bloginfo( 'name' ),
				'inputPlaceholder' => __( 'Type a message…', 'capripio-easy-chat-support' ),
				'send'         => __( 'Send', 'capripio-easy-chat-support' ),
				'startPrompt'  => __( 'Chat with us', 'capripio-easy-chat-support' ),
				'namePlaceholder'  => __( 'Your name', 'capripio-easy-chat-support' ),
				'emailPlaceholder' => __( 'Your email', 'capripio-easy-chat-support' ),
				'startButton'      => __( 'Start chat', 'capripio-easy-chat-support' ),
				'onlineLabel'      => __( 'Usually replies in a few minutes', 'capripio-easy-chat-support' ),
				'poweredBy'        => __( 'Powered by', 'capripio-easy-chat-support' ),
				'nameRequired'     => __( 'Please enter your name.', 'capripio-easy-chat-support' ),
				'emailInvalid'     => __( 'Please enter a valid email address.', 'capripio-easy-chat-support' ),
			),
		);

		/**
		 * Lets Pro (or any extension) add data to what's localized for
		 * the widget — e.g. a company logo URL — without touching this
		 * file. Free ignores keys it doesn't recognize.
		 */
		$data = apply_filters( 'cpecs_widget_localize_data', $data );

		wp_localize_script( 'cpecs-widget', 'CPECS_DATA', $data );

		/**
		 * Lets Pro enqueue its own front-end script/style alongside the
		 * widget once it's registered, so Pro can declare a dependency
		 * on the 'cpecs-widget' handle.
		 */
		do_action( 'cpecs_widget_enqueued' );
	}

	public function render() {
		if ( ! $this->settings->get( 'enabled' ) ) {
			return;
		}
		if ( is_admin() ) {
			return;
		}

		$position_class = 'bottom-left' === $this->settings->get( 'widget_position' ) ? 'cpecs-pos-left' : 'cpecs-pos-right';
		?>
		<div id="cpecs-widget-root" class="cpecs-widget-root <?php echo esc_attr( $position_class ); ?>" aria-live="polite"></div>
		<?php
	}
}
