<?php
/**
 * GurmeCargo için admin menülerini olşturan sınıfı olan GCARGO_Admin_Menu sınıfını barındıran dosya.
 *
 * @package GurmeHub
 */

/**
 * GurmeCargo admin menü ve bar sınıfı
 */
class GCARGO_Admin {


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

	/**
	 * Eklenti simgesi
	 *
	 * @var string $icon
	 */
	protected $icon = '';

	/**
	 * Eklenti menü ismi
	 *
	 * @var string $parent_title
	 */
	public $parent_title = 'Kargo Entegratör';

	/**
	 * Eklenti menü urlini oluşturacak slug
	 *
	 * @var string $parent_slug
	 */
	public $parent_slug = 'gurmecargo';

	/**
	 * Admin menüye eklenecek menüleri ekler ve callback fonksiyonlarını organize eder
	 *
	 * @return void
	 *   */
	public function admin_menu() {

		$admin_menu_pages = array(
			array(
				'parent_slug' => 'admin.php',
				'menu_title'  => __( 'Bulk Print', 'kargo-entegrator' ),
				'menu_slug'   => "{$this->prefix}-bulk-print",
				'hidden'      => true,
			),
			array(
				'parent_slug' => 'admin.php',
				'menu_title'  => __( 'Bulk Create', 'kargo-entegrator' ),
				'menu_slug'   => "{$this->prefix}-bulk-create",
				'hidden'      => true,
			),
			array(
				'parent_slug' => 'woocommerce',
				'menu_title'  => __( 'Shipments', 'kargo-entegrator' ),
				'menu_slug'   => "{$this->prefix}-shipments",
				'callback'    => array( gcargo_shipments_table(), 'render_shipments_page' ),
			),
			array(
				'parent_slug' => 'woocommerce',
				'menu_title'  => __( 'Returneds', 'kargo-entegrator' ),
				'menu_slug'   => "{$this->prefix}-returned-shipments",
				'callback'    => array( gcargo_returneds_table(), 'render_returneds_page' ),
			),
		);

		foreach ( $admin_menu_pages as $sub_menu_page ) {
			add_submenu_page(
				$sub_menu_page['parent_slug'],
				$sub_menu_page['menu_title'],
				"<gcargo-icon aria-label='Kargo Entegratör'></gcargo-icon>" . $sub_menu_page['menu_title'],
				gcargo_capability(),
				$sub_menu_page['menu_slug'],
				isset( $sub_menu_page['callback'] ) ? $sub_menu_page['callback'] : array( $this, 'view' ),
			);
		}
	}

	/**
	 * Eklenti alt menüleri açıldığında ilgili vue sayfasını render eder
	 *
	 * @return void
	 */
	public function view() {

		/**
		 * Nonce verification for admin pages.
		 */
		if ( ! current_user_can( gcargo_capability() ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'kargo-entegrator' ) );
		}

		$request = gcargo_clean( $_GET );
		$page    = isset( $request['page'] ) ? str_replace( "{$this->prefix}-", '', gcargo_clean( $request['page'] ) ) : false; //phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( $page ) {
			gcargo_vue()
				->set_vue_page( $page )
				->set_localize( $this->get_localize_data( $page ) )
				->require_script()
				->require_style()
				->create_app_div();
		}
	}

	/**
	 * Vue render edildiğinde kullanacağı verileri düzenler.
	 *
	 * @param string $page Açılan sayfa.
	 *
	 * @return array
	 */
	private function get_localize_data( $page ) {
		$localize = gcargo_data_handler()->default_localize_data();
		$request  = gcargo_clean( $_GET );

		switch ( $page ) {
			case 'bulk-print':
				$localize['print_data'] = isset( $request['print-data'] ) ? $request['print-data'] : false;
				break;
			case 'bulk-create':
				$warehouses                     = gcargo_saas_warehouses_settings()->get_settings();
				$cargo_integrations             = gcargo_saas_cargo_integrations_settings()->get_settings();
				$shipment_setting               = gcargo_saas_shipment_settings()->get_settings();
				$localize['warehouses']         = $warehouses;
				$localize['cargo_integrations'] = $cargo_integrations;
				$localize['shipment_setting']   = $shipment_setting;

				$order_ids = isset( $request['order_ids'] ) ? $request['order_ids'] : array();
				$order_ids = array_map( 'absint', (array) $order_ids );
				$order_ids = array_filter( $order_ids );

				$localize['orders'] = array_map(
					function ( $order_id ) use ( $warehouses, $cargo_integrations, $shipment_setting ) {
						return gcargo_shipment_map_wc_order(
							$order_id,
							$warehouses,
							$cargo_integrations,
							$shipment_setting
						)->create_request();
					},
					array_reverse( $order_ids )
				);
				break;
		}
		return apply_filters( "{$this->prefix}_get_localize_data", $localize );
	}
}
