<?php

namespace WcElectronInvoice\WooCommerce\Providers;

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

class OrderRefund extends \WcElectronInvoice\Providers\OrderRefund {
	/**
	 * @var \Automattic\WooCommerce\Admin\Overrides\OrderRefund
	 *
	 */
	protected $order;

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

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

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

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

	function set_payment_method() {
		// TODO: Implement set_payment_method() method.
	}

	function get_data() {

		$data                    = wc_get_order( $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 get_status() {
		return wc_get_order( $this->get_id() )->get_status();
	}

	function get_meta( $key, $default = '' ) {
        $order = wc_get_order($this->get_id());
        return $order->get_meta( $key, true );
	}

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

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

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

	function get_total_refunded() {
		$parentOrder = wc_get_order( $this->get_parent_id() );

		return $parentOrder ? $parentOrder->get_total_refunded() : 0;
	}

	function get_refunds() {
		return [];
	}

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

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

	function get_parent_order() {
		return wc_get_order( $this->get_parent_id() );
	}

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

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

	function get_sdi_type() {
		return wc_get_order( $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 wc_get_order( $this->get_id() )->get_remaining_refund_amount();
	}

	function get_remaining_refund_items() {
		return wc_get_order( $this->get_id() )->get_remaining_refund_items();
	}

	function get_total_qty_refunded() {
		return wc_get_order( $this->get_id() )->get_total_qty_refunded();
	}

	/**
	 * @inheritDoc
	 */
	function get_items( $type = 'line_item' ) {
		return wc_get_order( $this->get_id() )->get_items( $type );
	}

	function get_qty_refunded_for_item() {
		return wc_get_order( $this->get_id() )->get_qty_refunded_for_item( $this->get_id() );
	}

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

		return \WcElectronInvoice\Functions\getPostMeta( '_invoice_sent', null, $order_id );
	}

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

		return \WcElectronInvoice\Functions\getPostMeta( '_invoice_sent_timestamp', null, $order_id );
	}

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

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

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

		wc_get_order( $order_id )->update_meta_data( '_invoice_sent_timestamp', $timestamp );
	}

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

	function get_order_number_invoice() {
		return wc_get_order( $this->get_id() )->get_meta( "refund_number_invoice-{$this->get_id()}", true );
	}

	function get_refund_payment_method() {
		return wc_get_order( $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 wc_get_order( $this->get_id() )->get_edit_order_url();
	}

	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 );
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	function orders_json_data( $data ) {

		$newData = array(
			'provider'        => $this->get_provider(),
			'provider_label'  => $this->get_provider_label(),
			'currency_symbol' => $this->get_currency_symbol( $this->get_currency() ),
		);

		return array_merge( $data, $newData );
	}

	function orders_json_args_filter_var( $args ) {

		$newArgs = array(
			'provider'        => FILTER_UNSAFE_RAW,
			'provider_label'  => FILTER_UNSAFE_RAW,
			'currency_symbol' => FILTER_UNSAFE_RAW,
		);

		return array_merge( $args, $newArgs );
	}

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

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

	function get_total_refunded_for_item( $item_id, $item_type ) {
		return wc_get_order( $this->get_parent_id() )->get_total_refunded_for_item( $item_id, $item_type );
	}

	function get_tax_refunded_for_item( $item_id, $item_type ) {
		return wc_get_order( $this->get_parent_id() )->get_tax_refunded_for_item( $item_id, $item_type );
	}

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

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

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

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