<?php

namespace Wetail\Shipping\Engine;


use WC_Order;

defined( __NAMESPACE__ . '\PATH' ) or die();

if ( class_exists( __NAMESPACE__ . '\Wetail_Shipping_Order_Admin' ) ) {
	return;
}

class Wetail_Shipping_Order_Admin {
	/**
	 * Modify table
	 */
	static public function init() {

		add_action( 'load-edit.php', [ __CLASS__, 'load_edit_action_handler' ] );
		add_action( 'load-post.php', [ __CLASS__, 'load_edit_action_handler' ] );
		add_action( 'load-woocommerce_page_wc-orders', [ __CLASS__, 'load_edit_action_handler' ], 9999 ); // HPOS.

		/**
		 * @since 0.8.1
		 * @wrike https://www.wrike.com/open.htm?id=1337528076
		 */
		add_filter( 'bulk_actions-edit-shop_order', [ __CLASS__, 'add_bulk_actions' ], 20, 1 );
		add_filter( 'bulk_actions-woocommerce_page_wc-orders', [ __CLASS__, 'add_bulk_actions' ], 20, 1 ); // HPOS.
		add_filter( 'handle_bulk_actions-edit-shop_order', [ __CLASS__, 'handle_bulk_actions' ], 1, 3 );
		add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', [
			__CLASS__,
			'handle_bulk_actions'
		], 1, 3 ); // HPOS.
	}

	/**
	 * Moved some calls to inner method
	 *
	 * @return void
	 *
	 * @since 0.8.1
	 * @wrike https://www.wrike.com/open.htm?id=1337528076
	 */
	public static function load_edit_action_handler() {
		add_thickbox();

		// Fix: Isolate Thickbox from concatenation to avoid "imgLoader is not defined" error due to strict mode
		wp_add_inline_script( 'thickbox', '/* strict mode fix: isolate from concatenation */' );

		// Modify columns
		add_filter( 'manage_shop_order_posts_columns', [ __CLASS__, 'add_columns' ] );
		add_filter( 'woocommerce_shop_order_list_table_columns', [ __CLASS__, 'add_columns' ] );

		// Get column content
		add_filter( 'manage_posts_custom_column', [ __CLASS__, 'get_column_content' ], 10, 2 );
		add_action( 'woocommerce_shop_order_list_table_custom_column', [ __CLASS__, 'get_column_content' ], 10, 2 );

		add_action( 'admin_head', [ __CLASS__, 'add_js_templates' ] );
	}

	/**
	 * Handles attempts for custom bulk action
	 * It will always ignore unknown bulk action but throw error for custom we added
	 * It supposed to be processed via AJAX call
	 *
	 * @param $redirect_to string
	 * @param $action string
	 * @param $post_ids array
	 *
	 * @throws \Exception
	 *
	 * @since 0.8.1
	 * @wrike https://www.wrike.com/open.htm?id=1337528076
	 */
	public static function handle_bulk_actions( $redirect_to, $action, $post_ids ) {
		if ( $action !== 'wetail_shipping_print_labels' ) {
			return $redirect_to;
		}
		throw new \Exception( 'Not implemented. You\'re supposed to use AJAX call instead' );
	}

	/**
	 * Adds bulk action to shop order listing
	 *
	 * @param $bulk_array array
	 *
	 * @return array
	 *
	 * @since 0.8.1
	 * @wrike https://www.wrike.com/open.htm?id=1337528076
	 */
	public static function add_bulk_actions( $bulk_array ) {
		$bulk_array[ 'wetail_shipping_print_labels' ] = esc_html__( 'Print labels', 'wetail-shipping' );

		return $bulk_array;
	}

	public static function add_admin_scripts() {
		wp_enqueue_style(
			'jquery-datetimepicker',
			URL . '/assets/css/jquery-ui-timepicker-addon.css',
			[],
			'1.6.3',
		);

		wp_enqueue_script(
			'jquery-datetimepicker',
			URL . '/assets/js/libs/jquery-ui-timepicker-addon.js',
			[ 'jquery', 'jquery-ui-datepicker', 'jquery-ui-slider' ],
			'1.6.3',
		);

		wp_enqueue_style(
			'select2css',
			URL . '/assets/css/select2.css',
			[],
			'4.0.13'
		);

		wp_enqueue_script(
			'select2',
			URL . '/assets/js/libs/select2.min.js',
			[ 'jquery' ],
			'4.0.13',
			false
		);

		wp_enqueue_script(
			'wetail-shipping',
			URL . '/assets/js/admin.js',
			[ 'jquery', 'jquery-datetimepicker', 'jquery-ui-tooltip', 'select2' ],
			VERSION . '-' . filemtime( PATH . '/assets/js/admin.js' ),
			true
		);

		$create_labels_manually                            = get_option( 'wetail_shipping_create_labels', 'auto' ) === 'manually';
		$book_pickup_manually                              = wc_string_to_bool( get_option( 'wetail_shipping_book_pickup_manually', 'no' ) );
		$has_multiple_senders                              = wc_string_to_bool( get_option( 'wetail_shipping_has_multiple_senders', 'no' ) );
		$enable_manual_shipping_service_selection_override = wc_string_to_bool( get_option( 'wetail_shipping_enable_manual_shipping_service_selection_override', 'no' ) );

		wp_localize_script( 'wetail-shipping', '_wetail_shipping', [
			'i18n'                        => [
				'Update & print'                                                                              => esc_html__( 'Update & print', 'wetail-shipping' ),
				'Product'                                                                                     => esc_html__( 'Product', 'wetail-shipping' ),
				'Weight'                                                                                      => esc_html__( 'Weight', 'wetail-shipping' ),
				'Total order weight'                                                                          => esc_html__( 'Total order weight', 'wetail-shipping' ),
				'Overwrite the total weight of the order, if necessary'                                       => esc_html__( 'Overwrite the total weight of the order, if necessary', 'wetail-shipping' ),
				'Quantity'                                                                                    => esc_html__( 'Quantity', 'wetail-shipping' ),
				'Add row'                                                                                     => esc_html__( 'Add row', 'wetail-shipping' ),
				'Enter the ID of the product'                                                                 => esc_html__( 'Enter the ID of the product', 'wetail-shipping' ),
				'Enter the name of the product'                                                               => esc_html__( 'Enter the name of the product', 'wetail-shipping' ),
				'Enter the quantity of the product'                                                           => esc_html__( 'Enter the quantity of the product', 'wetail-shipping' ),
				'The ID of the product is empty. Please try again.'                                           => esc_html__( 'The ID of the product is empty. Please try again.', 'wetail-shipping' ),
				'The name of the product is empty. Please try again.'                                         => esc_html__( 'The name of the product is empty. Please try again.', 'wetail-shipping' ),
				'The quantity of the product is empty. Please try again.'                                     => esc_html__( 'The quantity of the product is empty. Please try again.', 'wetail-shipping' ),
				'The quantity of the product should be a number. Please try again.'                           => esc_html__( 'The quantity of the product should be a number. Please try again.', 'wetail-shipping' ),
				'Please select a return service provided by your parcel delivery company for the order'       => esc_html__( 'Please select a return service provided by your parcel delivery company for the order', 'wetail-shipping' ),
				'Print'                                                                                       => esc_html__( 'Print', 'wetail-shipping' ),
				'Send to client'                                                                              => esc_html__( 'Send to client', 'wetail-shipping' ),
				'OK'                                                                                          => esc_html__( 'OK', 'wetail-shipping' ),
				'Cancel'                                                                                      => esc_html__( 'Cancel', 'wetail-shipping' ),
				'Confirm'                                                                                     => esc_html__( 'Confirm', 'wetail-shipping' ),
				'Continue'                                                                                    => esc_html__( 'Continue', 'wetail-shipping' ),
				'Your order can not be processed. Please enter the weight and/or dimensions of your package.' => esc_html__( 'Your order can not be processed. Please enter the weight and/or dimensions of your package.', 'wetail-shipping' ),
				'Missing account connection'                                                                  => esc_html__( 'Missing account connection', 'wetail-shipping' ),
				'Missing order info'                                                                          => esc_html__( 'Missing order info', 'wetail-shipping' ),
				'Order'                                                                                       => esc_html__( 'Order', 'wetail-shipping' ),
				'Select sender & pickup'                                                                      => esc_html__( 'Select sender & pickup', 'wetail-shipping' ),
				'Please'                                                                                      => esc_html__( 'Please', 'wetail-shipping' ),
				'select a shipping sender'                                                                    => esc_html__( 'select a shipping sender', 'wetail-shipping' ),
				'Sender'                                                                                      => esc_html__( 'Sender', 'wetail-shipping' ),
				'Choose sender'                                                                               => esc_html__( 'Choose sender', 'wetail-shipping' ),
				'The order has no matching carrier mapping'                                                   => esc_html__( 'The order has no matching carrier mapping', 'wetail-shipping' ),
				'Carrier'                                                                                     => esc_html__( 'Carrier', 'wetail-shipping' ),
				'Select carrier'                                                                              => esc_html__( 'Select carrier', 'wetail-shipping' ),
				'Use mapping'                                                                                 => esc_html__( 'Use mapping', 'wetail-shipping' ),
				'Choose mapping below'                                                                        => esc_html__( 'Choose mapping below', 'wetail-shipping' ),
				'or'                                                                                          => esc_html__( 'or', 'wetail-shipping' ),
				'edit order details'                                                                          => esc_html__( 'edit order details', 'wetail-shipping' ),
				'and/or'                                                                                      => esc_html__( 'and/or', 'wetail-shipping' ),
				'schedule pickup'                                                                             => esc_html__( 'schedule pickup', 'wetail-shipping' ),
				'Schedule pickup'                                                                             => esc_html__( 'Schedule pickup', 'wetail-shipping' ),
				'Choose date'                                                                                 => esc_html__( 'Choose date', 'wetail-shipping' ),
				'Time is approximate, please contact carrier for information'                                 => esc_html__( 'Time is approximate, please contact carrier for information', 'wetail-shipping' ),
				'Select return service'                                                                       => esc_html__( 'Select return service', 'wetail-shipping' ),
				'Print shipping label'                                                                        => esc_html__( 'Print shipping label', 'wetail-shipping' ),
				'Print label'                                                                                 => esc_html__( 'Print label', 'wetail-shipping' ),
				'View label'                                                                                  => esc_html__( 'View label', 'wetail-shipping' ),
				'Return'                                                                                      => esc_html__( 'Return', 'wetail-shipping' ),
			],
			'settings'                    => [
				'create_labels_manually'                            => $create_labels_manually,
				'enable_manual_shipping_service_selection_override' => $enable_manual_shipping_service_selection_override,
				'has_multiple_senders'                              => $has_multiple_senders,
				'book_pickup_manually'                              => $book_pickup_manually,
				'license_key_is_set'                                => ! empty( get_option( 'wetail_shipping_api_key' ) ),
			],
			'wetail_shipping_nonce'       => wp_create_nonce( 'wetail_shipping_admin_nonce' ),
			'error_code'                  => '',
			/**
			 * Moved hardcoded methods to backend
			 * @since 1.0.6
			 * @clickup https://app.clickup.com/t/8696gbw5w
			 */
			'return_service_options_data' => self::get_return_service_options_data(),
		] );

		wp_enqueue_style(
			'wetail-shipping',
			URL . '/assets/css/style.css',
			[],
			VERSION . '-' . filemtime( PATH . '/assets/css/style.css' )
		);

		wp_enqueue_style(
			'wetail-shipping-icons',
			URL . '/assets/fonts/wetail-icons/styles.css',
			[],
			VERSION . '-' . filemtime( PATH . '/assets/fonts/wetail-icons/styles.css' )
		);
	}

	/**
	 * Add columns
	 *
	 * @param array $columns
	 */
	static public function add_columns( $columns = [] ) {
		$columns[ 'wetail_shipping_engine' ] = 'Wetail Shipping';

		return $columns;
	}

	/**
	 * Get column content
	 *
	 * @param string $column
	 * @param integer|WC_Order $order_id
	 */
	static public function get_column_content( $column, $order_id ) {
		if ( is_a( $order_id, 'WC_Order' ) ) {
			$order_id = $order_id->get_id(); // HPOS.
		}

		switch ( $column ) {
			case 'wetail_shipping_engine':
				$order                        = wc_get_order( $order_id );
				$wetail_shipping_has_error    = wc_string_to_bool( $order->get_meta( 'wetail_shipping_error' ) );
				$is_fulfilment_button_visible = self::is_fullfilment_button_visible( $order_id );

				echo '<div style="display: flex; align-items: center; flex-wrap: nowrap;">';

				/**
				 * Create fulfillment button
				 */
				self::render_button( array(
					'order_id'   => $order_id,
					'css_class'  => 'createFulfillment',
					'title'      => esc_html__( 'Create label', 'wetail-shipping' ),
					'icon'       => self::render_icon( 'printShippingLabel' ),
					'type' => 'printShippingLabel',
					'is_visible' => $is_fulfilment_button_visible,
					'text'       => esc_attr__( 'Create fulfillment', 'wetail-shipping' ),
				) );

				/**
				 * Print Existing shipping label button
				 */
				self::render_button( array(
					'order_id'   => $order_id,
					'css_class'  => 'printExistingShippingLabel',
					'type' => 'printExistingShippingLabel',
					'title'      => esc_html__( 'Print label', 'wetail-shipping' ),
					'icon'       => self::render_icon( 'printExistingShippingLabel' ) . '<span class="success-circle"></span>',
					'is_visible' => ! $is_fulfilment_button_visible
				) );

				/**
				 * Print shipping label button
				 */
				self::render_button( array(
					'order_id'   => $order_id,
					'css_class'  => 'printShippingLabel',
					'type'       => 'printShippingLabel',
					'title'      => esc_html__( 'Create label', 'wetail-shipping' ),
					'icon'       => self::render_icon( 'printShippingLabel' ),
					'is_visible' => ! $is_fulfilment_button_visible
				) );

				/**
				 * Print return label button
				 */
				self::render_button( array(
					'order_id'   => $order_id,
					'css_class'  => 'printReturnLabel',
					'title'      => esc_html__( 'Return', 'wetail-shipping' ),
					'type'       => 'printReturnLabel',
					'icon'       => self::render_icon( 'printReturnLabel' ),
					'is_visible' => ! $is_fulfilment_button_visible
				) );

				/**
				 * Error button
				 */
				self::render_button( array(
					'order_id'   => $order_id,
					'css_class'  => 'errorLabel',
					'title'      => esc_html__( 'Creation of shipping label failed', 'wetail-shipping' ),
					'icon'       => self::render_icon( 'error' ),
					'is_visible' => ! $is_fulfilment_button_visible && $wetail_shipping_has_error
				) );

				self::render_spinner();

				echo '</div>';

				break;
		}
	}

	static public function render_icon($type)
	{
		$icons = [

			'printExistingShippingLabel' => '<svg width="18" height="18" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path fill-rule="evenodd" clip-rule="evenodd" d="M5.07687 0C4.8325 0 4.63048 0.190938 4.61613 0.435471L4.28875 6.0143H2.30769C2.12361 6.0143 1.95713 6.12395 1.88407 6.29331L0.0379137 10.5727C0.0109582 10.6352 -0.00149755 10.7018 0.000143141 10.7678L0 10.7795V16.7938C0 17.0493 0.206638 17.2564 0.461538 17.2564H17.5385C17.7934 17.2564 18 17.0493 18 16.7938V10.7795L17.9999 10.7679C18.0015 10.7018 17.9891 10.6352 17.9621 10.5727L16.1159 6.29331C16.0429 6.12395 15.8764 6.0143 15.6923 6.0143H13.7112L13.3838 0.435471C13.3694 0.190938 13.1674 0 12.923 0H5.07687ZM13.7655 6.93958L13.8198 7.86485H14.5384C14.7933 7.86485 14.9999 8.07198 14.9999 8.32749C14.9999 8.583 14.7933 8.79013 14.5384 8.79013H13.3846H4.61534H3.23069C2.97579 8.79013 2.76915 8.583 2.76915 8.32749C2.76915 8.07198 2.97579 7.86485 3.23069 7.86485H4.18015L4.23445 6.93958H2.61095L1.16397 10.2937H16.836L15.389 6.93958H13.7655ZM12.4878 0.925277L12.7593 5.55167H5.24057L5.51206 0.925277H12.4878ZM12.8951 7.86485L12.7865 6.0143H5.21342L5.10482 7.86485H12.8951ZM0.923077 16.3311V11.2421H17.0769V16.3311H0.923077ZM8.53849 2.31319C8.53849 2.18544 8.64181 2.08187 8.76926 2.08187L11.7693 2.08187C11.8967 2.08187 12 2.18544 12 2.31319C12 2.44094 11.8967 2.54451 11.7693 2.54451L8.76926 2.54451C8.64181 2.54451 8.53849 2.44094 8.53849 2.31319ZM6.46152 2.08187C6.33407 2.08187 6.23076 2.18544 6.23076 2.31319C6.23076 2.44094 6.33407 2.54451 6.46152 2.54451H7.84614C7.97359 2.54451 8.07691 2.44094 8.07691 2.31319C8.07691 2.18544 7.97359 2.08187 7.84614 2.08187H6.46152ZM8.53849 3.23847C8.53849 3.11071 8.64181 3.00715 8.76926 3.00715L11.7693 3.00715C11.8967 3.00715 12 3.11071 12 3.23847C12 3.36622 11.8967 3.46979 11.7693 3.46979L8.76926 3.46979C8.64181 3.46979 8.53849 3.36622 8.53849 3.23847ZM8.76926 3.93243C8.64181 3.93243 8.53849 4.03599 8.53849 4.16375C8.53849 4.2915 8.64181 4.39507 8.76926 4.39507H11.7693C11.8967 4.39507 12 4.2915 12 4.16375C12 4.03599 11.8967 3.93243 11.7693 3.93243L8.76926 3.93243ZM2.76915 13.4165C3.02405 13.4165 3.23069 13.2094 3.23069 12.9539C3.23069 12.6984 3.02405 12.4912 2.76915 12.4912C2.51425 12.4912 2.30761 12.6984 2.30761 12.9539C2.30761 13.2094 2.51425 13.4165 2.76915 13.4165Z" fill="#2270B1"/>
        </svg>',

			'printReturnLabel' => '<svg width="21" height="14" viewBox="0 0 21 14" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M16.1936 2.95888C16.131 2.95888 16.0685 2.96144 16.0059 2.964L14.6823 0.257127C14.6085 0.104898 14.4571 0.0102339 14.2908 0.0102339H8.93879C8.90876 0.00383772 8.87874 0 8.84621 0C8.81369 0 8.78366 0.00383772 8.75364 0.0102339H3.38166C3.21527 0.0102339 3.06389 0.106177 2.99008 0.257127L0.0463803 6.27467C0.0463803 6.27467 0.0438782 6.28107 0.0426272 6.28491C0.0288657 6.31561 0.0176063 6.34631 0.0101001 6.37957V6.38213C0.00384486 6.41155 0.00134277 6.44225 0.00134277 6.47423V13.551C0.00134277 13.7979 0.197756 13.9987 0.439208 13.9987H17.2345C17.4759 13.9987 17.6723 13.7979 17.6723 13.551V12.5506C19.6014 11.911 21.0013 10.0574 21.0013 7.87372C21.0013 5.16301 18.8445 2.9576 16.1936 2.9576V2.95888ZM9.28408 0.905702H14.0193L15.0889 3.09192C13.5651 3.46034 12.3204 4.56944 11.7399 6.02778H9.28408V0.905702ZM3.65314 0.905702H8.40835V6.02778H1.14605L3.65188 0.905702H3.65314ZM16.7966 13.1045H0.875821V6.92325H11.4772C11.4184 7.23154 11.3858 7.55007 11.3858 7.875C11.3858 10.5857 13.5426 12.7911 16.1936 12.7911C16.3975 12.7911 16.5989 12.777 16.7953 12.7515V13.1045H16.7966ZM17.6723 11.6001C17.3946 11.7153 17.1018 11.801 16.7966 11.8483C16.6002 11.879 16.3988 11.8957 16.1948 11.8957C14.0268 11.8957 12.2628 10.0919 12.2628 7.875C12.2628 7.54751 12.3028 7.22771 12.3754 6.92325C12.4505 6.60855 12.5618 6.30921 12.7044 6.02778C13.2561 4.9417 14.2795 4.14474 15.4943 3.92087C15.7219 3.87865 15.9546 3.85563 16.1936 3.85563C16.2787 3.85563 16.3625 3.85947 16.4463 3.86458C18.4968 3.99762 20.1256 5.74507 20.1256 7.87628C20.1256 9.55848 19.1098 11.0015 17.6723 11.6001Z" fill="#2270B1"/>
            <path d="M17.1694 3.31963C17.0317 3.31963 16.9204 3.43348 16.9204 3.57419C16.9204 3.71491 17.0317 3.82876 17.1694 3.82876C17.9187 3.82876 18.528 4.52339 18.528 5.37664C18.528 6.2299 17.9187 6.92452 17.1694 6.92452H16.395L16.6302 6.1493C16.6602 6.04952 16.6277 5.94079 16.5476 5.87555C16.4675 5.81031 16.3562 5.80135 16.2686 5.8538L13.1485 7.70102C13.0722 7.74707 13.0247 7.83022 13.0247 7.92105C13.0247 8.01188 13.0722 8.09503 13.1485 8.14108L16.2736 9.98063C16.3124 10.0037 16.3549 10.0139 16.3975 10.0139C16.4525 10.0139 16.5076 9.9947 16.5526 9.9576C16.6327 9.89236 16.6652 9.78362 16.6339 9.68384L16.3712 8.82547H17.1719C18.6569 8.82547 19.8641 7.59101 19.8641 6.07255C19.8641 4.55409 18.6569 3.31963 17.1681 3.31963H17.1694Z" fill="#2270B1"/>
        </svg>',

			'error' => '<svg xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 19 19" fill="none">
            <circle cx="9.5" cy="9.5" r="9.5" fill="#FC7B7B"/>
            <path d="M9.18494 11.73C8.92894 11.73 8.71027 11.6447 8.52894 11.474C8.34761 11.2927 8.25161 11.074 8.24094 10.818L8.00094 4.48201C7.99027 4.23667 8.07027 4.02334 8.24094 3.84201C8.41161 3.66067 8.61961 3.57001 8.86494 3.57001H9.72894C9.97427 3.57001 10.1823 3.66067 10.3529 3.84201C10.5236 4.02334 10.6036 4.23667 10.5929 4.48201L10.3529 10.818C10.3423 11.074 10.2463 11.2927 10.0649 11.474C9.88361 11.6447 9.66494 11.73 9.40894 11.73H9.18494ZM9.56894 13.01C9.81427 13.01 10.0223 13.1007 10.1929 13.282C10.3743 13.4633 10.4649 13.6767 10.4649 13.922V14.338C10.4649 14.5833 10.3743 14.7967 10.1929 14.978C10.0223 15.1593 9.81427 15.25 9.56894 15.25H9.02494C8.77961 15.25 8.56627 15.1593 8.38494 14.978C8.21427 14.7967 8.12894 14.5833 8.12894 14.338V13.922C8.12894 13.6767 8.21427 13.4633 8.38494 13.282C8.56627 13.1007 8.77961 13.01 9.02494 13.01H9.56894Z" fill="white"/>
        </svg>',

			'default' => '<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 15 15" fill="none">
            <path fill-rule="evenodd" clip-rule="evenodd" d="M3.00024 1.07143C3.00024 0.479695 3.47994 0 4.07167 0H13.3574C13.9491 0 14.4288 0.479695 14.4288 1.07143V13.9286C14.4288 14.5203 13.9491 15 13.3574 15H4.07167C3.47994 15 3.00024 14.5203 3.00024 13.9286V1.07143ZM4.07167 0.714286C3.87443 0.714286 3.71453 0.874184 3.71453 1.07143V13.9286C3.71453 14.1258 3.87443 14.2857 4.07167 14.2857H13.3574C13.5546 14.2857 13.7145 14.1258 13.7145 13.9286V1.07143C13.7145 0.874184 13.5546 0.714286 13.3574 0.714286H4.07167Z" fill="#2270B1"/>
            <rect x="4.78589" y="8.21429" width="0.714286" height="5" rx="0.357143" fill="#2270B1"/>
            <rect x="4.78589" y="1.78571" width="2.14286" height="0.714286" rx="0.357143" fill="#2270B1"/>
            <rect x="7.64294" y="1.78571" width="5" height="0.714286" rx="0.357143" fill="#2270B1"/>
            <rect x="7.64294" y="3.21429" width="5" height="0.714286" rx="0.357143" fill="#2270B1"/>
            <rect x="7.64294" y="4.64285" width="5" height="0.714286" rx="0.357143" fill="#2270B1"/>
            <rect x="3.35718" y="6.78571" width="10.7143" height="0.714286" fill="#2270B1"/>
            <rect x="6.21436" y="8.21429" width="0.714286" height="5" rx="0.357143" fill="#2270B1"/>
            <rect x="7.64294" y="8.21429" width="0.714286" height="5" rx="0.357143" fill="#2270B1"/>
            <rect x="11.9285" y="8.21429" width="0.714286" height="5" rx="0.357143" fill="#2270B1"/>
            <rect x="9.07141" y="8.21429" width="2.14286" height="5" rx="0.5" fill="#2270B1"/>
            <path d="M6.29985 6.14999C6.29985 7.724 5.02386 8.99999 3.44985 8.99999C1.87584 8.99999 0.599854 7.724 0.599854 6.14999C0.599854 4.57598 1.87584 3.29999 3.44985 3.29999C5.02386 3.29999 6.29985 4.57598 6.29985 6.14999Z" fill="white"/>
            <path fill-rule="evenodd" clip-rule="evenodd" d="M6.9 6.15001C6.9 8.05539 5.35538 9.60001 3.45 9.60001C1.54462 9.60001 0 8.05539 0 6.15001C0 4.24463 1.54462 2.70001 3.45 2.70001C5.35538 2.70001 6.9 4.24463 6.9 6.15001ZM3.45 9.00001C5.02401 9.00001 6.3 7.72402 6.3 6.15001C6.3 4.576 5.02401 3.30001 3.45 3.30001C1.87599 3.30001 0.6 4.576 0.6 6.15001C0.6 7.72402 1.87599 9.00001 3.45 9.00001Z" fill="#2270B1"/>
            <path d="M3.86162 7.38509C3.86162 7.61424 3.67586 7.8 3.44671 7.8C3.21756 7.8 3.0318 7.61424 3.0318 7.38509V6.57456H2.22127C1.98857 6.57456 1.79993 6.38592 1.79993 6.15322C1.79993 5.92051 1.98857 5.73187 2.22127 5.73187H3.0318V4.91491C3.0318 4.68576 3.21756 4.5 3.44671 4.5C3.67586 4.5 3.86162 4.68576 3.86162 4.91491V5.73187H4.67858C4.91128 5.73187 5.09993 5.92051 5.09993 6.15322C5.09993 6.38592 4.91128 6.57456 4.67858 6.57456H3.86162V7.38509Z" fill="#2270B1"/>
        </svg>',

		];

		switch ($type) {
			case 'printExistingShippingLabel':
				return $icons['printExistingShippingLabel'];

			case 'printReturnLabel':
				return $icons['printReturnLabel'];

			case 'error':
				return $icons['error'];

			default:
				return $icons['default'];
		}
	}

	static public function render_button( $args ) {
		$order_id   = $args[ 'order_id' ];
		$css_class  = $args[ 'css_class' ];
		$title      = $args[ 'title' ];
		$text       = $args[ 'text' ] ?? null;
		$icon       = $args[ 'icon' ] ?? null;
		$type       = $args[ 'type' ] ?? '';
		$is_visible = $args[ 'is_visible' ] ?? false;

		if ( $text ) {
			$css_class .= ' wetail-shipping-engine-button__with-text';
		}

		$format              = '<a href="#" class="button button-secondary wetail-shipping-engine-button wetail-shipping-engine-button__width-unset %1$s" ';
		$format              .= 'data-order-id="%2$s" ';
		$format              .= 'data-shipping-service-id="%3$s" ';
		$format              .= 'data-nonce="" ';
		$format              .= 'data-type="%5$s" ';
		$format              .= 'title="%4$s">';
		$format              .= $icon . $text . '</a>';
		$css_class           = $css_class . ( $is_visible ? ' wetail-shipping-engine-button--visible' : '' );
		$shipping_service_id = '';

		$html         = wp_unslash( sprintf( $format, $css_class, $order_id, $shipping_service_id, $title, $type ) );
		$allowed_html = array(
			'a'      => array(
				'href'                     => true,
				'title'                    => true,
				'class'                    => true,
				'data-order-id'            => true,
				'data-shipping-service-id' => true,
				'data-type'                => true,
				'data-nonce'               => true,
			),
			'svg'    => array(
				'width'   => true,
				'height'  => true,
				'viewBox' => true,
				'fill'    => true,
				'xmlns'   => true,
			),
			'path'   => array(
				'fill-rule' => true,
				'clip-rule' => true,
				'd'         => true,
				'fill'      => true,
			),
			'rect'   => array(
				'x'      => true,
				'y'      => true,
				'width'  => true,
				'height' => true,
				'rx'     => true,
				'fill'   => true,
			),
			'circle' => array(
				'cx'   => true,
				'cy'   => true,
				'r'    => true,
				'fill' => true,
			),
			'span'   => array(
				'class' => true,
			)
		);

		echo wp_kses( $html, $allowed_html );
	}

	static public function is_fullfilment_button_visible( $order_id ) {
		$order                              = wc_get_order( $order_id );
		$wetail_shipping_has_error          = wc_string_to_bool( $order->get_meta( 'wetail_shipping_error' ) );
		$wetail_shipping_label_generated    = wc_string_to_bool( $order->get_meta( Wetail_Shipping_Order_Controller::WETAIL_SHIPPING_SHIPPING_LABEL_GENERATED ) );
		$wetail_shipping_label_printed      = wc_string_to_bool( $order->get_meta( Wetail_Shipping_Order_Controller::WETAIL_SHIPPING_SHIPPING_LABEL_PRINTED ) );

		if ( ! $wetail_shipping_label_generated ) {
			$wetail_shipping_label_generated = $order->get_meta( Wetail_Shipping_Order_Controller::WETAIL_SHIPPING_SHIPPING_LABEL_DEPRECATED );
		}

		return  ! $wetail_shipping_has_error && ! $wetail_shipping_label_generated && ! $wetail_shipping_label_printed;
	}

	static private function render_spinner() {
		echo '<div class="spinner" style="display: none"></div>';
	}


	static public function add_js_templates() {
		include_once PATH . '/templates/update-product-dimensions.php';
		include_once PATH . '/templates/no-license.php';
	}

	/**
	 * Retrieves return service options data.
	 *
	 * @return array Filtered array of return service options, each containing 'name'
	 *               and 'service_id' keys.
	 * @since 1.0.6
	 * @clickup https://app.clickup.com/t/8696gbw5w
	 */
	public static function get_return_service_options_data() {
		return apply_filters( 'wetail_shipping_service_return_service_options', array(
			array(
				'name'       => 'Postnord Return Pickup',
				'service_id' => 'postnord_return_pickup_20',
			),
			array(
				'name'       => 'Postnord Return DropOff',
				'service_id' => 'postnord_return_dropoff_24',
			),
			array(
				'name'       => 'DHL Return Connect',
				'service_id' => 'dhl_return_connect_107',
			),
			array(
				'name'       => 'DHL Home Delivery Return',
				'service_id' => 'dhl_home_delivery_return_402',
			),
			array(
				'name'       => 'DHL Service Point C2B',
				'service_id' => 'dhl_service_point_c2b_104',
			),
			array(
				'name'       => 'DB Schenker retur via ombud',
				'service_id' => 'db_schenker_parcel_retur_via_ombud_REP',
			),
			array(
				'name'       => 'Bring Pickup Parcel Retur',
				'service_id' => 'bring_pickup_parcel_return',
			)
		) );
	}
}
