<?php

namespace WcElectronInvoice\WooCommerce\Providers;

use WcElectronInvoice\Providers\OrderQuery;

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

class Order extends \WcElectronInvoice\Providers\Order {

	/**
	 * @var \WC_Order
	 *
	 */
	protected $order;

	/**
	 * Return order ID.
	 * If the orded is a refund, return the parent order ID.
	 * See class OrderRefund.
	 *
	 * @param $order
	 * @param $provider
	 */
	function get_id() {
		return $this->order->get_id();
	}

	function set_id() {
		$this->id = $this->order->get_id();
	}

	function get_parent_id() {

		if ( $this->order->get_type() === 'shop_order_refund' ) {
			return $this->order->get_parent_id();
		}

		return $this->order->get_id();
	}

	function set_data() {
		$this->data = $this->wc_get_order_once( $this->get_id() )->get_data();
	}

	public function get_data() {

		$data                    = $this->wc_get_order_once( $this->get_id() )->get_data();
		$data['provider']        = $this->get_provider();
		$data['provider_label']  = $this->get_provider_label();
		$data['currency']        = $this->get_currency();
		$data['currency_symbol'] = $this->get_currency_symbol( $this->get_currency() );
		$data['parent_id']       = $this->get_parent_id();

		$data['invoice_sent'] = $this->get_invoice_sent();
		$data['invoice_type'] = $this->get_invoice_type();

		return $data;
	}

	function wc_get_order_once( $order_id ) {
		if ( empty( $this->order ) ) {
			$this->order = wc_get_order( $order_id );
		}

		return $this->order;
	}

	function refresh_meta_data(): void {
        $this->order->read_meta_data(true);
    }

	function get_meta( $key, $single = true ) {
        $order = $this->wc_get_order_once($this->get_id());
		return $order->get_meta( $key, $single );
	}

    function update_meta($key, $value)
    {
        $order = $this->wc_get_order_once($this->get_id());
        $order->update_meta_data($key, $value);
        $order->save();
    }

	function delete_meta( $key ){
        $order = $this->wc_get_order_once($this->get_id());
		$order->delete_meta_data( $key );
        $order->save();
	}

	function get_total() {
		return $this->order->get_total();
	}

	function get_total_tax() {
		return $this->wc_get_order_once( $this->get_id() )->get_total_tax();
	}

	function get_total_refunded() {
		return $this->wc_get_order_once( $this->get_id() )->get_total_refunded();
	}

    function get_refunds()
    {
        $order = $this->wc_get_order_once($this->get_id());
        if (0 === (int)$this->get_total_refunded()) {
            return array();
        }

        return $order->get_refunds();
    }

	function get_billing_choice_type() {
		return $this->order->get_meta( '_billing_choice_type', true );
	}

	function get_invoice_type() {
		return $this->wc_get_order_once( $this->get_id() )->get_meta( '_billing_invoice_type', true );
	}

	function get_sdi_type() {
		return $this->wc_get_order_once( $this->get_id() )->get_meta( '_billing_sdi_type', true );
	}

	function get_user_id() {
		return $this->order->get_user_id();
	}

	function get_type() {
		return $this->order->get_type();
	}

	function get_type_label( $orderType = '' ) {

		$orderType = empty( $orderType ) ? $this->get_type() : $orderType;

		switch ( $orderType ) {
			case 'shop_order_refund':
				return __( 'Refund', WC_EL_INV_TEXTDOMAIN );
			default:
				return __( 'Order', WC_EL_INV_TEXTDOMAIN );
		}
	}

	function get_remaining_refund_amount() {
		return $this->wc_get_order_once( $this->get_id() )->get_remaining_refund_amount();
	}

    function get_remaining_refund_items()
    {
        $order = $this->wc_get_order_once($this->get_id());
        if (0 === (int)$this->get_total_refunded()) {
            return $order->get_item_count();
        }

        return $order->get_remaining_refund_items();
    }

    function get_total_qty_refunded()
    {
        $order = $this->wc_get_order_once($this->get_id());
        if (0 === (int)$order->get_total_refunded()) {
            return 0;
        }

        return $order->get_total_qty_refunded();
    }

	function get_items( $type = 'line_item' ) {

		$items = $this->wc_get_order_once( $this->get_id() )->get_items( $type );

		if ( $type === 'line_item' ) {
			// convert to OrderItem
			foreach ( $items as $key => $item ) {
				$items[ $key ] = $this->fromWcOrderItemToOrderItem( $item );
			}

			return $items;
		}


		return $items;

	}

    function get_qty_refunded_for_item()
    {
        $order = $this->wc_get_order_once($this->get_id());
        if (0 === (int)$this->get_total_refunded()) {
            return 0;
        }

        return $order->get_qty_refunded_for_item($this->get_id());
    }

	function get_invoice_sent( $order_id = '' ) {
		if( ! empty( $order_id  ) ){
			return OrderQuery::instance()->getProviderOrder($order_id, 'woocommerce')->get_invoice_sent();
		}

		return $this->get_meta( '_invoice_sent', true );
	}

	function get_invoice_sent_timestamp( $order_id = '' ) {
		if( ! empty( $order_id  ) ){
			return OrderQuery::instance()->getProviderOrder($order_id, 'woocommerce')->get_invoice_sent_timestamp();
		}

		return $this->get_meta( '_invoice_sent_timestamp', true );
	}

	function set_invoice_sent( $sent, $order_id = '' ) {
		$order_id = empty( $order_id ) ? $this->get_id() : $order_id;

		return $this->wc_get_order_once( $order_id )->update_meta_data( '_invoice_sent', $sent );
	}

	function get_order_number_invoice() {
		return $this->wc_get_order_once( $this->get_id() )->get_meta( 'order_number_invoice', true );
	}

	function get_billing_tax_code() {
		return $this->wc_get_order_once( $this->get_id() )->get_meta( '_billing_tax_code', true );
	}

	function get_vat_number() {
		return $this->wc_get_order_once( $this->get_id() )->get_meta( '_billing_vat_number', true );
	}

	function get_refund_payment_method() {
		return $this->wc_get_order_once( $this->get_id() )->get_meta( 'refund_payment_method', true );
	}

	function get_edit_user_url() {
		return get_edit_user_link( $this->get_user_id() );
	}

	function get_edit_order_url() {
		return $this->wc_get_order_once( $this->get_id() )->get_edit_order_url();
	}

	function get_provider() {
		return $this->provider->name();
	}

	function get_provider_label() {
		return $this->provider->get_label();
	}

	function get_status() {
		return $this->order->get_status();
	}

	function get_currency() {
		return $this->order->get_currency();
	}

	function get_currency_symbol( $currency = '' ) {
		$currency = empty( $currency ) ? $this->get_currency() : $currency;

		return get_woocommerce_currency_symbol( $currency );
	}

	public function get_payment_method() {
		return $this->wc_get_order_once( $this->get_id() )->get_payment_method();
	}

	function set_order( $order ) {
		$this->order = $order;
	}

	function set_payment_method() {
		$this->payment_method = $this->get_payment_method();
	}

	function get_billing_first_name() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_first_name();
	}

	public function get_billing_last_name() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_last_name();
	}

	public function get_billing_company() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_company();
	}

	public function get_billing_address_1() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_address_1();
	}

	public function get_billing_address_2() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_address_2();
	}

	public function get_billing_city() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_city();
	}

	public function get_billing_state() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_state();
	}

	public function get_billing_postcode() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_postcode();
	}

	public function get_billing_country() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_country();
	}

	public function get_billing_email() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_email();
	}

	public function get_billing_phone() {
		return $this->wc_get_order_once( $this->get_id() )->get_billing_phone();
	}

	public function get_shipping_first_name() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_first_name();
	}

	public function get_shipping_last_name() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_last_name();
	}

	public function get_shipping_company() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_company();
	}

	public function get_shipping_address_1() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_address_1();
	}

	public function get_shipping_address_2() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_address_2();
	}

	public function get_shipping_city() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_city();
	}

	public function get_shipping_state() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_state();
	}

	public function get_shipping_postcode() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_postcode();
	}

	public function get_shipping_country() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_country();
	}

	public function get_shipping_method() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_method();
	}

	public function get_shipping_total() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_total();
	}

	public function get_shipping_tax() {
		return $this->wc_get_order_once( $this->get_id() )->get_shipping_tax();
	}

	function has_status( $status ) {
		return $this->get_status() === $status;
	}

    function get_date_paid() {
        return $this->wc_get_order_once( $this->get_id() )->get_date_paid();
    }

	function get_date_completed() {
		return $this->wc_get_order_once( $this->get_id() )->get_date_completed();
	}

	function get_date_created() {
		return $this->wc_get_order_once( $this->get_id() )->get_date_created();
	}

    function get_date_modified() {
        return $this->wc_get_order_once( $this->get_id() )->get_date_modified();
    }

	function get_nature_rc() {
		return $this->wc_get_order_once( $this->get_id() )->get_meta( 'nature_rc', true );
	}

	function get_ref_norm_rc() {
		return $this->wc_get_order_once( $this->get_id() )->get_meta( 'ref_norm_rc', true );
	}

    function get_total_refunded_for_item($item_id, $item_type)
    {
        $order = $this->wc_get_order_once($this->get_id());
        if (0 === (int)$this->get_total_refunded()) {
            return 0;
        }

        return $order->get_total_refunded_for_item($item_id, $item_type);
    }

	function get_tax_refunded_for_item( $item_id, $item_type ) {
        $order = $this->wc_get_order_once($this->get_id());
        if (0 === (int)$this->get_total_refunded()) {
            return 0;
        }

		return $order->get_tax_refunded_for_item( $item_id, $item_type );
	}

	function set_date_completed( $timestamp ) {
		$this->wc_get_order_once( $this->get_id() )->set_date_completed( $timestamp );
	}

	/**
	 * It wraps the WC_Product $wcProduct into a Product
	 *
	 * @param $wcProduct
	 *
	 * @return Product $product
	 */
	private function fromWcProductToProduct( $wcProduct ) {
		return new Product( $wcProduct );
	}

	/**
	 * It wraps the WC_Order_Item $wcOrderItem into a OrderItem
	 *
	 * @param $wcOrderItem
	 *
	 * @return OrderItem
	 */
	private function fromWcOrderItemToOrderItem( $wcOrderItem ) {
		return new OrderItem( $wcOrderItem );
	}

	function save() {
		return $this->wc_get_order_once( $this->get_id() )->save();
	}

	function set_invoice_sent_timestamp( $timestamp, $order_id = '' ) {
		$order_id = empty( $order_id ) ? $this->get_id() : $order_id;
		$this->wc_get_order_once( $order_id )->update_meta_data( '_invoice_sent_timestamp', $timestamp );
	}
}
