<?php

namespace WooPoPCozmosLabsPMS\Providers;

use WcElectronInvoice\Providers\TaxProviderAbstract;

defined( 'ABSPATH' ) || die( 'Not allowed.' );

class TaxesProvider extends TaxProviderAbstract {

	/**
	 * @inheritDoc
	 */
	function get_rates( $tax_class = '', $customer = null ) {
		// TODO: Implement get_rates() method.
	}

	/**
	 * @inheritDoc
	 */
	function get_base_tax_rates() {
		$taxSettings = self::get_tax_settings();
		if ( ! $taxSettings['enable_tax'] ) {
			return array();
		}

		// TODO: PMS support tax rates
		// right now we return only default rate

		return [
			'rate'     => (float) self::get_default_rate(),
			'label'    => esc_attr__( 'Imposte', WC_EL_INV_TEXTDOMAIN ),
			'shipping' => 'no',
			'compound' => 'no',
		];
	}

	/**
	 * @inheritDoc
	 */
	function get_rates_for_tax_class( $tax_class ) {
		$taxSettings = self::get_tax_settings();
		if ( ! $taxSettings['enable_tax'] ) {
			return array();
		}

		// TODO: PMS support tax rates
		// right now we return only default rate in an array
		$taxClass = (object) [
			'tax_rate_id'       => 1,
			'tax_rate_country'  => self::get_default_billing_country(),
			'tax_rate_state'    => '',
			'tax_rate'          => (float) self::get_default_rate(),
			'tax_rate_name'     => esc_attr__( 'Imposte', WC_EL_INV_TEXTDOMAIN ),
			'tax_rate_priority' => 1,
			'tax_rate_compound' => 0,
			'tax_rate_shipping' => 0,
			'tax_rate_order'    => 0,
			'tax_rate_class'    => $tax_class,
			'postcode_count'    => 0,
			'city_count'        => 0,
		];

		return [ $taxClass ];
	}

	/**
	 * @inheritDoc
	 */
	function get_shipping_tax_rates() {
		// no shipping rates in PMS
		return array();
	}

	/**
	 * @inheritDoc
	 */
	function get_shipping_item_tax_rate($item) {
		// Cozmos doesn't handle shipping items with tax_class
		// Try to get from tax_class as fallback
		$tax_class = $item->get_tax_class();
		if (!empty($tax_class)) {
			$taxes = self::get_rates_for_tax_class($tax_class);
			if (!empty($taxes)) {
				return (object) reset($taxes);
			}
		}
		return null;
	}

	/**
	 * @inheritDoc
	 */
	function get_shipping_item_tax_rate_id($item) {
		$taxRate = $this->get_shipping_item_tax_rate( $item );

		return isset( $taxRate->tax_rate_id ) ? (int) $taxRate->tax_rate_id : null;
	}

	/**
	 * @inheritDoc
	 */
	function is_tax_enabled() {
		$taxEnabled = self::get_tax_settings( 'enable_tax' );

		// WooCommerce uses 'yes' or 'no' for tax enabled
		return $taxEnabled ? 'yes' : 'no';
	}

	/**
	 * @inheritDoc
	 */
	static function get_tax_settings( $setting = '', $default = '' ) {

		$settings = get_option( 'pms_tax_settings', array() );

		// instead of using 1 or 0 for true or false, we use the actual boolean values
		$settings['enable_tax'] = isset( $settings['enable_tax'] ) ? (bool) $settings['enable_tax'] : false;

		if ( ! empty( $setting ) ) {
			return isset( $settings[ $setting ] ) ? $settings[ $setting ] : $default;
		}

		return $settings;
	}

	static function get_default_rate() {
		return (float) self::get_tax_settings( 'default_tax_rate' );
	}

	static function get_default_billing_country() {
		return self::get_tax_settings( 'default-billing-country' );
	}

	static function get_merchant_vat_country() {
		return self::get_tax_settings( 'merchant-vat-country' );
	}

	/**
	 * @inheritDoc
	 */
	function name() {
		return 'cozmoslabspms';
	}
}
