<?php

namespace Brandy\Admin\Pages\Dashboard;

use Brandy\Traits\SingletonTrait;

class PluginService {
	use SingletonTrait;

	public static function get_required_plugins() {

		return self::mapped_plugins(
			array(
				'woocommerce'   => array(
					'slug'              => 'woocommerce',
					'display_name'      => 'Create an ecommerce website on WordPress',
					'name'				=> 'WooCommerce',
					'badge'        => 'Woo',
					'icon'              => 'https://ps.w.org/woocommerce/assets/icon.svg',
					'download_link'     => 'https://downloads.wordpress.org/plugin/woocommerce.zip',
					'url'               => 'https://wordpress.org/plugins/woocommerce',
					'version'           => 0,
					'groups'            => array( 'woocommerce' ),
				),
				'brandy-blocks' => array(
					'slug'              => 'brandy-blocks',
					'display_name'      => 'Add custom ecommerce blocks to WordPress.',
					'name'				=> 'Brandy Blocks',
					'badge'        => 'Brandy Blocks',
					'icon'              => 'https://ps.w.org/brandy-blocks/assets/icon-256x256.png?rev=3095006',
					'download_link'     => 'https://downloads.wordpress.org/plugin/brandy-blocks.zip',
					'url'               => 'https://wordpress.org/plugins/brandy-blocks',
					'version'           => 0,
					'groups'            => array( 'woocommerce' ),
				),
			)
		);
	}

	public static function get_suggested_plugins() {

		return self::mapped_plugins(
			array(
				'brandy-sites'      => array(
					'slug'              => 'brandy-sites',
					'display_name'      => __( 'Create stunning, professional websites', 'brandy' ),
					'name'				=> 'Brandy Sites',
					'badge'        => 'Brandy Sites',
					'icon'              => 'https://ps.w.org/brandy-sites/assets/icon-256x256.png?rev=3125864',
					'download_link'     => 'https://downloads.wordpress.org/plugin/brandy-sites.zip',
					'url'               => 'https://wordpress.org/plugins/brandy-sites/',
					'version'           => 0,
					'groups'            => array( 'all', 'woocommerce' ),
				),
				'filebird'          => array(
					'slug'              => 'filebird',
					'display_name'      => __( 'Organize WP media library with folders', 'brandy' ),
					'name'				=> 'FileBird',
					'badge'        		=> 'FileBird',
					'icon'              => 'https://ps.w.org/filebird/assets/icon-128x128.gif',
					'download_link'     => 'https://downloads.wordpress.org/plugin/filebird.zip',
					'url'               => 'https://wordpress.org/plugins/filebird/',
					'version'           => 0,
					'groups'            => array( 'management' ),
				),
				'filebird-document-library'          => array(
					'slug'              => 'filebird-document-library',
					'display_name'      => __( 'Create and publish document galleries', 'brandy' ),
					'name'				=> 'FileBird Document Library',
					'badge'        		=> 'FileBird',
					'icon'              => 'https://ps.w.org/filebird-document-library/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/filebird-document-library.zip',
					'url'               => 'https://wordpress.org/plugins/filebird-document-library/',
					'version'           => 0,
					'groups'            => array( 'management' ),
				),
				'yaymail'           => array(
					'slug'              => 'yaymail',
					'display_name'      => __( 'Customize WooCommerce email templates', 'brandy' ),
					'name'				=> 'YayMail',
					'badge'        => 'YayMail',
					'icon'              => 'https://ps.w.org/yaymail/assets/icon-256x256.gif',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yaymail.zip',
					'url'               => 'https://wordpress.org/plugins/yaymail/',
					'version'           => 0,
					'groups'            => array( 'woocommerce', 'marketing' ),
				),
				'yaycurrency'       => array(
					'slug'              => 'yaycurrency',
					'display_name'      => __( 'Add currency switcher with flags', 'brandy' ),
					'name'				=> 'YayCurrency',
					'badge'        => 'YayCurrency',
					'icon'              => 'https://ps.w.org/yaycurrency/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yaycurrency.zip',
					'url'               => 'https://wordpress.org/plugins/yaycurrency/',
					'version'           => 0,
					'groups'            => array( 'woocommerce', 'marketing' ),
				),
				'yaypricing'        => array(
					'slug'              => 'yaypricing',
					'display_name'      => __( 'Setup dynamic pricing & cart discounts', 'brandy' ),
					'name'				=> 'YayPricing',
					'badge'        => 'YayPricing',
					'icon'              => 'https://ps.w.org/yaypricing/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yaypricing.zip',
					'url'               => 'https://wordpress.org/plugins/yaypricing/',
					'version'           => 0,
					'groups'            => array( 'woocommerce', 'marketing' ),
				),
				'yayextra'          => array(
					'slug'              => 'yayextra',
					'display_name'      => __( 'Add extra product options with custom fees', 'brandy' ),
					'name'				=> 'YayExtra',
					'badge'        => 'YayExtra',
					'icon'              => 'https://ps.w.org/yayextra/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yayextra.zip',
					'url'               => 'https://wordpress.org/plugins/yayextra/',
					'version'           => 0,
					'groups'            => array( 'woocommerce', 'marketing' ),
				),
				'yayswatches'       => array(
					'slug'              => 'yayswatches',
					'display_name'      => __( 'Display swatches on product page & shop page', 'brandy' ),
					'name'				=> 'YaySwatches',
					'badge'        => 'YaySwatches',
					'icon'              => 'https://ps.w.org/yayswatches/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yayswatches.zip',
					'url'               => 'https://wordpress.org/plugins/yayswatches/',
					'version'           => 0,
					'groups'            => array( 'woocommerce' ),
				),
				'yaysmtp'           => array(
					'slug'              => 'yaysmtp',
					'display_name'      => __( 'Send WordPress emails successfully', 'brandy' ),
					'name'				=> 'YaySMTP',
					'badge'        => 'YaySMTP',
					'icon'              => 'https://ps.w.org/yaysmtp/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yaysmtp.zip',
					'url'               => 'https://wordpress.org/plugins/yaysmtp/',
					'version'           => 0,
					'groups'            => array( 'marketing' ),
				),
				'yayreviews'           => array(
					'slug'              => 'yay-customer-reviews-woocommerce',
					'display_name'      => __( 'Advanced Customer Reviews for WooCommerce', 'brandy' ),
					'name'				=> 'YayReviews',
					'badge'        => 'YayReviews',
					'icon'              => 'https://ps.w.org/yay-customer-reviews-woocommerce/assets/icon-256x256.png?rev=3340109',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yay-customer-reviews-woocommerce.zip',
					'url'               => 'https://wordpress.org/plugins/yay-customer-reviews-woocommerce/',
					'version'           => 0,
					'groups'            => array( 'woocommerce', 'marketing' ),
				),
				'yay-wholesale-b2b' => array(
					'slug'              => 'yay-wholesale-b2b',
					'display_name'      => __( 'Sell to wholesale customers and businesses', 'brandy' ),
					'name'				=> 'Yay Wholesale B2B',
					'badge'        => 'Yay Wholesale B2B',
					'icon'              => 'https://ps.w.org/yay-wholesale-b2b/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yay-wholesale-b2b.zip',
					'url'               => 'https://wordpress.org/plugins/yay-wholesale-b2b/',
					'groups'            => array( 'woocommerce', 'marketing' ),
					'version'           => 0,
				),
				'yayboost-sales-booster-for-woocommerce' => array(
					'slug'              => 'yayboost-sales-booster-for-woocommerce',
					'display_name'      => __( 'Boost conversions with smart sales triggers', 'brandy' ),
					'name'				=> 'YayBoost',
					'badge'        => 'YayBoost',
					'icon'              => 'https://ps.w.org/yayboost-sales-booster-for-woocommerce/assets/icon-256x256.png?rev=3498248',
					'download_link'     => 'https://downloads.wordpress.org/plugin/yayboost-sales-booster-for-woocommerce.zip',
					'url'               => 'https://wordpress.org/plugins/yayboost-sales-booster-for-woocommerce/',
					'groups'            => array( 'featured', 'woocommerce' ),
					'version'           => 0,
				),
				'wp-whatsapp'       => array(
					'slug'              => 'wp-whatsapp',
					'display_name'      => __( 'Connect with customers on the go.', 'brandy' ),
					'name'				=> 'WhatsApp',
					'badge'        => 'WhatsApp',
					'icon'              => 'https://ps.w.org/wp-whatsapp/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/wp-whatsapp.zip',
					'url'               => 'https://wordpress.org/plugins/wp-whatsapp/',
					'version'           => 0,
					'groups'            => array( 'woocommerce', 'marketing' ),
				),
				'filester'          => array(
					'slug'              => 'filester',
					'display_name'      => __( 'Manage WordPress files and configuration', 'brandy' ),
					'name'				=> 'Filester',
					'badge'        => 'Filester',
					'icon'              => 'https://ps.w.org/filester/assets/icon-256x256.gif',
					'download_link'     => 'https://downloads.wordpress.org/plugin/filester.zip',
					'url'               => 'https://wordpress.org/plugins/filester/',
					'version'           => 0,
					'groups'            => array( 'management' ),
				),
				'cf7-multi-step'    => array(
					'slug'              => 'cf7-multi-step',
					'display_name'      => __( 'Add multi-step to contact form 7', 'brandy' ),
					'name'				=> 'CF7 Multi Step',
					'badge'        => 'Forms',
					'icon'              => 'https://ps.w.org/cf7-multi-step/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/cf7-multi-step.zip',
					'url'               => 'https://wordpress.org/plugins/cf7-multi-step/',
					'version'           => 0,
					'groups'            => array( 'marketing' ),
				),
				'cf7-database'      => array(
					'slug'              => 'cf7-database',
					'display_name'      => __( 'Save contact form 7 fields to database', 'brandy' ),
					'name'				=> 'CF7 Database',
					'badge'        => 'Forms',
					'icon'              => 'https://ps.w.org/cf7-database/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/cf7-database.zip',
					'url'               => 'https://wordpress.org/plugins/cf7-database/',
					'version'           => 0,
					'groups'            => array( 'management', 'marketing' ),
				),
				'wp-duplicate-page' => array(
					'slug'              => 'wp-duplicate-page',
					'display_name'      => __( 'Duplicate page, post & custom post types', 'brandy' ),
					'name'				=> 'WP Duplicate Page',
					'badge'        => 'Post types',
					'icon'              => 'https://ps.w.org/wp-duplicate-page/assets/icon-256x256.gif',
					'download_link'     => 'https://downloads.wordpress.org/plugin/wp-duplicate-page.zip',
					'url'               => 'https://wordpress.org/plugins/wp-duplicate-page/',
					'version'           => 0,
					'groups'            => array( 'management' ),
				),
				'notibar'           => array(
					'slug'              => 'notibar',
					'display_name'      => __( 'Add top banner & announcement bar', 'brandy' ),
					'name'				=> 'Notibar',
					'badge'        => 'Notibar',
					'icon'              => 'https://ps.w.org/notibar/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/notibar.zip',
					'url'               => 'https://wordpress.org/plugins/notibar/',
					'version'           => 0,
					'groups'            => array( 'marketing' ),
				),
				'fastdup'           => array(
					'slug'              => 'fastdup',
					'display_name'      => __( 'Backup and migrate your WordPress sites', 'brandy' ),
					'name'				=> 'Fastdup',
					'badge'        => 'Fastdup',
					'icon'              => 'https://ps.w.org/fastdup/assets/icon-256x256.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/fastdup.zip',
					'url'               => 'https://wordpress.org/plugins/fastdup/',
					'version'           => 0,
					'groups'            => array( 'management' ),
				),
				'ninja-gdpr-compliance'          => array(
					'slug'              => 'ninja-gdpr-compliance',
					'display_name'      => __( 'Protect personal user data and privacy of EU citizens', 'brandy' ),
					'name'				=> 'GDPR CCPA',
					'badge'        => 'GDPR CCPA',
					'icon'              => 'https://ps.w.org/ninja-gdpr-compliance/assets/icon-128x128.png',
					'download_link'     => 'https://downloads.wordpress.org/plugin/ninja-gdpr-compliance.zip',
					'url'               => 'https://wordpress.org/plugins/ninja-gdpr-compliance/',
					'version'           => 0,
					'groups'            => array( 'management' ),
				),
			)
		);
	}

	public static function get_active_plugins() {
		return apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
	}

	/**
	 * Map status and file dir to given plugins list
	 */
	private static function mapped_plugins( $plugins ) {

		require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

		$active_plugins = self::get_active_plugins();

		$plugins = array_map(
			function( $p ) use ( $active_plugins ) {
				$slug     = $p['slug'];
				$pro_slug = $p['slug'] . '-pro';

				/**
				 * Check is activated plugin
				 */
				foreach ( $active_plugins as $active_plugin ) {
					if ( false !== strpos( $active_plugin, $slug ) || false !== strpos( $active_plugin, $pro_slug ) ) {
						$p['status'] = 'activated';
						return $p;
					}
				}

				/**
				 * Check is installed plugin
				 */
				$plugin_status     = install_plugin_install_status( $p );
				$pro_plugin_status = install_plugin_install_status(
					wp_parse_args(
						array(
							'slug' => $pro_slug,
						),
						$p
					)
				);
				$p['status']       = 'not_installed';
				if ( 'install' !== $plugin_status['status'] || 'install' !== $pro_plugin_status['status'] ) {
					$p['status'] = 'installed';
				}
				if ( 'newer_installed' === $plugin_status['status'] ) {
					$p['file'] = $plugin_status['file'];
				}
				if ( 'newer_installed' === $pro_plugin_status['status'] ) {
					$p['file'] = $pro_plugin_status['file'];
				}
				return $p;
			},
			$plugins
		);

		return $plugins;
	}

	public static function install_and_activate_plugin( $plugin ) {
		require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
		require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
		require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
		require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';

		$plugin_status     = install_plugin_install_status(
			array(
				'slug'    => $plugin['slug'],
				'version' => $plugin['version'],
			)
		);
		$pro_plugin_status = install_plugin_install_status(
			array(
				'slug'    => $plugin['slug'] . '-pro',
				'version' => $plugin['version'],
			)
		);

		if ( 'install' === $plugin_status['status'] && 'install' === $pro_plugin_status['status'] ) {
			$skin     = new \WP_Ajax_Upgrader_Skin();
			$upgrader = new \Plugin_Upgrader( $skin );

			$result = $upgrader->install( $plugin['download_link'] );
			if ( is_wp_error( $result ) ) {
				throw new \Error( 'Install failed' );
			}

			$args        = array(
				'slug'   => $upgrader->result['destination_name'],
				'fields' => array(
					'short_description' => true,
					'icons'             => true,
					'banners'           => false,
					'added'             => false,
					'reviews'           => false,
					'sections'          => false,
					'requires'          => false,
					'rating'            => false,
					'ratings'           => false,
					'downloaded'        => false,
					'last_updated'      => false,
					'added'             => false,
					'tags'              => false,
					'compatibility'     => false,
					'homepage'          => false,
					'donate_link'       => false,
				),
			);
			$plugin_data = plugins_api( 'plugin_information', $args );
			if ( empty( $plugin_data ) || is_wp_error( $plugin_data ) ) {
				throw new \Error( __( 'Install failed', 'brandy' ) );
			}

			$install_status = install_plugin_install_status( $plugin_data );

			if ( is_wp_error( $install_status ) ) {
				throw new \Error( __( 'Install failed', 'brandy' ) );
			}

			$plugin_file = $install_status['file'];
		} else {
			if ( 'install' !== $plugin_status['status'] ) {
				$plugin_file = $plugin_status['file'];
			}
			if ( 'install' !== $pro_plugin_status['status'] ) {
				$plugin_file = $pro_plugin_status['file'];
			}
		}

		self::activate_plugin( $plugin_file );

	}

	public static function activate_plugin( $plugin_file ) {

		require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
		require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
		require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
		require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';

		$activate_status = activate_plugin( $plugin_file );

		if ( is_wp_error( $activate_status ) ) {
			throw new \Error( __( 'Activate failed', 'brandy' ) );
		}
	}

	public static function get_plugins_information() {
		require_once ABSPATH . 'wp-admin/includes/plugin-install.php';

		$yaycommerce_plugins_api = plugins_api(
			'query_plugins',
			array(
				'search' => 'yaycommerce',
			)
		);

		if ( is_wp_error( $yaycommerce_plugins_api ) ) {
			$yaycommerce_plugins = array();
		} else {
			$yaycommerce_plugins = $yaycommerce_plugins_api->plugins;
		}

		$ninjateam_plugins_api = plugins_api(
			'query_plugins',
			array(
				'search' => 'ninjateam',
			)
		);

		if ( is_wp_error( $ninjateam_plugins_api ) ) {
			$ninjateam_plugins = array();
		} else {
			$ninjateam_plugins = $ninjateam_plugins_api->plugins;
		}

		$suggested_plugins_slugs = array_map(
			function( $item ) {
				return $item['slug'];
			},
			array_values( self::get_suggested_plugins() )
		);

		$result_plugins = array_filter(
			array_merge( $yaycommerce_plugins, $ninjateam_plugins ),
			function( $item ) use ( $suggested_plugins_slugs ) {
				return in_array( $item['slug'], $suggested_plugins_slugs, true );
			}
		);

		return array_values( $result_plugins );
	}

}
