<?php

namespace WPDesk\FakturyKSEF\Xml;

use LogicException;
use WPDesk\CoreAlias\InvoicesIntegration;
use WPDesk\FakturyKSEF\Xml\DocumentData\DocumentData;

class DataMapper {

	public const PAYMENT_COD      = 'cod';
	public const PAYMENT_CASH     = 'gotowka';
	public const PAYMENT_BACS     = 'bacs';
	public const PAYMENT_TRANSFER = 'przelew-bankowy';
	public const PAYMENT_CARD     = 'card';
	public const PAYMENT_CHEQUE   = 'cheque';

	public const WDT_ZERO_RATE = '0 WDT';
	public const EX_ZERO_RATE  = '0 EX';
	public const KR_ZERO_RATE  = '0 KR';

	/**
	 * @param string $payment_method
	 *
	 * @return int|mixed
	 */
	public function get_payment_method( string $payment_method = '' ) {

		$method_id = 7;
		switch ( $payment_method ) {
			case self::PAYMENT_COD:
			case self::PAYMENT_CASH:
				$method_id = 1;
				break;
			case self::PAYMENT_BACS:
			case self::PAYMENT_TRANSFER:
				$method_id = 6;
				break;
			case self::PAYMENT_CARD:
				$method_id = 2;
				break;
			case self::PAYMENT_CHEQUE:
				$method_id = 4;
				break;
		}

		return apply_filters( 'fiksef_datamapper_payment_method', $method_id, $payment_method );
	}

	public static function get_field_name_by_xml_name( $name ) {
		switch ( $name ) {
			case 'P_7':
				return 'P_7 (Nazwa produktu.)';
			case 'P_8A':
				return 'P_8A (J.m.)';
			case 'P_12':
				return 'P_12 (Stawka podatku)';
			case 'Zaplacono':
				return 'Zapłacono';
		}

		return $name;
	}

	public static function get_ksef_zero_rate( DocumentData $document, $item ): string {

		$customer         = $document->get_customer();
		$customer_country = strtoupper( (string) $customer->get_country() );

		if ( '' === $customer_country ) {
			throw new LogicException( esc_html__( 'The customer\'s country cannot be determined for mapping the 0% rate to KSeF.', 'ksef-for-flexible-invoices' ) );
		}

		if ( 'PL' === $customer_country ) {
			return self::KR_ZERO_RATE;
		}

		if ( ! InvoicesIntegration::is_pro() ) {
			$purchase_url = get_locale() === 'pl_PL' ? 'https://wpdesk.link/faktury-woocomemrce-pro-ksef' : 'https://wpdesk.link/flexible-invoices-pro';
			throw new LogicException(
				sprintf(
				// translators: 1. Open link, 2. Close link
					esc_html__( 'You can send OSS and WDT invoices using %1$sFlexible Invoices PRO →%2$s', 'ksef-for-flexible-invoices' ),
					'<a href="' . $purchase_url . '" target="_blank">',
					'</a>'
				)
			);
		}

		$is_ue_country = in_array( $customer_country, WC()->countries->get_european_union_countries(), true );

		$product_id = ! empty( $item['wc_variation_id'] ) ? (int) $item['wc_variation_id'] : 0;
		$product_id = $product_id > 0 ? $product_id : (int) ( $item['wc_product_id'] ?? 0 );

		$product = wc_get_product( $product_id );

		if ( ! $product ) {
			// translators: 1. Product id
			throw new LogicException( sprintf( esc_html__( 'Unable to determin whether product #%d is virtual or not. Product problably doesn\'t exists. Try setting 0 vat rate type manually.', 'ksef-for-flexible-invoices' ), $product_id ) );
		}

		$physical_product = ! $product->is_virtual();

		if ( $is_ue_country ) {
			if ( ! $physical_product ) {
				throw new LogicException( esc_html__( 'Unable to map 0% rate to 0 WDT. Item is not an physical product. Try setting 0 vat rate type manually.', 'ksef-for-flexible-invoices' ) );
			}
			return self::WDT_ZERO_RATE;
		}

		if ( ! $physical_product ) {
			throw new LogicException( esc_html__( 'Unable to map 0% rate to 0 EX. Item is not an physical product. Try setting 0 vat rate type manually.', 'ksef-for-flexible-invoices' ) );
		}

		return self::EX_ZERO_RATE;
	}
}
