<?php
/**
 * Class ParcelShop
 *
 * @package DhlPolska\API
 */

namespace DhlPolska\API;

use DhlPolska\API\ParcelShop\Method\CreateShipment;
use DhlPolska\API\ParcelShop\Method\DeleteShipment;
use DhlPolska\API\ParcelShop\Method\GetLabel;
use DhlPolska\API\ParcelShop\Method\GetNearestServicepoints;
use DhlPolska\API\ParcelShop\Method\GetPnp;
use DhlPolska\API\ParcelShop\SoapClient as ParcelShopSoapClient;
use SoapClient;

/**
 * ParcelShop API.
 */
class ParcelShop extends WebAPI {

	public const API = 'ParcelShop';

	public const TEST_WSDL = 'https://sandbox.dhl24.com.pl/servicepoint.html?wsdl';
	public const PROD_WSDL = 'https://dhl24.com.pl/servicepoint.html?wsdl';

	/**
	 * @param string $wsdl .
	 * @param array  $options .
	 *
	 * @return SoapClient|ParcelShopSoapClient
	 * @codeCoverageIgnore
	 */
	protected function create_soap_client( string $wsdl, array $options ): SoapClient {
		return new ParcelShopSoapClient( static::API, $wsdl, $options, $this->logger );
	}

	/**
	 * Check connection.
	 *
	 * @throws \Exception .
	 */
	public function check_connection() {
		$this->get_client()->getNearestServicepoints(
			new GetNearestServicepoints(
				$this->auth_data,
				'00001',
				'Warszawa',
				1
			)
		);
	}

	/**
	 * @param ShipmentDataInterface $shipment_data .
	 *
	 * @return string Shipment ID.
	 */
	public function create_shipment( ShipmentDataInterface $shipment_data ): string {
		$response = $this->get_client()->createShipment( new CreateShipment( $this->auth_data, $shipment_data ) );

		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
		return $response->createShipmentResult->shipmentNumber;
	}

	/**
	 * @param string $shipment_id .
	 * @param string $label_type  .
	 *
	 * @return string Label content (base64).
	 */
	public function get_label( string $shipment_id, string $label_type ): string {
		$response = $this->get_client()->getLabel( new GetLabel( $this->auth_data, $shipment_id, $label_type ) );

		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
		return $response->getLabelResult->labelContent;
	}

	/**
	 * @param string $date .
	 * @param string $type .
	 *
	 * @return array{file_name: string, file_data: string, mime_type: string}
	 */
	public function get_pnp( string $date, string $type ): array {
		$response = $this->get_client()->getPnp( new GetPnp( $this->auth_data, $date ) );

		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
		$result = $response->getPnpResult ?? null;
		if ( ! is_object( $result ) ) {
			throw new \Exception( 'DHL ParcelShop getPnp error.' );
		}

		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
		$file_name = (string) ( $result->labelName ?? '' );
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
		$file_data = (string) ( $result->labelContent ?? '' );
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
		$mime_type = (string) ( $result->labelFormat ?? '' );

		if ( '' === $file_name || '' === $file_data || '' === $mime_type ) {
			throw new \Exception( 'DHL ParcelShop getPnp error.' );
		}

		return [
			'file_name' => $file_name,
			'file_data' => $file_data,
			'mime_type' => $mime_type,
		];
	}

	/**
	 * @param string $shipment_id .
	 *
	 * @return void
	 */
	public function delete_shipment( string $shipment_id ): void {
		$this->get_client()->deleteShipment( new DeleteShipment( $this->auth_data, $shipment_id ) );
	}
}
