<?php

namespace Repetito;

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Repetito\Admin\RepetitoApi;
use Repetito\Admin\AdminPage;
use Repetito\Admin\OrdersListColumn;
use Repetito\Web\Shortcodes;
use Repetito\Web\Frontend;
use Repetito\Web\Languages;
use const Repetito\FILE;
class Plugin {

	public static function activate() {
		if (!repetito_check_woocommerce()) {
			deactivate_plugins(plugin_basename(FILE));
			wp_die(
				esc_html__('Repetito requires WooCommerce to be installed and active. Please install and activate WooCommerce first.', 'repetito'),
				esc_html__('Plugin Activation Error', 'repetito'),
				array('back_link' => true)
			);
		}
	}

	public static function deactivate() {
	}

	public static function uninstall() {
	}

	public static function init() {
		if ( is_admin() ) {
			self::admin();
		}
		Shortcodes::instance( array( 'repetito', 'repetito-form' ) );
		add_action( 'rest_api_init', array( self::class, 'api' ) );
		FrontEnd::instance();
		Languages::instance();
	}

	/**
	 * @return void
	 */
	public static function admin(): void {
		$repetitoApi = RepetitoApi::instance();
		$adminPage = AdminPage::instance();
		$ordersListColumn = OrdersListColumn::instance();

		// Register Menu.
		$adminPage->register();

		add_action( 'woocommerce_order_status_completed', array( $repetitoApi, 'registerOrder' ), 10, 1 );
		add_action( 'woocommerce_order_status_changed', array( $repetitoApi, 'integrationStatus' ), 10, 3);

		// Fire integration status when settings are updated
		add_action( 'update_option_repetito-settings', array( $repetitoApi, 'integrationStatus' ), 10, 2 );

	}

	public static function api() {
		Languages::instance();
	}
}
