<?php

/**
 * Main plugin class with Freemius integration.
 * 
 * @author Nikunj Hatkar
 * @link https://github.com/nikunj8866
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
    // Prevent direct access
}
class Broken_Image_Fallback {
    /**
     * Plugin version
     */
    public $version = BROKIMFA_VERSION;

    /**
     * The single instance of the class
     */
    protected static $_instance = null;

    /**
     * Admin instance
     */
    public $admin = null;

    /**
     * Frontend instance
     */
    public $frontend = null;

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

    /**
     * Constructor
     */
    public function __construct() {
        $this->init_hooks();
        $this->includes();
        $this->init();
    }

    /**
     * Hook into actions and filters.
     */
    private function init_hooks() : void {
        add_filter( 'plugin_action_links_' . BROKIMFA_PLUGIN_BASENAME, array($this, 'plugin_action_links') );
        // Freemius hooks
        add_action( 'brokimfa_fs_loaded', array($this, 'freemius_loaded') );
    }

    /**
     * Freemius loaded callback.
     */
    public function freemius_loaded() {
        // Add upgrade notice for free users
        if ( brokimfa_fs()->is_free_plan() ) {
            add_action( 'admin_notices', array($this, 'show_upgrade_notice') );
        }
        // Add custom Freemius menu items
        brokimfa_fs()->add_filter( 'plugin_icon', array($this, 'get_plugin_icon') );
        brokimfa_fs()->add_action( 'after_uninstall', array($this, 'cleanup_after_uninstall') );
    }

    /**
     * Show upgrade notice for free users
     */
    public function show_upgrade_notice() {
        $screen = get_current_screen();
        if ( strpos( $screen->id, 'broken-image-fallback' ) === false ) {
            return;
        }
        echo '<div class="notice notice-info is-dismissible brokimfa-upgrade-notice">
			<div style="display: flex; align-items: center; padding: 10px 0;">
				<div style="margin-right: 15px;">
					<svg width="40" height="40" viewBox="0 0 40 40" style="fill: #2271b1;">
						<rect x="5" y="8" width="25" height="18" fill="none" stroke="currentColor" stroke-width="2" rx="2"/>
						<path d="M10 14l5 5 3-3 5 5v5H10V14z" fill="currentColor" opacity="0.3"/>
						<circle cx="14" cy="13" r="1.5" fill="currentColor" opacity="0.5"/>
						<path d="M26 32l4-4-4-4v2h-4v4h4v2z" fill="#00a32a"/>
					</svg>
				</div>
				<div style="flex: 1;">
					<h3 style="margin: 0 0 5px 0; color: #1d2327;">🚀 Upgrade to Pro for Advanced Features!</h3>
					<p style="margin: 0; color: #646970;">
						Get WordPress-specific features, advanced customization, fade effects, and priority support.
						<strong>Use code LAUNCH50 for 50% off!</strong>
					</p>
				</div>
				<div>
					<a href="' . esc_url( brokimfa_fs()->get_upgrade_url() ) . '" class="button button-primary" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border: none; text-shadow: none;">
						Upgrade Now
					</a>
				</div>
			</div>
		</div>';
    }

    /**
     * Get plugin icon for Freemius
     */
    public function get_plugin_icon() {
        return BROKIMFA_PLUGIN_URL . 'assets/icon-256x256.png';
    }

    /**
     * Cleanup after uninstall
     */
    public function cleanup_after_uninstall() {
        delete_option( 'brokimfa_options' );
    }

    /**
     * Include required files
     */
    public function includes() {
        include_once BROKIMFA_PLUGIN_PATH . 'includes/class-broken-image-fallback-admin.php';
        include_once BROKIMFA_PLUGIN_PATH . 'includes/class-broken-image-fallback-frontend.php';
    }

    /**
     * Initialize plugin
     */
    public function init() {
        // Initialize admin
        if ( is_admin() ) {
            $this->admin = new Broken_Image_Fallback_Admin();
        }
        // Initialize frontend
        if ( !is_admin() ) {
            $this->frontend = new Broken_Image_Fallback_Frontend();
        }
    }

    /**
     * Add plugin action links
     */
    public function plugin_action_links( $links ) {
        $settings_link = '<a href="' . admin_url( 'admin.php?page=broken-image-fallback' ) . '">' . __( 'Settings', 'broken-image-fallback' ) . '</a>';
        array_unshift( $links, $settings_link );
        // Add upgrade link for free users
        if ( brokimfa_fs()->is_free_plan() ) {
            $upgrade_link = '<a href="' . brokimfa_fs()->get_upgrade_url() . '" style="color: #e26f56; font-weight: bold;">' . __( 'Upgrade to Pro', 'broken-image-fallback' ) . '</a>';
            array_unshift( $links, $upgrade_link );
        }
        return $links;
    }

    /**
     * Get plugin options
     */
    public static function get_options() {
        return get_option( 'brokimfa_options', array() );
    }

    /**
     * Check if feature is available (FREE vs PRO)
     */
    public static function is_feature_available( $feature ) {
        // FREE features that are always available
        $free_features = array('default_image_fallback', 'lazy_loading_compat', 'basic_settings');
        // Check if it's a free feature
        if ( in_array( $feature, $free_features ) ) {
            return true;
        }
        // All other features require PRO
        return brokimfa_fs()->is_premium();
    }

    /**
     * Get upgrade URL with utm parameters
     */
    public static function get_upgrade_url( $source = 'plugin' ) {
        return brokimfa_fs()->get_upgrade_url() . '&utm_source=' . $source . '&utm_medium=plugin&utm_campaign=upgrade';
    }

}
