<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
require_once SIFENCY_ADDONS_DIR . 'classes/admin-module/plugins/class-ajax.php';

/**
 * Class Sifency_Addons_Plugin_Installer_Admin_UI
 */
final class Sifency_Addons_Plugin_Installer_Admin_UI {

	private static $_instance = null;
	private $page_hook_suffix = '';

	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	private function __construct() {

	}

	public function set_page_hook_suffix( $hook_suffix ) {
		$this->page_hook_suffix = $hook_suffix;
	}

	/**
	 * Render the Page
	 */
	public function render_admin_page() {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		?>
		<div class="sifency-installer-wrap">
			<div class="sifency-installer-cards-container">
				<?php
				$plugins = $this->get_recommended_plugins();
				foreach ( $plugins as $plugin ) {
					$this->render_plugin_card( $plugin );
				}
				?>
			</div>
		</div>
		<?php
	}

	private function render_plugin_card( $plugin ) {
		$status     = $this->get_plugin_status( $plugin['main_file'] );
		$button_txt = __( 'Install & Activate', 'sifency-addons' );
		$button_cls = 'install-btn';
		$action     = 'install';
		$disabled   = '';

		// Determine Source (Repo vs External)
		$source       = isset( $plugin['source'] ) ? $plugin['source'] : 'repo';
		$download_url = isset( $plugin['download_url'] ) ? $plugin['download_url'] : '';

		switch ( $status ) {
			case 'active':
				$button_txt = __( 'Activated', 'sifency-addons' );
				$button_cls = 'activated-btn sifency-admin-btn';
				$disabled   = 'disabled';
				$action     = '';
				break;
			case 'inactive':
				$button_txt = __( 'Activate', 'sifency-addons' );
				$button_cls = 'activate-btn sifency-admin-btn';
				$action     = 'activate';
				break;
		}
		?>
		<div class="sifency-installer-card">
			<div class="sifency-installer-card-header">
				<div class="plugin-icon-and-name">
					<div class="sifency-installer-card-icon"><img src="<?php echo esc_url( $plugin['icon'] ); ?>" alt="<?php echo esc_attr( $plugin['name'] ); ?>"></div>
					<h3 class="sifency-installer-card-title"><?php echo esc_html( $plugin['name'] ); ?></h3>
				</div>
				<?php if ( ! empty( $plugin['is_required'] ) ) : ?>
					<span class="sifency-plugin-badge is-required"><?php esc_html_e( 'Required', 'sifency-addons' ); ?></span>
				<?php elseif ( ! empty( $plugin['is_supported'] ) ) : ?>
					<span class="sifency-plugin-badge is-supported"><?php esc_html_e( 'Supported', 'sifency-addons' ); ?></span>
				<?php endif; ?>
			</div>
			<p class="sifency-installer-card-desc"><?php echo esc_html( $plugin['description'] ); ?></p>
			<div class="sifency-installer-card-footer">
				<button class="button button-primary plugin-action-btn <?php echo esc_attr( $button_cls ); ?>" 
					data-slug="<?php echo esc_attr( $plugin['slug'] ); ?>" 
					data-mainfile="<?php echo esc_attr( $plugin['main_file'] ); ?>" 
					data-action="<?php echo esc_attr( $action ); ?>" 
					data-source="<?php echo esc_attr( $source ); ?>"
					data-download-url="<?php echo esc_url( $download_url ); ?>"
					<?php echo esc_html( $disabled ); ?>>
					
					<span class="btn-text"><?php echo esc_html( $button_txt ); ?></span>
					<span class="spinner"></span>
				</button>
			</div>
		</div>
		<?php
	}

	private function get_recommended_plugins() {
		$plugins = array();

		$plugins = array_merge(
			$plugins,
			array(
				array(
					'name'        => 'Elementor',
					'slug'        => 'elementor',
					'main_file'   => 'elementor/elementor.php',
					'description' => 'The most advanced frontend drag & drop page builder.',
					'icon'        => 'https://ps.w.org/elementor/assets/icon-128x128.gif',
					'source'      => 'repo',
				),
				array(
					'name'        => 'WooCommerce',
					'slug'        => 'woocommerce',
					'main_file'   => 'woocommerce/woocommerce.php',
					'description' => 'An eCommerce toolkit that helps you sell anything. Beautifully.',
					'icon'        => 'https://ps.w.org/woocommerce/assets/icon-256x256.png',
				),
				array(
					'name'        => 'MetForm',
					'slug'        => 'metform',
					'main_file'   => 'metform/metform.php',
					'description' => 'Drag & drop form builder for Elementor with advanced features.',
					'icon'        => 'https://ps.w.org/metform/assets/icon-256x256.png',
				),
				array(
					'name'        => 'Contact Form 7',
					'slug'        => 'contact-form-7',
					'main_file'   => 'contact-form-7/wp-contact-form-7.php',
					'description' => 'The most popular free contact form plugin – simple yet powerful.',
					'icon'        => 'https://ps.w.org/contact-form-7/assets/icon-128x128.png',
					'source'      => 'repo',
				),
				array(
					'name'        => 'MC4WP: Mailchimp for WordPress',
					'slug'        => 'mailchimp-for-wp',
					'main_file'   => 'mailchimp-for-wp/mailchimp-for-wp.php',
					'description' => 'The #1 Mailchimp plugin – add newsletter sign-up forms and integrate with existing forms.',
					'icon'        => 'https://ps.w.org/mailchimp-for-wp/assets/icon-128x128.png',
					'source'      => 'repo',
				),
				array(
					'name'        => 'Easy Digital Downloads',
					'slug'        => 'easy-digital-downloads',
					'main_file'   => 'easy-digital-downloads/easy-digital-downloads.php',
					'description' => 'A complete system for selling digital products on WordPress.',
					'icon'        => 'https://ps.w.org/easy-digital-downloads/assets/icon-256x256.png',
				),
			)
		);

		return apply_filters( 'sifency_addons_recommended_plugins', $plugins );
	}

	private function get_plugin_status( $plugin_main_file ) {
		$all_plugins = get_plugins();
		if ( isset( $all_plugins[ $plugin_main_file ] ) ) {
			return is_plugin_active( $plugin_main_file ) ? 'active' : 'inactive';
		}
		return 'not_installed';
	}
}

Sifency_Addons_Plugin_Installer_Admin_UI::instance();
