<?php
/**
 * Easy Digital Downloads integration content view.
 *
 * @package Awesomemotive\Rewardswp\Views\Admin\Parts
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Admin\Parts;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Interfaces\IntegrationInterface;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Settings\Options\Integrations\IntegrationSourceOption;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;
use function Awesomemotive\Rewardswp\Helpers\get_view_component;
use function Awesomemotive\Rewardswp\Helpers\kses;

class EasyDigitalDownloadsIntegrationContentView extends AbstractView implements ViewInterface {
	/**
	 * Integration instance.
	 *
	 * @since 1.0.0
	 *
	 * @var IntegrationInterface
	 */
	private IntegrationInterface $integration;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param IntegrationInterface $integration
	 */
	public function __construct( IntegrationInterface $integration ) {
		$this->integration = $integration;
	}

	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {

		?>

		<div class="py-5">
			<div class="gap-8 md:flex md:items-center">
				<div class="md:w-1/3 flex-none">
					<label for="rewardswp_settings[integrations][easy_digital_downloads]" class="text-sm font-semibold">
						<?php esc_html_e( 'Integration Status', 'rewardswp' ); ?>
					</label>
				</div>
				<div class="md:w-2/3">
					<?php

					$is_active = 'easy_digital_downloads' === get_option_value( IntegrationSourceOption::class );

					if ( 'unknown' === $this->integration->get_source() ) {
						$locked_message = esc_html__( 'Please install and activate Easy Digital Downloads to enable this integration.', 'rewardswp' );
					} elseif ( $is_active ) {
						$locked_message = esc_html__( 'The Easy Digital Downloads integration has been automatically enabled.', 'rewardswp' );
					} else {
						$locked_message = esc_html__( 'This integration cannot be used because WooCommerce is active.', 'rewardswp' );
					}

					echo wp_kses(
						get_view_component(
							'toggle',
							[
								'section'        => 'integrations',
								'name'           => 'easy_digital_downloads',
								'label'          => esc_html__( 'Integration Enabled', 'rewardswp' ),
								'is_checked'     => $is_active,
								'locked'         => true,
								'locked_message' => $locked_message,
							]
						),
						kses()
					);

					?>
				</div>
			</div>
		</div>

		<?php
	}
}
