<?php
/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Mojito_Shipping
 * @author     Mojito Team <support@mojitowp.com>
 */

namespace Mojito_Shipping;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Plugin activation class
 */
class Mojito_Shipping_Activator {

	/**
	 * Plugin activation.
	 *
	 * @since    1.0.0
	 */
	public static function activate() {

		mojito_shipping_debug( 'Plugin activated' );


		if ( ! class_exists( 'Mojito_Shipping\Mojito_Shipping_Address' ) ) {
			require_once MOJITO_SHIPPING_DIR . 'includes/class-mojito-shipping-address.php';
	
		}

		// Preload
		$address = new Mojito_Shipping_Address();
		$address->pre_load_pymexpress_locations();

		// Set San Jose - CR as a default country/region on activation.
		update_option( 'woocommerce_default_country', 'CR:SJ' );


		/**
		 * Cron jobs
		 */
		if ( mojito_shipping_fs()->is__premium_only() && mojito_shipping_fs()->can_use_premium_code() ) {

			$legacy_hooks = array(
				'mojito-shipping-cron-ccr',
				'mojito-shipping-cron-pymexpress',
				'mojito-shipping-cron-mojito-proxy-ip-check',
				'mojito-shipping-cron-complete-orders',
				'mojito-shipping-cron-update-exchange-rate',
			);

			foreach ( $legacy_hooks as $legacy_hook ) {
				wp_clear_scheduled_hook( $legacy_hook );
			}

			if ( ! wp_next_scheduled( 'mojito-shipping-pymexpress-cron-control-check-ip' ) ) {
				$recurrence = \get_option( 'mojito-shipping-pymexpress-cron-control-check-ip', 'no' );
				if ( ! empty( $recurrence ) && 'no' !== $recurrence ) {
					wp_schedule_event( time(), $recurrence, 'mojito-shipping-pymexpress-cron-control-check-ip' );
				}
			}
			if ( ! wp_next_scheduled( 'mojito-shipping-pymexpress-cron-control-check-api' ) ) {
				$recurrence = \get_option( 'mojito-shipping-pymexpress-cron-control-check-api', 'no' );
				if ( ! empty( $recurrence ) && 'no' !== $recurrence ) {
					wp_schedule_event( time(), $recurrence, 'mojito-shipping-pymexpress-cron-control-check-api' );
				}
			}
			if ( ! wp_next_scheduled( 'mojito-shipping-pymexpress-cron-control-complete-orders' ) ) {
				$recurrence = \get_option( 'mojito-shipping-pymexpress-cron-control-complete-orders', 'no' );
				if ( ! empty( $recurrence ) && 'no' !== $recurrence ) {
					wp_schedule_event( time(), $recurrence, 'mojito-shipping-pymexpress-cron-control-complete-orders' );
				}
			}
			if ( ! wp_next_scheduled( 'mojito-shipping-pymexpress-cron-control-update-exchange-rate' ) ) {
				$recurrence = \get_option( 'mojito-shipping-pymexpress-cron-control-update-exchange-rate', 'no' );
				if ( ! empty( $recurrence ) && 'no' !== $recurrence ) {
					wp_schedule_event( time(), $recurrence, 'mojito-shipping-pymexpress-cron-control-update-exchange-rate' );
				}
			}
		}
	}

}
