<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @var int $order_id
 * @var string $shipping_method
 * @var array $api_response
 */

$order = wc_get_order( $order_id );
if ( ! $order || is_wp_error( $order ) ) {
	return;
}

$helper = \Ilabs\Inpost_International\Helpers\Helper::init();

$disabled              = false;
$package_created       = false;
$package_data          = array();
$inpost_intl_locker_id = '';
$status_desc           = '';
$status_title          = '';
$status_message        = '';
$status                = null;
$tracking_number       = '';
$uuid                  = '';

$package_meta = $order->get_meta( 'inpost_international_package' );
if ( ! empty( $package_meta ) ) {
	try {
		$package_data = json_decode( $package_meta, true );
		$package_data = ! empty( $package_data['api_response'] ) ? $package_data : array();
	} catch ( Exception $e ) {
		$package_data = array();
	}
}

if ( ! empty( $api_response ) ) {
	$package_created = $api_response;
	$disabled        = true;
} elseif ( empty( $api_response ) && ! empty( $package_data ) && is_array( $package_data ) ) {
	$package_created = $package_data;
	$disabled        = true;
}



if ( ! empty( $package_created['creating_args']['shipment']['destination']['pointName'] ) ) {
	$inpost_intl_locker_id = $package_created['creating_args']['shipment']['destination']['pointName'];
} elseif ( ! empty( $order->get_meta( 'inpost_international_locker_id' ) ) ) {
	$inpost_intl_locker_id = $order->get_meta( 'inpost_international_locker_id' );
}


if ( ! empty( $package_created['api_response']['trackingNumber'] ) || ! empty( $package_created['trackingNumber'] ) ) {
	$tracking_number = ! empty( $package_created['api_response']['trackingNumber'] )
		? $package_created['api_response']['trackingNumber']
		: $package_created['trackingNumber'];
}


if ( ! empty( $package_created['api_response']['uuid'] ) || ! empty( $package_created['uuid'] ) ) {
	$uuid = ! empty( $package_created['api_response']['uuid'] )
		? $package_created['api_response']['uuid']
		: $package_created['uuid'];
}

if ( empty( $tracking_number ) ) {
	$status_message = esc_html__( 'Not created yet', 'inpost-international' );
} elseif ( ! empty( $package_created['api_response']['status'] ) || ! empty( $package_created['status'] ) ) {
		$status_message = ! empty( $package_created['api_response']['status'] )
			? $package_created['api_response']['status']
			: $package_created['status'];
}

$class             = array( 'wc-enhanced-select' );
$custom_attributes = array( 'style' => 'width:100%;' );
if ( $disabled ) {
	$custom_attributes['disabled'] = 'disabled';
	$class[]                       = 'inpost_intl_disabled';
}

$animation      = inpost_intl_core()->get_plugin_images_url() . '/animation-stripe.gif';
$animation_2    = inpost_intl_core()->get_plugin_images_url() . '/animation-round-small.gif';
$animation_html = '<img style="height:20px; display:none;" src="' . esc_url( $animation ) . '"/>';

?>
<?php if ( ! empty( $inpost_intl_locker_id ) ) { ?>
	<p>
		<label for="inpost_intl_locker_id">
			<?php esc_html_e( 'Selected parcel locker', 'inpost-international' ); ?>
		</label>
		<input value="<?php echo esc_attr( $inpost_intl_locker_id ); ?>"
			type="text"
			class="inpost_intl_order_metabox_locker_id" id="inpost_intl_order_metabox_locker_id"
			name="inpost_intl_order_metabox_locker_id"
			<?php echo $disabled ? ' disabled ' : ''; ?>
		>
	</p>
<?php } ?>

<p>
	<span style="font-weight: bold">
		<?php esc_html_e( 'Status:', 'inpost-international' ); ?>
	</span>
	<span class="inpost_intl_status" title="<?php echo esc_attr( $status_message ); ?>">
		<?php echo esc_html( $status_message ); ?>

	</span>
</p>

<?php if ( ! empty( $tracking_number ) ) { ?>
<p>
	<span style="font-weight: bold">
		<?php echo esc_html__( 'Tracking number:', 'inpost-international' ); ?>
	</span>
	<a id="inpost_intl_metabox_tracking_number" data-id="<?php echo esc_attr( $tracking_number ); ?>" href="#">
		<?php echo esc_html( $tracking_number ); ?>
	</a>
	<span id="inpost_intl_order_metabox_tracking_preloader">
		<?php echo '<img class="inpost_intl_preloader" src="' . esc_url( $animation_2 ) . '" alt="inpost international preloader"/>'; ?>
	</span>
	<span id="inpost_intl_status_error"></span>
</p>
<?php } ?>

<p>
	<?php echo esc_html__( 'Templates:', 'inpost-international' ); ?>
</p>

<?php
inpost_intl_core()->locate_template(
	'order-metabox-dimensions.php',
	array(
		'order_id'        => $order_id,
		'disabled'        => $disabled,
		'package_created' => $package_created,
	)
);
?>


<?php
inpost_intl_core()->locate_template(
	'order-metabox-insurance.php',
	array(
		'order_id'        => $order_id,
		'disabled'        => $disabled,
		'package_created' => $package_created,
	)
);
?>
<p>
	<?php

    $country_code = 'PL';
	$sender_options = get_option( 'inpost_international_settings_merchant' );
	if ( ! empty( $sender_options['country'] ) ) {
		$country_code = $sender_options['country'];
	}
    $send_options = $helper->get_package_send_method_options( $shipping_method, $country_code );

	$params = array(
		'type'              => 'select',
		'options'           => $send_options,
		'class'             => array( 'wc-enhanced-select' ),
		'custom_attributes' => $custom_attributes,
		'label'             => esc_html__( 'Send method', 'inpost-international' ),
	);

	$saved_send_method = 'point-to-point';
	if ( $package_created ) {
		if ( ! empty( $package_created['creating_args']['shipment']['origin']['address'] ) ) {
			$saved_send_method = 'address-to-point';
		} else {
			$saved_send_method = 'point-to-point';
		}
	} else {
		$plugin_sending_settings = get_option( 'inpost_international_settings_sending' );
		if ( ! empty( $plugin_sending_settings['default_send_method'] ) ) {

			$saved_send_method = $helper->validate_send_method_option( $shipping_method, $plugin_sending_settings['default_send_method'] );
		}
	}

	woocommerce_form_field( 'inpost_intl_send_method', $params, $saved_send_method );

	?>
</p>
<?php
inpost_intl_core()->locate_template(
	'order-metabox-reference-field.php',
	array(
		'order_id'        => $order_id,
		'disabled'        => $disabled,
		'package_created' => $package_created,
	)
);
?>



<p>

	<?php if ( empty( $package_data ) ) { ?>
		<button id="inpost_intl_create_package_order"
				class="button button-primary">
			<?php echo esc_html__( 'Send parcel', 'inpost-international' ); ?>
		</button>
	<?php } else { ?>

		<?php if ( ! empty( $tracking_number ) ) { ?>
			<button id="inpost_intl_get_label_order"
					class="button button-secondary">
				<?php echo esc_html__( 'Get label', 'inpost-international' ); ?>
			</button>
		<?php } ?>

	<?php } ?>

	<span id="inpost_intl_order_metabox_preloader">
		<?php echo '<img class="inpost_intl_preloader" src="' . esc_url( $animation ) . '" alt="inpost international preloader"/>'; ?>
	</span>
</p>

<p id="inpost_intl_error"></p>


<?php
inpost_intl_core()->locate_template( 'order-metabox-no-funds-alert.php', array( 'order_id' => $order_id ) );
?>
<input type="hidden" id="inpost_international_order_id" value="<?php echo esc_attr( $order_id ); ?>">
<input type="hidden" id="inpost_international_uuid" value="<?php echo esc_attr( $uuid ); ?>">
