<?php

namespace MOWCPS\Common\Handlers\SyncHandler;

use MOWCPS\Common\Models\Sync_Details_DAO;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Constants\Plugin_Filters;

/**
 * Concrete Batch_Sync_Handler class for batch sync operations.
 */
class Batch_Sync_Handler extends Abstract_Sync_Handler {

	/**
	 * Cron arguments.
	 *
	 * @var array
	 */
	public $cron_args;

	/**
	 * Constructor.
	 *
	 * @param array $cron_args Arguments used to initialize the sync.
	 */
	public function __construct( $cron_args ) {
		parent::__construct( $cron_args['supplier_id'], $cron_args['connection_id'], $cron_args['schedule_id'], $cron_args['sync_arguments']['batch_number'], $cron_args['sync_arguments']['page_number'] );
		$this->cron_args = $cron_args;
		Sync_Details_DAO::get_instance()->set_priority_queue( $cron_args['priority_queue'] );
	}

	/**
	 * Start sync.
	 *
	 * @return void
	 */
	public function start_batch_sync() {
		// Check supplier sync process.
		$this->check_supplier_sync_running_status();

		if ( Constants::VENDOR_AVAILABLE_PRODUCTS === $this->cron_args['sync_arguments']['cron_initiator'] ) {
			Sync_Details_DAO::get_instance()->set_schedule_id( $this->schedule_id );
			$sync_end_status = $this->start_sync();
			$this->end_sync( $sync_end_status );
		} elseif ( has_filter( Plugin_Filters::FILTER_START_DISCARDING_SYNC ) ) {
			$current_number        = $this->cron_args['sync_arguments']['page_number'] > $this->cron_args['sync_arguments']['batch_number'] ? $this->cron_args['sync_arguments']['page_number'] : $this->cron_args['sync_arguments']['batch_number'];
			$start_discarding_sync = apply_filters( Plugin_Filters::FILTER_START_DISCARDING_SYNC, $this->cron_args['supplier_id'], $this->cron_args['connection_id'], $this->cron_args['schedule_id'], $current_number );
		}
	}
}
