<?php
/**
 * Ingram_Micro App
 *
 * @category   WordPress_Plugin
 * @package    Common
 * @subpackage Suppliers
 * @author     miniOrange <info@miniorange.com>
 * @license    MIT/Expat
 * @link       https://plugins.miniorange.com/woocommerce-api-product-sync-with-woocommerce-rest-apis
 */

namespace MOWCPS\Common\Suppliers;

use WP_Error;
use MOWCPS\Common\Abstracts\Base_Supplier_App;
use MOWCPS\Common\Interfaces\Supplier_App_Interface;
use MOWCPS\Common\Constants\Constants;
use MOWCPS\Common\Hooks\Ingram_Micro as Ingram_Micro_Hook_class;
use MOWCPS\Common\Suppliers\Ingram_Micro_API;
use MOWCPS\Common\Suppliers\Ingram_Micro_SFTP;

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

/**
 * Class Ingram_Micro
 *
 * Handles Ingram_Micro supplier app operations
 */
class Ingram_Micro extends Base_Supplier_App implements Supplier_App_Interface {

	/**
	 * Constructor
	 */
	public function __construct() {
		parent::__construct( Constants::SUPPLIER_APP_TYPE_INGRAM_MICRO );
		new Ingram_Micro_Hook_class();
	}

	/**
	 * Validate Ingram_Micro-specific configuration
	 *
	 * @param array $supplier_data Supplier data to validate.
	 * @return bool|WP_Error true if valid, WP_Error if invalid.
	 */
	public function validate_supplier_specific_configuration( $supplier_data ) {
		if ( Constants::SYNC_TYPE_API === $supplier_data['sync_type'] ) {
			return ( new Ingram_Micro_API() )->validate_supplier_specific_configuration( $supplier_data );
		} else {
			return ( new Ingram_Micro_SFTP() )->validate_supplier_specific_configuration( $supplier_data );
		}
	}

	/**
	 * Perform Ingram_Micro-specific connection test
	 *
	 * @param array $supplier_data Supplier data.
	 * @return array Test result.
	 */
	public function perform_connection_test( $supplier_data ) {
		if ( Constants::SYNC_TYPE_API === $supplier_data['sync_type'] ) {
			return ( new Ingram_Micro_API() )->perform_connection_test( $supplier_data );
		} else {
			return ( new Ingram_Micro_SFTP() )->perform_connection_test( $supplier_data );
		}
	}

	public static function alter_request_payload( $arguments ) {
		if ( Constants::SUPPLIER_APP_TYPE_INGRAM_MICRO === $arguments['supplier_data']['app_type'] && Constants::SYNC_TYPE_API === $arguments['supplier_data']['details']['sync_type'] ) {
			if ( $arguments['supplier_data']['details']['env_type'] ) {
				$arguments['supplier_data']['details']['env_type'] = '/sandbox';
			} else {
				$arguments['supplier_data']['details']['env_type'] = '';
			}

		}
		return $arguments;
	}

	public static function alter_supplier_data( $arguments ) {
		if ( Constants::SYNC_TYPE_API === ( $arguments['data']['details']['sync_type'] ?? Constants::SYNC_TYPE_API ) ) {
			$arguments['app_type'] = 'ingram-micro-api';
		} else {
			$arguments['app_type'] = 'ingram-micro-sftp';
		}

		return $arguments;
	}

	public static function realter_supplier_data( $arguments ) {
		$app_type = $arguments['app_type'] ?? '';
		if ( is_string( $app_type ) && false !== strpos( $app_type, 'ingram' ) ) {
			$arguments['app_type'] = Constants::SUPPLIER_APP_TYPE_INGRAM_MICRO;
		}

		return $arguments;
	}
}
