<?php
/**
 * Handles the instance of the Northmill Online Shared code.
 *
 * @package flo-sync
 *
 * @phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag
 */

namespace NMFS;

use NMFS\Admin\Pages\Admin_Tools;
use NMFS\Admin\Pages\Admin_Status as Admin_Status_Page;
use NMFS\Admin\Settings_Fields;
use NMFS\Admin\Views_Admin;
use NMFS\Products\Product_Fields\Wc_Product_Tab;
use NMFS\Products\Product_Fields\Wc_Product_Tab_Content;
use NMFS\Sync\Admin_Status;
use NMFS\Sync\Items\Article_Availabilities\Setup as Article_Availabilities_Setup;
use NMFS\Sync\Items\Articles\Articles;
use NMFS\Sync\Items\Articles\Stock\Checkout;
use NMFS\Sync\Items\Articles\Stock\Shipping;
use NMFS\Sync\Items\Articles\Stock\Stock;
use NMFS\Sync\Items\Articles\Stock\View as Stock_View;
use NMFS\Sync\Items\Campaigns\Campaigns;
use NMFS\Sync\Items\Orders\Orders;
use NMFS\Sync\Syncers;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstract_Plugin_Shared;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Traits\Singleton_Trait;

defined( 'ABSPATH' ) || exit;

/**
 * Class Shared
 */
class Shared extends Abstract_Plugin_Shared {
	use Singleton_Trait;

	/**
	 * {@inheritDoc}
	 */
	protected function setup_hooks( string $service_name_underscored ): void {
		// Filters whether to do stock management on receipt creation.
		add_filter(
			'northmill_online_shared_do_stock_management_on_receipt_creation',
			function () {
				return Settings_Fields::ORDER_STOCK_ADJUSTMENT_ORDER_PAID === Stock::get_stock_adjustment_setting();
			},
			10,
			0
		);

		// Tells Shared to set up Hub Account features.
		add_filter(
			'northmill_online_shared_is_setting_up_hub_accounts_' . $service_name_underscored,
			'__return_true'
		);

		// Register the banner URLs.
		add_filter(
			'northmill_online_shared_banner_urls_' . $service_name_underscored,
			function () {
				return array(
					'high' => 'https://downloadsflo.northmill.com/wp-content/uploads/2017/04/moreflo-system-1024x532.png',
					'low'  => 'https://downloadsflo.northmill.com/wp-content/uploads/2017/04/moreflo-system-1024x532.png',
				);
			},
		);

		// Register the package info URL.
		add_filter(
			'northmill_online_shared_package_info_json_url_' . $service_name_underscored,
			function () {
				return 'https://downloadsflo.northmill.com/plugin/flo-sync.json';
			},
			10,
			0
		);

		// Add action for post-Shared setup.
		add_action(
			'northmill_online_shared_after_setup_' . $service_name_underscored,
			array( $this, 'post_successful_setup' )
		);

		Admin_Status_Page::setup_shared_hooks( $service_name_underscored );
		Admin_Tools::setup_shared_hooks( $service_name_underscored );
		Plugin_Uninstall::setup_shared_hooks( $service_name_underscored );
		Plugin_Upgrade::setup_shared_hooks( $service_name_underscored );
		Settings_Fields::setup_shared_hooks( $service_name_underscored );
		Syncers::setup_shared_hooks( $service_name_underscored );
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_package_file_path(): string {
		return NMF_ABS_FILE_PATH;
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_translations(): array {
		return array(
			'flo-sync',
		);
	}

	/**
	 * Run after successful Shared setup.
	 */
	public function post_successful_setup(): void {
		$this->updater->setup();

		Assets::setup();
		Plugin_Upgrade::setup();
		Views_Admin::setup();

		// Product_Fields.
		new Wc_Product_Tab();
		new Wc_Product_Tab_Content();

		// Sync.
		Admin_Status::setup();

		// Sync/Article_Availabilities.
		Article_Availabilities_Setup::setup();

		// Sync/Articles.
		Articles::setup();

		// Sync/Articles/Stock.
		Stock::setup();
		Stock_View::setup();
		Checkout::setup();
		Shipping::setup();

		// Sync/Campaigns.
		Campaigns::setup();

		// Sync/Orders.
		Orders::setup();
	}
}
