<?php
/**
 * GurmeCargo meta boxları olşturan sınıf olan GCARGO_Meta_Boxes sınıfını barındıran dosya.
 *
 * @package GurmeHub
 */

use Automattic\WooCommerce\Utilities\OrderUtil;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;

/**
 * GurmeCargo meta box sınıfı.
 */
class GCARGO_Meta_Boxes {

	/**
	 * Eklenti Prefix
	 *
	 * @var string $prefix
	 */
	protected $prefix = GCARGO_PREFIX;

	/**
	 * Meta boxları kayıt etme.
	 *
	 * @return void
	 * @SuppressWarnings(PHPMD.StaticAccess)
	 */
	public function add_meta_box() {
		if ( class_exists( CustomOrdersTableController::class ) && function_exists( 'wc_get_container' ) ) {
			$hpos_enabled = wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled();
			if ( OrderUtil::is_order_edit_screen() ) {
				add_meta_box( "{$this->prefix}_shop_order_meta_box", 'Kargo Entegratör', array( $this, 'shop_order_meta_box' ), $hpos_enabled ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order', 'side', 'high' );
			}
		}
	}

	/**
	 * GurmePOS shop order meta boxu render eder
	 *
	 * @param Automattic\WooCommerce\Admin\Overrides\Order|WC_Order|WP_Post $post Post
	 */
	public function shop_order_meta_box( $post ) {

		$post_id = $post instanceof Automattic\WooCommerce\Admin\Overrides\Order || $post instanceof WC_Order ? $post->get_id() : $post->ID;

		if ( $post_id ) {

			$order = wc_get_order( $post_id );
			if ( $order && ! in_array( $order->get_status(), gcargo_get_unwanted_order_statuses(), true ) ) {

				$shipments = gcargo_shipments()->get_shipments_by_order_id( $post_id );
				$returneds = gcargo_returneds()->get_returneds_by_order_id( $post_id );

				$shipment_mapper = gcargo_shipment_map_wc_order( $post_id );
				$returned_mapper = gcargo_returned_map_wc_order( $post_id );
				$localize        = array_merge(
					gcargo_data_handler()->default_localize_data(),
					array(
						'order_id'              => $post_id,
						'shipments'             => $shipments,
						'returneds'             => $returneds,
						'returned'              => $returned_mapper->create_request(),
						'order_shipment_status' => $shipment_mapper->get_status(),
						'order_returned_status' => $returned_mapper->get_status(),
						'shipment'              => $shipment_mapper->create_request(),
						'warehouses'            => $shipment_mapper->get_warehouses(),
						'cargo_integrations'    => $shipment_mapper->get_cargo_integrations(),
						'api_key'               => gcargo_api_key()->get(),
						'auto_shipment_error'   => wc_get_order( $post_id )->get_meta( 'gcargo_shipment_error', true ),
					)
				);

				gcargo_vue()
				->set_localize( $localize )
				->set_vue_page( 'admin-order-detail' )
				->require_script()
				->require_style()
				->create_app_div();
			} else {
				esc_html_e( 'Sipariş bulunamadı yada sipariş durumu kargo oluşturmaya uygun değil.', 'kargo-entegrator' );
			}
		}
	}
}
