<?php

namespace IGD;

defined( 'ABSPATH' ) || exit();

final class Main {

    protected static $instance = null;

    public function __construct() {
        $this->register_hooks();
        $this->init_auto_loader();
        $this->includes();

        do_action( 'igd_loaded' );
    }

    public function includes() {
        // Core includes
        include_once IGD_INCLUDES . "/functions.php";
        include_once IGD_INCLUDES . "/class-enqueue.php";
        include_once IGD_INCLUDES . "/class-hooks.php";
        include_once IGD_INCLUDES . "/class-shortcode.php";
        include_once IGD_INCLUDES . "/class-shortcode-locations.php";
        include_once IGD_INCLUDES . "/class-ajax.php";

        // Integrations
        include_once IGD_INCLUDES . "/class-integrations.php";

        // Admin includes
        if ( is_admin() ) {
            include_once IGD_INCLUDES . "/class-admin.php";
        }

    }

    public function init_auto_loader() {
        spl_autoload_register( function ( $class_name ) {
            if ( strpos( $class_name, 'IGD' ) !== false ) {
                $classes_dir = IGD_PATH . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR;
                $file_name   = strtolower( str_replace( [ 'IGD\\', '_' ], [ '', '-' ], $class_name ) );
                $file_name   = "class-$file_name.php";

                $file = $classes_dir . $file_name;

                if ( file_exists( $file ) ) {
                    include_once $file;
                }
            }
        } );
    }

    private function register_hooks() {

        register_activation_hook( IGD_FILE, [ $this, 'activate' ] );
        register_deactivation_hook( IGD_FILE, [ $this, 'deactivate' ] );
        register_uninstall_hook( IGD_FILE, [ 'IGD\Main', 'uninstall' ] );

        // Check environment on plugins_loaded hook when WordPress is fully initialized
        add_action( 'plugins_loaded', [ $this, 'check_environment' ], 1 );

        add_action( 'admin_notices', [ $this, 'print_notices' ], 15 );
        $path = plugin_basename( IGD_FILE );
        add_filter( "plugin_row_meta", [ $this, 'plugin_row_meta' ], 10, 2 );
        add_filter( "plugin_action_links_$path", [ $this, 'plugin_action_links' ] );
        add_action( "after_plugin_row_$path", [ $this, 'render_pro_upgrade_notice' ], 10, 3 );
    }

    public function check_environment() {
        $errors = [];

        // Check PHP version
        $php_version     = PHP_VERSION;
        $min_php_version = '7.4';
        if ( version_compare( $php_version, $min_php_version, '<' ) ) {
            $errors[] = sprintf(
                    wp_kses_post( 'Integrate Google Drive requires PHP version <strong>%1$s</strong> or greater. You are currently running PHP <strong>%2$s</strong>. Please contact your hosting provider to update PHP.' ),
                    esc_html( $min_php_version ),
                    esc_html( $php_version )
            );
        }

        // Check WordPress version
        $wp_version     = get_bloginfo( 'version' );
        $min_wp_version = '5.0';
        if ( version_compare( $wp_version, $min_wp_version, '<' ) ) {
            $errors[] = sprintf(
                    wp_kses_post( 'Integrate Google Drive requires WordPress version <strong>%1$s</strong> or greater. You are currently running WordPress <strong>%2$s</strong>. Please update WordPress to the latest version.' ),
                    esc_html( $min_wp_version ),
                    esc_html( $wp_version )
            );
        }

        // Check if required extensions are available
        if ( ! function_exists( 'curl_init' ) ) {
            $errors[] = wp_kses_post( 'Integrate Google Drive requires the <strong>cURL</strong> PHP extension. Please contact your hosting provider to enable it.' );
        }

        if ( ! extension_loaded( 'json' ) ) {
            $errors[] = wp_kses_post( 'Integrate Google Drive requires the <strong>JSON</strong> PHP extension. Please contact your hosting provider to enable it.' );
        }

        // If there are errors, deactivate and display them
        if ( ! empty( $errors ) ) {
            deactivate_plugins( plugin_basename( IGD_FILE ) );

            $error_message = '<div style="margin-top: 10px; padding: 12px; background: #fef5e7; border-left: 4px solid #f39c12; border-radius: 3px;">';
            $error_message .= '<p style="margin: 0 0 10px 0; font-weight: 600; color: #d68910;">Integrate Google Drive Plugin Requirements Not Met</p>';
            foreach ( $errors as $error ) {
                $error_message .= '<p style="margin: 5px 0; color: #7d6608;">' . wp_kses_post( $error ) . '</p>';
            }
            $error_message .= '</div>';

            wp_die( wp_kses_post( $error_message ), esc_html__( 'Plugin Requirements', 'integrate-google-drive' ) );
        }

    }

    public function activate_deactivate( $method ) {

        if ( ! class_exists( 'IGD\Install' ) ) {
            include_once IGD_INCLUDES . "/class-install.php";
        }

        Install::$method();
    }

    public function activate() {
        $this->activate_deactivate( 'activate' );
    }

    public function deactivate() {
        $this->activate_deactivate( 'deactivate' );
    }

    public static function uninstall() {

        if ( ! class_exists( 'IGD\Install' ) ) {
            include_once IGD_INCLUDES . "/class-install.php";
        }

        // Remove crons
        Install::deactivate();

        // Delete data
        if ( igd_get_settings( 'deleteData' ) ) {
            delete_option( 'igd_tokens' );
            delete_option( 'igd_accounts' );
            delete_option( 'igd_settings' );
            delete_option( 'igd_cached_folders' );

            igd_delete_cache();
        }

    }

    public function plugin_row_meta( $plugin_meta, $plugin_file ) {

        if ( $plugin_file !== plugin_basename( IGD_FILE ) ) {
            return $plugin_meta;
        }

        $plugin_meta[] = sprintf( '<a target="_blank" href="https://softlabbd.com/docs-category/integrate-google-drive-docs/" style="color:#2FB44B; ">%s</a>', esc_html__( 'Docs', 'integrate-google-drive' ) );
        $plugin_meta[] = sprintf( '<a target="_blank" href="https://softlabbd.com/support/" style="color:#2FB44B; ">%s</a>', esc_html__( 'Support', 'integrate-google-drive' ) );

        return $plugin_meta;
    }

    public function plugin_action_links( $links ) {
        $links[] = sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=integrate-google-drive-settings' ), esc_html__( 'Settings', 'integrate-google-drive' ) );

        if ( ! igd()->is_pro_active() ) {
            $links[] = sprintf( '<a href="%s" target="_blank" style="color:#2FB44B;"><i class="igd-icon icon-pro" style="vertical-align: sub;"></i> %s</a>', 'https://softlabbd.com/integrate-google-drive-pricing', esc_html__( 'Get Pro', 'integrate-google-drive' ) );
        }

        return $links;
    }

    public function render_pro_upgrade_notice( $plugin_file, $plugin_data, $status ) {

        if ( igd()->is_pro_active() ) {
            return;
        }

        ?>
        <tr class="igd-pro-upgrade-row">
            <td colspan="4" class="igd-pro-upgrade-cell">
                <div id="igd-upgrade-notice" class="igd-upgrade-notice">
                    <div class="igd-upgrade-content">
                        <p class="igd-upgrade-title">
                            <?php
                            echo wp_kses(
                                    __(
                                            '🚀 Unlock powerful features with <a href="https://softlabbd.com/integrate-google-drive-pricing" class="igd-pro-highlight">Integrate Google Drive (PRO)</a>',
                                            'integrate-google-drive'
                                    ),
                                    array(
                                            'span' => array(
                                                    'class' => true,
                                            ),
                                            'a'    => array(
                                                    'href' => true,
                                            ),
                                    )
                            );
                            ?>
                        </p>

                        <p class="igd-upgrade-description">
                            <?php esc_html_e(
                                    'Enable File Proofing, File Uploaders, Media Player, advanced form integrations, Media Library sync, and many more productivity-boosting modules.',
                                    'integrate-google-drive'
                            ); ?>
                        </p>
                    </div>

                    <div class="igd-upgrade-actions">
                        <a
                                href="#"
                                id="igd-hide-pro-notice"
                                class="igd-upgrade-hide"
                        >
                            <?php esc_html_e( 'Dismiss', 'integrate-google-drive' ); ?>
                        </a>

                        <a
                                href="https://softlabbd.com/integrate-google-drive-pricing"
                                target="_blank"
                                rel="noopener noreferrer"
                                class="button button-primary igd-upgrade-button"
                        >
                            <?php esc_html_e( '🚀 Upgrade to PRO', 'integrate-google-drive' ); ?>
                        </a>
                    </div>
                </div>
            </td>
        </tr>

        <script type="text/javascript">
            (function ($) {


                if (localStorage.getItem('hide-igd-pro-notice')) {
                    $('.igd-pro-upgrade-row').hide();
                }

                $(document).on('click', '#igd-hide-pro-notice', function (e) {
                    e.preventDefault();
                    $(this).closest('.igd-pro-upgrade-row').fadeOut(150);

                    localStorage.setItem('hide-igd-pro-notice', '1');
                });

            })(jQuery);
        </script>

        <?php
    }


    public function is_pro_active() {

        if ( ! function_exists( 'igd_fs' ) ) {
            return false;
        }

        return igd_fs()->can_use_premium_code__premium_only();
    }

    public function add_notice( $class, $message ) {
        $notices = get_option( sanitize_key( 'igd_notices' ), [] );

        if ( is_string( $message ) && is_string( $class ) && ! wp_list_filter( $notices, [ 'message' => $message ] ) ) {
            $notices[] = [
                    'message' => $message,
                    'class'   => $class,
            ];

            update_option( sanitize_key( 'igd_notices' ), $notices );
        }
    }

    public function print_notices() {
        $notices = get_option( sanitize_key( 'igd_notices' ), [] );

        foreach ( $notices as $notice ) {
            $class   = ! empty( $notice['class'] ) ? $notice['class'] : 'info';
            $message = ! empty( $notice['message'] ) ? $notice['message'] : '';

            printf( '<div class="notice notice-large is-dismissible igd-admin-notice notice-%1$s">%2$s</div>',
                    esc_attr( $class ),
                    wp_kses( $message, [
                            'a'      => [
                                    'href'   => [],
                                    'title'  => [],
                                    'target' => [],
                                    'rel'    => [],
                            ],
                            'strong' => [],
                            'em'     => [],
                            'br'     => [],
                            'p'      => [
                                    'class' => [],
                            ],
                            'span'   => [
                                    'class' => [],
                            ],
                            'div'    => [
                                    'class' => [],
                            ],
                            'style'  => [],
                            'script' => [],
                    ] ),
            );
        }

        update_option( sanitize_key( 'igd_notices' ), [] );
    }

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

        return self::$instance;
    }
}

if ( ! function_exists( 'igd' ) ) {
    function igd() {
        return Main::instance();
    }
}

igd();
