<?php

namespace Booktics\Extensions;

/**
 * Plugin manager
 */
class Plugin_Manager {

    /**
     * Check if a plugin is installed.
     *
     * @param string $slug The slug of the plugin.
     *
     * @return bool True if installed, false otherwise.
     */
    public static function is_installed( string $slug ): bool {
        if ( ! function_exists( 'get_plugins' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        $plugins = get_plugins();

        if ( is_array( $plugins ) ) {
            foreach ( $plugins as $plugin_path => $plugin ) {
                if ( strpos( $plugin_path, $slug . '/' ) === 0 ) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Check if a plugin is active.
     *
     * @param string $slug The slug of the plugin.
     *
     * @return bool True if activated, false otherwise.
     */
    public static function is_activated( string $slug ): bool {
        if ( ! self::is_installed( $slug ) ) {
            return false;
        }

        return is_plugin_active( self::get_plugin_path( $slug ) );
    }

    /**
     * Install a plugin from the WordPress repository by slug.
     *
     * @param string $slug The slug of the plugin.
     *
     * @return bool True if installation succeeds, false otherwise.
     */
    public static function install_plugin( string $slug ): bool {
        if ( self::is_installed( $slug ) ) {
            return true; // Already installed
        }

        include_once ABSPATH . 'wp-admin/includes/file.php';
        include_once ABSPATH . 'wp-admin/includes/misc.php';
        include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

        $skin          = new \Automatic_Upgrader_Skin();
        $upgrader      = new \Plugin_Upgrader( $skin );
        $download_link = null;
        foreach ( self::extensions() as $extension ) {
            if ( $extension['slug'] === $slug && ! empty( $extension['download_link'] ) ) {
                $download_link = $extension['download_link'];
                break;
            }
        }

        if ( ! $download_link ) {
            return false;
        }

        $result = $upgrader->install( $download_link );

        return (bool) $result;
    }

    /**
     * Activate a plugin by slug.
     *
     * @param string $slug The slug of the plugin.
     *
     * @return bool True if activated, false otherwise.
     */
    public static function activate_plugin( string $slug ): bool {
        if ( ! self::is_installed( $slug ) ) {
            return false; // Plugin isn't installed
        }

        $plugin_path = self::get_plugin_path( $slug );
        $result      = activate_plugin( $plugin_path );

        if ( is_wp_error( $result ) ) {
            return $result;
        }

        return true;
    }

    /**
     * Deactivate a plugin by slug.
     *
     * @param string $slug The slug of the plugin.
     *
     * @return bool True if deactivated, false otherwise.
     */
    public static function deactivate_plugin( string $slug ) {
        if ( ! self::is_activated( $slug ) ) {
            return false; // Plugin isn't activated
        }

        $plugin_path = self::get_plugin_path( $slug );

        deactivate_plugins( $plugin_path );

        return true;
    }

    /**
     * Get the plugin path by slug.
     *
     * @param string $slug The slug of the plugin.
     *
     * @return string The path to the plugin file.
     */
    private static function get_plugin_path( string $slug ) {
        if ( ! function_exists( 'get_plugins' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        $plugins = get_plugins();

        if ( is_array( $plugins ) ) {
            foreach ( $plugins as $plugin_path => $plugin ) {
                if ( strpos( $plugin_path, $slug . '/' ) === 0 ) {
                    return $plugin_path;
                }
            }
        }

        return false;
    }

    /**
     * Check if the provided file is the main plugin file.
     *
     * @param string $file Path to the file.
     *
     * @return bool True if it is the main plugin file, false otherwise.
     */
    private static function is_main_plugin_file( string $file ): bool {

        $plugin_data = get_file_data( $file, array( 'Name' => 'Plugin Name' ) );

        return ! empty( $plugin_data['Name'] );
    }

    /**
     * Get the plugin name by slug.
     *
     * @param string $slug Plugin slug (e.g., 'woocommerce).
     *
     * @return string|null Returns the plugin name if found, or null if not found.
     */
    public static function get_plugin_name_by_slug( string $slug ) {
        if ( ! function_exists( 'get_plugins' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        $all_plugins = get_plugins();

        foreach ( $all_plugins as $plugin_file => $plugin_data ) {
            if ( strpos( $plugin_file, $slug ) !== false ) {
                return $plugin_data['Name'];
            }
        }

        return null;
    }

    /**
     * List of all extensions
     *
     * @return  array
     */
    public static function extensions(): array {
        return array(
            'booktics-stripe-addon' => array(
                'name'          => 'booktics-stripe-addon',
                'slug'          => 'booktics-stripe-addon',
                'type'          => 'addon',
                'upgrade'       => true,
                'upgrade_link'  => 'https://themewinter.com/purchase-history',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'Booktics Stripe Addon', 'booktics' ),
                'description'   => __( 'It allows you to use stripe payment.', 'booktics' ),
                'icon'          => Extension_Icon::get( 'booktics-stripe-addon' ),
                'notice'        => '',
                'demo_link'     => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-stripe-addon',
                'settings_link' => '',
                'doc_link'      => 'https://docs.arraytics.com/docs/booktics/payment-type/how-to-configure-the-stripe-in-booktics/',
                'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-stripe-addon-1.0.0.zip',
            ),
            'booktics-google-calendar' => array(
                'name'          => 'booktics-google-calendar',
                'slug'          => 'booktics-google-calendar',
                'type'          => 'addon',
                'upgrade'       => true,
                'upgrade_link'  => 'https://themewinter.com/purchase-history',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'Booktics Google Calendar', 'booktics' ),
                'description'   => __( 'It allows you to use google calendar.', 'booktics' ),
                'icon'          => Extension_Icon::get( 'booktics-google-calendar' ),
                'notice'        => '',
                'demo_link'     => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-google-calendar',
                'settings_link' => '',
                'doc_link'      => 'https://docs.arraytics.com/docs/booktics/integration/how-to-integrate-google-calendar-with-booktics/',
                'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-google-calendar-1.0.0.zip',
            ),
            'booktics-outlook-calendar' => array(
                'name'          => 'booktics-outlook-calendar',
                'slug'          => 'booktics-outlook-calendar',
                'type'          => 'addon',
                'upgrade'       => true,
                'upgrade_link'  => 'https://themewinter.com/purchase-history',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'Booktics Outlook Calendar', 'booktics' ),
                'description'   => __( 'It allows you to use outlook calendar.', 'booktics' ),
                'icon'          => Extension_Icon::get( 'booktics-outlook-calendar' ),
                'notice'        => '',
                'demo_link'     => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-outlook-calendar-addon',
                'settings_link' => '',
                'doc_link'      => 'https://docs.arraytics.com/docs/booktics/integration/how-to-integrate-outlook-calendar-with-booktics/',
                'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-outlook-calendar-addon-1.0.0.zip',
            ),
            'booktics-fluentcrm-addon' => array(
                'name'          => 'booktics-fluentcrm-addon',
                'slug'          => 'booktics-fluentcrm-addon',
                'type'          => 'addon',
                'upgrade'       => true,
                'upgrade_link'  => 'https://themewinter.com/purchase-history',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'Booktics FluentCRM Addon', 'booktics' ),
                'description'   => __( 'It allows you to use FluentCRM.', 'booktics' ),
                'icon'          => Extension_Icon::get( 'booktics-fluentcrm-addon' ),
                'notice'        => '',
                'demo_link'     => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-fluentcrm-addon',
                'settings_link' => '',
                'doc_link'      => 'https://docs.arraytics.com/docs/booktics/',
                'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-fluentcrm-addon-1.0.0.zip',
            ),
            'booktics-paypal-addon' => array(
                'name'          => 'booktics-paypal-addon',
                'slug'          => 'booktics-paypal-addon',
                'type'          => 'addon',
                'upgrade'       => true,
                'upgrade_link'  => 'https://themewinter.com/purchase-history',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'Booktics PayPal Addon', 'booktics' ),
                'description'   => __( 'It allows you to use PayPal.', 'booktics' ),
                'icon'          => Extension_Icon::get( 'booktics-paypal-addon' ),
                'notice'        => '',
                'demo_link'     => 'https://support.themewinter.com/docs/plugins/plugin-docs/integration/booktics-paypal-addon',
                'settings_link' => '',
                'doc_link'      => 'https://docs.arraytics.com/docs/booktics/payment-type/how-to-configure-the-paypal-in-booktics-pro/',
                'download_link' => 'https://products.arraytics.com/booktics-addons/booktics-paypal-addon-1.0.0.zip',
            ),
            'eventin' => array(
                'name'          => 'eventin',
                'slug'          => 'wp-event-solution',
                'type'          => 'plugin',
                'upgrade'       => false,
                'upgrade_link'  => '',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'Eventin', 'booktics' ),
                'description'   => __( 'Best Free WordPress Event Calendar Plugin to manage booking, registration, RSVPs, schedule recurring events and sell event tickets with WooCommerce.', 'booktics' ),
                'icon'          => Extension_Icon::get( 'eventin' ),
                'notice'        => '',
                'demo_link'     => 'https://product.themewinter.com/eventin',
                'settings_link' => '',
                'doc_link'      => 'https://support.themewinter.com/docs/plugins/docs/eventin/',
                'download_link' => 'https://downloads.wordpress.org/plugin/wp-event-solution.latest-stable.zip',
            ),
            'wp-cafe' => array(
                'name'          => 'wp-cafe',
                'slug'          => 'wp-cafe',
                'type'          => 'plugin',
                'upgrade'       => false,
                'upgrade_link'  => '',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'WPCafe', 'booktics' ),
                'description'   => __( 'WPCafe - A restaurant plugin to increase sales with an Online Ordering System, Delivery, Pickup, Food Menu, Reservations, and Table Management.', 'booktics' ),
                'icon'          => Extension_Icon::get( 'wpcafe' ),
                'notice'        => '',
                'demo_link'     => 'https://product.themewinter.com/wpcafe',
                'settings_link' => '',
                'doc_link'      => 'https://support.themewinter.com/docs/plugins/docs/wp-cafe/',
                'download_link' => 'https://downloads.wordpress.org/plugin/wp-cafe.latest-stable.zip',
            ),
            'timetics' => array(
                'name'          => 'timetics',
                'slug'          => 'timetics',
                'type'          => 'plugin',
                'upgrade'       => false,
                'upgrade_link'  => '',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'WP Timetics', 'booktics' ),
                'description'   => __( 'Appointment Booking and Scheduling Calendar Plugin - WP Timetics', 'booktics' ),
                'icon'          => Extension_Icon::get( 'timetics' ),
                'notice'        => '',
                'demo_link'     => 'https://arraytics.com/timetics',
                'settings_link' => '',
                'doc_link'      => 'https://docs.arraytics.com/docs/timetics/getting-started/',
                'download_link' => 'https://downloads.wordpress.org/plugin/timetics.latest-stable.zip',
            ),
            'poptics' => array(
                'name'          => 'poptics',
                'slug'          => 'poptics',
                'type'          => 'plugin',
                'upgrade'       => false,
                'upgrade_link'  => '',
                'status'        => 'off',
                'is_pro'        => false,
                'title'         => __( 'Poptics', 'booktics' ),
                'description'   => __( 'Discover the ultimate popup mix with Poptics Popup Builder to boost your lead generation and sales conversions.', 'booktics' ),
                'icon'          => Extension_Icon::get( 'poptics' ),
                'notice'        => '',
                'demo_link'     => 'https://demo.aethonic.com/popticsadmin/',
                'settings_link' => '',
                'doc_link'      => 'https://docs.aethonic.com/docs/getting-started/intro/',
                'download_link' => 'https://downloads.wordpress.org/plugin/poptics.latest-stable.zip',
            ),
        );
    }
}
