<?php namespace MeowCrew\CancellationOffers;

use Automattic\WooCommerce\Utilities\FeaturesUtil;
use MeowCrew\CancellationOffers\Core\AdminNotifier;
use MeowCrew\CancellationOffers\Core\FileManager;
use MeowCrew\CancellationOffers\Core\ServiceContainerTrait;
use MeowCrew\CancellationOffers\Offer\CPT\CancellationOffersCPT;
use MeowCrew\CancellationOffers\Frontend\OfferPopup\OfferPopup;
use MeowCrew\CancellationOffers\Settings\Settings;
use MeowCrew\CancellationOffers\SurveyAnswers\SurveyAnswersPage\SurveyAnswersPage;
use MeowCrew\CancellationOffers\SurveyAnswers\SurveyAnswersTable;

/**
 * Class CancellationOffersPlugin
 *
 * @package MeowCrew\CancellationOffers
 */
class CancellationOffersPlugin {
	
	use ServiceContainerTrait;
	
	public static $mainFile = null;
	
	public const VERSION = '2.0.0';
	
	public function __construct( $mainFile ) {
		add_action( 'admin_enqueue_scripts', function () {
			wp_enqueue_style( 'cancellation-offers-general-admin-style',
				$this->getContainer()->getFileManager()->locateCSSAsset( 'admin/general-style' ),
				array( 'wp-color-picker' ), self::VERSION );
			wp_enqueue_script( 'cancellation-offers-general-admin-script',
				$this->getContainer()->getFileManager()->locateJSAsset( 'admin/general-script' ),
				array( 'jquery', 'wp-color-picker' ), self::VERSION, true );
			
			wp_enqueue_media();
		} );
		
		self::$mainFile = $mainFile;
		
		$this->initializeCoreServices();
		
		if ( $this->checkRequirements() ) {
			$this->initializePlugin();
		}
		
		add_action( 'plugins_loaded', [ $this, 'loadTextDomain' ] );
		
		add_filter( 'plugin_action_links_' . plugin_basename( $this->getContainer()->getFileManager()->getMainFile() ),
			array( $this, 'addPluginActions' ), 10, 4 );
		
		add_filter( 'plugin_row_meta', array( $this, 'addPluginsMeta' ), 10, 2 );
		
		$this->declareCompatibilities();
	}
	
	public function declareCompatibilities() {
		add_action( 'before_woocommerce_init', function () {
			if ( class_exists( FeaturesUtil::class ) ) {
				
				$mainFile = $this->getContainer()->getFileManager()->getMainFile();
				
				FeaturesUtil::declare_compatibility( 'custom_order_tables', $mainFile );
				FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', $mainFile );
				FeaturesUtil::declare_compatibility( 'product_block_editor', $mainFile );
			}
		} );
	}
	
	protected function initializeCoreServices() {
		$this->getContainer()->add( Freemius::class, Freemius::class );
		$this->getContainer()->add( 'fileManager', FileManager::class, array( self::$mainFile ) );
		$this->getContainer()->add( 'adminNotifier', AdminNotifier::class );
		$this->getContainer()->add( 'settings', Settings::class );
	}
	
	protected function initializePlugin() {
		$this->getContainer()->add( CancellationOffersCPT::class, CancellationOffersCPT::class );
		$this->getContainer()->add( OfferPopup::class, OfferPopup::class );
		$this->getContainer()->add( SurveyAnswersPage::class, SurveyAnswersPage::class );
	}
	
	/**
	 * Load plugin translations
	 */
	public function loadTextDomain() {
		$name = $this->getContainer()->getFileManager()->getPluginName();
		load_plugin_textdomain( 'cancellation-surveys-offers-woocommerce-subscriptions', false, $name . '/languages/' );
	}
	
	public function checkRequirements(): bool {
		
		if ( ! function_exists( 'is_plugin_active' ) ) {
			include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
		}
		
		// Check if WooCommerce is active
		if ( ! ( is_plugin_active( 'woocommerce/woocommerce.php' ) || is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) ) {
			/* translators: %s: required plugin */
			$message = sprintf( __( '<b>Cancellation Surveys & Offers for WooCommerce Subscriptions</b> plugin requires %s to be installed and activated.',
				'cancellation-surveys-offers-woocommerce-subscriptions' ),
				'<a target="_blank" href="https://wordpress.org/plugins/woocommerce/">WooCommerce</a>' );
			
			$this->getContainer()->getAdminNotifier()->push( $message, AdminNotifier::ERROR );
			
			return false;
		}
		
		// Check if WooCommerce Subscriptions is active
		if ( ! ( is_plugin_active( 'woocommerce-subscriptions/woocommerce-subscriptions.php' ) || is_plugin_active_for_network( 'woocommerce-subscriptions/woocommerce-subscriptions.php' ) ) ) {
			/* translators: %s: required plugin */
			$message = sprintf( __( '<b>Cancellation Surveys & Offers for WooCommerce Subscriptions</b> plugin requires %s to be installed and activated.',
				'cancellation-surveys-offers-woocommerce-subscriptions' ),
				'<a target="_blank" href="https://woocommerce.com/products/woocommerce-subscriptions/">WooCommerce Subscriptions</a>' );
			
			$this->getContainer()->getAdminNotifier()->push( $message, AdminNotifier::ERROR );
			
			return false;
		}
		
		return true;
	}
	
	/**
	 * Add setting to plugin actions at plugins list
	 *
	 * @param  array  $actions
	 *
	 * @return array
	 */
	public function addPluginActions( array $actions ): array {
		$actions[] = '<a href="' . $this->getContainer()->getSettings()->getLink() . '">' . __( 'Settings',
				'cancellation-surveys-offers-woocommerce-subscriptions' ) . '</a>';
		
		return $actions;
	}
	
	
	public function addPluginsMeta( $links, $file ) {
		
		if ( strpos( $file, 'cancellation-surveys-offers' ) === false ) {
			return $links;
		}
		
		$links['docs'] = '<a target="_blank" href="' . self::getDocumentationURL() . '">' . __( 'Documentation',
				'cancellation-surveys-offers-for-woo-subscriptions' ) . '</a>';
		
		$links['contact-us'] = '<a href="' . self::getContactUsURL() . '"><b style="color: green">' . __( 'Contact Us',
				'cancellation-surveys-offers-for-woo-subscriptions' ) . '</b></a>';
		
		if ( ! csows_fs()->is_anonymous() && csows_fs()->is_installed_on_site() ) {
			$links['account'] = '<a href="' . self::getAccountPageURL() . '"><b>' . __( 'Account',
					'cancellation-surveys-offers-for-woo-subscriptions' ) . '</b></a>';
		}
		
		return $links;
	}
	
	/**
	 * Fired when the plugin is activated
	 */
	public function activate() {
		SurveyAnswersTable::create();
	}
	
	/**
	 * Fired during plugin uninstalling
	 */
	public static function uninstall() {
		SurveyAnswersTable::delete();
	}
	
	public static function getDocumentationURL(): string {
		return 'https://meow-crew.com/documentation/cancellation-surveys-offers-for-woo-subscriptions-documentation ';
	}
	
	public static function getContactUsURL(): string {
		return admin_url( 'admin.php?page=csows-contact-us' );
	}
	
	public static function getAccountPageURL(): string {
		return admin_url( 'admin.php?page=csows-account' );
	}
}
