<?php
/**
 * @package PaczkomatyInpost
 */

/**
 * Class WPDesk_Paczkomaty_Allegro
 */
class WPDesk_Paczkomaty_Allegro {

	/**
	 * Default width/height/length
	 */
	const PACZKOMAT_DEFAULT_WHL = 300;

	/**
	 * Plugin
	 *
	 * @var WPDesk_Paczkomaty_Plugin
	 */
	private $plugin;

	/**
	 * @var array
	 */
	private $allegro_shipment_id_usluga = array(
		'10022'                                => 'paczkomaty',         // Paczkomaty 24/7.
		'20022'                                => 'paczkomaty',         // Paczkomaty 24/7 COD.
		'10023'                                => 'allegro_paczkomaty', // Allegro Paczkomaty InPost.
		'20023'                                => 'allegro_paczkomaty', // Allegro Paczkomaty InPost COD.
		'16'                                   => 'allegro_polecony',   // Allegro miniKurier24 InPost.
		'17'                                   => 'allegro_kurier',     // Allegro Kurier InPost.
		'18'                                   => 'allegro_kurier',     // Allegro Kurier InPost COD.
		'33'                                   => 'kurier',             // Kurier InPost.
		'34'                                   => 'kurier',             // Kurier InPost COD.
		'98f86f81-0018-41c5-ac83-073a56fc7021' => 'allegro_polecony',   // Allegro miniKurier24 InPost COD.
	);

	/**
	 * @var array
	 */
	private $allegro_shipment_id_cod = array(
		'20022',                                // Paczkomaty 24/7 COD.
		'20023',                                // Allegro Paczkomaty InPost COD.
		'18',                                   // Allegro Kurier InPost COD.
		'34',                                   // Kurier InPost COD.
		'98f86f81-0018-41c5-ac83-073a56fc7021', // Allegro miniKurier24 InPost COD.
	);

	/**
	 * WPDesk_Paczkomaty_Allegro constructor.
	 *
	 * @param WPDesk_Paczkomaty_Plugin $plugin Access to methods of plugin main class.
	 */
	public function __construct( WPDesk_Paczkomaty_Plugin $plugin ) {
		$this->plugin = $plugin;
	}

	/**
	 * Hook various behaviors to filters and actions.
	 */
	public function hooks() {
		add_action( 'paczkomaty_shipping_method_allegro_shipment', array( $this, 'paczkomaty_shipping_method_allegro_shipment' ), 10, 3 );
	}

	/**
	 * Creates shipment.
	 *
	 * @param WC_Order $order Class object.
	 * @param string   $usluga Name of service.
	 *
	 * @return WPDesk_Flexible_Shipping_Shipment_Paczkomaty
	 */
	private function create_shipment( $order, $usluga ) {
		$fs_method = array(
			'method_integration' => 'paczkomaty',
			'paczkomaty_usluga'  => $usluga,
		);
		/** @var WPDesk_Flexible_Shipping_Shipment_Paczkomaty $shipment */
		$shipment = fs_create_shipment( $order, $fs_method );
		return $shipment;
	}

	/**
	 * Adds post_meta for the shipment.
	 *
	 * @param WPDesk_Flexible_Shipping_Shipment_Paczkomaty $shipment Class object.
	 * @param string                                       $parcel_locker_name Name of parcel locker.
	 * @param WC_Paczkomaty_Shipping_Method                $paczkomaty_shipping_method Class object.
	 *
	 * @return WPDesk_Flexible_Shipping_Shipment_Paczkomaty
	 */
	public function add_meta_for_paczkomaty( $shipment, $parcel_locker_name, $paczkomaty_shipping_method ) {
		$shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_ID, $parcel_locker_name );

		if ( $paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_DEFAULT_PACKAGE_SIZE, '' ) ) {
			$shipment->set_meta( '_paczkomat_size', $paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_DEFAULT_PACKAGE_SIZE, '' ) );
		}

		if ( $paczkomaty_shipping_method->get_parcel_lockers_api() == WC_Paczkomaty_Shipping_Method::API_TYPE_XML ) {
			if ( $paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_SELF_SEND, '' ) ) {
				if ( $paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_SELF_SEND, '' ) == '1' ) {
					$shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SENDING_METHOD, WC_Paczkomaty_Shipping_Method::SENDING_METHOD_PARCEL_LOCKER );
				}
				if ( $paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_SELF_SEND, '' ) == '3' ) {
					$shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SENDING_METHOD, WC_Paczkomaty_Shipping_Method::SENDING_METHOD_DISPATCH_ORDER );
				}
			}
		} else {
			if ( $paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_SENDING_METHOD, '' ) ) {
				$shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SENDING_METHOD, $paczkomaty_shipping_method->get_option( WC_Paczkomaty_Shipping_Method::SETTING_SENDING_METHOD, '' ) );
			}
		}
		return $shipment;
	}

	/**
	 * Add shipment - adds shipment for order from Allegro.
	 *
	 * @param WC_Order $order Order.
	 * @param string   $allegro_shipment_id Allegro shipment ID.
	 * @param string   $parcel_locker_name Parcel locker.
	 */
	public function paczkomaty_shipping_method_allegro_shipment( $order, $allegro_shipment_id, $parcel_locker_name ) {
		$usluga = $this->allegro_shipment_id_usluga[ $allegro_shipment_id ];

		$order->calculate_totals();

		$shipment = $this->create_shipment( $order, $usluga );

		$shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_KEY_PACZKOMATY_USLUGA, $usluga );

		$paczkomaty_shipping_method = $this->plugin->get_paczkomaty_shipping_method();

		if ( WC_Paczkomaty_Shipping_Method::USLUGA_PACZKOMATY === $usluga
			|| WC_Paczkomaty_Shipping_Method::USLUGA_ALLEGRO_PACZKOMATY === $usluga
		) {
			$shipment = $this->add_meta_for_paczkomaty( $shipment, $parcel_locker_name, $paczkomaty_shipping_method );
		}

		if ( WC_Paczkomaty_Shipping_Method::USLUGA_KURIER === $usluga
			|| WC_Paczkomaty_Shipping_Method::USLUGA_ALLEGRO_KURIER === $usluga
		) {
			$shipment->set_meta(
				WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_WIDTH,
				self::PACZKOMAT_DEFAULT_WHL
			);
			$shipment->set_meta(
				WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_HEIGHT,
				self::PACZKOMAT_DEFAULT_WHL
			);
			$shipment->set_meta(
				WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_LENGTH,
				self::PACZKOMAT_DEFAULT_WHL
			);
		}

		$shipment->set_meta(
			WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_DROPOFF_PARCEL_MACHINE,
			$paczkomaty_shipping_method->get_option( 'default_parcel_locker' )
		);
		$shipment->set_meta(
			WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_CUSTOMER_REF,
			// Translators: order number.
			sprintf( __( 'Zamówienie %s', 'woocommerce-paczkomaty-inpost' ), $shipment->get_order()->get_order_number() )
		);
		$shipment->set_meta(
			WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_COD_AMOUNT,
			$shipment->get_order()->get_total()
		);
		$shipment->set_meta(
			WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_SENDING_METHOD,
			$paczkomaty_shipping_method->get_option( 'sending_method', '' )
		);
		$shipment->set_meta(
			WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_WEIGHT,
			( new WooCommerce_Order_Weight_Calculator( $shipment->get_order() ) )->get_order_weight_kg()
		);

		if ( in_array( strval( $allegro_shipment_id ), $this->allegro_shipment_id_cod, true ) ) {
			$shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_COD, '1' );
		} else {
			$shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_PACZKOMAT_COD, '0' );
		}

		$shipment->set_meta( WPDesk_Flexible_Shipping_Shipment_Paczkomaty::META_FLEXIBLE_SHIPPING_STATUS, 'new' );

		$shipment->save();
	}

}
