<?php

namespace Barn2\Plugin\WC_Product_Tabs_Pro;

use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Registerable;
use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Translatable;
use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Service_Provider;
use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Plugin\Premium_Plugin;
use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Plugin\Licensed_Plugin;
use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Admin\Notices;
use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Service_Container;
use Barn2\Plugin\WC_Product_Tabs_Pro\Dependencies\Lib\Util as Lib_Util;

defined( 'ABSPATH' ) || exit;

/**
 * The main plugin class. Responsible for setting up to core plugin services.
 *
 * @package   Barn2/woocommerce-product-tabs-pro
 * @author    Barn2 Plugins <info@barn2.com>
 * @license   GPL-3.0
 * @copyright Barn2 Media Ltd
 */
class Plugin extends Premium_Plugin implements Licensed_Plugin, Registerable, Translatable, Service_Provider {

	use Service_Container;

	const NAME        = 'WooCommerce Product Tabs Pro';
	const ITEM_ID     = 552232;

	private $services = [];

	/**
	 * The notices object
	 *
	 * @var Notices
	 */
	private $notices;

	private $freemius;

	/**
	 * Constructs and initalizes the main plugin class.
	 *
	 * @param string $file    The root plugin __FILE__
	 * @param string $version The current plugin version
	 */
	public function __construct( $file = null, $version = '1.0' ) {
		parent::__construct(
			[
				'name'               => self::NAME,
				'item_id'            => self::ITEM_ID,
				'version'            => $version,
				'file'               => $file,
				'is_woocommerce'     => true,
				'settings_path'      => 'admin.php?page=wpt-settings',
				'documentation_path' => 'kb-categories/product-tabs-documentation/',
			]
		);

		$this->freemius = new Freemius( $this );

		if ( ! is_null( $this->freemius->get_helper() ) ) {
			do_action( 'barn2_freemius_helper_loaded' );
		}
	}

	/**
	 * Registers the plugin with WordPress.
	 */
	public function register(): void {
		parent::register();

		$this->notices = new Notices();

		if ( is_admin() ) {
			$this->notices->boot();
		}

		// We create Plugin_Setup here so the plugin activation hook will run.
		$plugin_setup = new Plugin_Setup( $this->get_file(), $this );
		$plugin_setup->register();

		add_action( 'init', [ $this, 'load_textdomain' ], 5 );
		add_action( 'plugins_loaded', [ $this, 'maybe_load_plugin' ] );
	}

	/**
	 * Maybe bootup plugin.
	 */
	public function maybe_load_plugin(): void {
		$this->register_services();
	}

	/**
	 * Setup the plugin services
	 */
	public function get_services(): array {
		if ( Lib_Util::is_admin() ) {
			$services = [
				'admin_controller' => new Admin\Admin_Controller( $this ),
			];

			if ( $this->is_license_active() ) {
				$services['meta_boxes']          = new Admin\Meta_Boxes( $this );
				$services['product_editor_tabs'] = new Admin\Product_Editor_Tabs( $this );

			}
		}

		if ( Lib_Util::is_front_end() ) {
			if ( $this->is_license_active() ) {
				$services['frontend'] = new Frontend( $this );
			}
		}

		$services['post_type'] = new Tab_Post_Type( $this );

		return $services;
	}

	public function get_service( $service ) {
		return $this->services[ $service ];
	}

	/**
	 * Load the language file
	 */
	public function load_textdomain(): void {
		load_plugin_textdomain( 'woocommerce-product-tabs', false, dirname( $this->get_basename() ) . '/languages' );
	}

	public function is_license_active() {
		return $this->has_valid_license() || $this->freemius->has_valid_freemius_license();
	}

	public function is_freemius_active() {
		return $this->freemius->has_valid_freemius_license();
	}
}
