<?php

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

/**
 * @var int $order_id
 * @var bool $disabled
 * @var mixed $package_created
 */


$helper         = \Ilabs\Inpost_International\Helpers\Helper::init();
$input_disabled = '';
if ( $disabled ) {
	$input_disabled = 'disabled';
}
$length                       = '';
$width                        = '';
$height                       = '';
$weight                       = '';
$length_from_chosen_template  = '';
$width_from_chosen_template   = '';
$height_from_chosen_template  = '';
$weight_from_chosen_template  = '';
$sending_options['templates'] = array();

$saved_template = $helper->get_saved_template_by_product_meta( $order_id );

$select_options              = array(
	'default' => esc_html__( 'From product settings', 'inpost-international' ),
);
$get_dimensions_from_product = false;
$sending_options             = get_option( 'inpost_international_settings_sending' );
if ( ! empty( $sending_options['templates'] ) && is_array( $sending_options['templates'] ) ) {
	foreach ( $sending_options['templates'] as $key => $template ) {
		if ( ! empty( $template['name'] ) ) {
			$select_options[ $key ] = $template['name'];

			if ( $saved_template === $template['name'] ) {
				$saved_template              = $key;
				$length_from_chosen_template = ! empty( $template['length'] ) ? $template['length'] : '';
				$width_from_chosen_template  = ! empty( $template['width'] ) ? $template['width'] : '';
				$height_from_chosen_template = ! empty( $template['height'] ) ? $template['height'] : '';
				$weight_from_chosen_template = ! empty( $template['weight'] ) ? $template['weight'] : '';

			}
		}
	}
}


if ( 'default' === $saved_template || ! empty( $sending_options['product_template'] ) || empty( $sending_options['templates'] ) ) {
	$get_dimensions_from_product = true;
}


if ( $package_created && ! empty( $package_created['creating_args']['shipment']['parcel'] ) ) {

	$parcel_data                   = $package_created['creating_args']['shipment']['parcel'];
	$length                        = ! empty( $parcel_data['dimensions']['length'] ) ? $parcel_data['dimensions']['length'] : '';
	$width                         = ! empty( $parcel_data['dimensions']['width'] ) ? $parcel_data['dimensions']['width'] : '';
	$height                        = ! empty( $parcel_data['dimensions']['height'] ) ? $parcel_data['dimensions']['height'] : '';
	$weight                        = ! empty( $parcel_data['weight']['amount'] ) ? $parcel_data['weight']['amount'] : '';
	$saved_template_name_from_args = ! empty( $package_created['creating_args']['creating']['template'] ) ? $package_created['creating_args']['creating']['template'] : null;

	$saved_template = false;
	if ( 'default' === $saved_template_name_from_args ) {
		$saved_template = 'default';
	} elseif ( ! empty( $sending_options['templates'] ) && is_array( $sending_options['templates'] ) ) {
		foreach ( $sending_options['templates'] as $key => $template ) {
			if ( ! empty( $template['name'] ) ) {
				if ( $saved_template_name_from_args === $template['name'] ) {
					$saved_template = $key;
					break;
				}
			}
		}
	}

	if ( ! $saved_template ) {
		$select_options['custom'] = esc_html__( 'Custom', 'inpost-international' );
		$saved_template           = 'custom';
	}
} elseif ( $get_dimensions_from_product ) {

		$order_dimensions = $helper->get_order_dimensions_based_on_order( $order_id );
		$length           = ! empty( $order_dimensions['length'] ) ? $order_dimensions['length'] : 0;
		$width            = ! empty( $order_dimensions['width'] ) ? $order_dimensions['width'] : 0;
		$height           = ! empty( $order_dimensions['height'] ) ? $order_dimensions['height'] : 0;
		$weight           = ! empty( $order_dimensions['weight'] ) ? $order_dimensions['weight'] : 0;
		$saved_template   = 'default';
} else {
	$length = $length_from_chosen_template;
	$width  = $width_from_chosen_template;
	$height = $height_from_chosen_template;
	$weight = $weight_from_chosen_template;
}
// Select among saved templates.
$params = array(
	'type'              => 'select',
	'options'           => $select_options,
	'class'             => array( 'inpost_intl_package_dimensions' ),
	'input_class'       => array( 'inpost_intl_package_dimensions' ),
	'label'             => '',
	'custom_attributes' => array(
		'style' => 'background: #eee',
	),
);

woocommerce_form_field( 'inpost_intl_package_size_dimensions', $params, $saved_template );
?>

<p>
	<label style="display: block" for="insurance_amounts">
		<?php esc_html_e( 'Length:', 'inpost-international' ); ?>
	</label>
	<input <?php echo esc_attr( $input_disabled ); ?>
		class="inpost_intl_length inpost_intl_dimensions inpost_intl_validated_input"
		type="number"
		value="<?php echo esc_attr( $length ); ?>"
		placeholder="0.0"
		step="0.1"
		min="2.1"
		max="64"
		id="inpost_intl_length"
		name="inpost_intl_dimensions[length]"
	>
	<label style="display: block" for="insurance_amounts">
		<?php esc_html_e( 'Width:', 'inpost-international' ); ?>
	</label>
	<input <?php echo esc_attr( $input_disabled ); ?>
		class="inpost_intl_width inpost_intl_dimensions inpost_intl_validated_input"
		type="number"
		value="<?php echo esc_attr( $width ); ?>"
		placeholder="0.0"
		step="0.1"
		min="2.1"
		max="38"
		id="inpost_intl_width"
		name="inpost_intl_dimensions[width]"
	>
	<label style="display: block" for="insurance_amounts">
		<?php esc_html_e( 'Height:', 'inpost-international' ); ?>
	</label>
	<input <?php echo esc_attr( $input_disabled ); ?>
		class="inpost_intl_height inpost_intl_dimensions inpost_intl_validated_input"
		type="number"
		value="<?php echo esc_attr( $height ); ?>"
		placeholder="0.0"
		step="0.1"
		min="2.1"
		max="39"
		id="inpost_intl_height"
		name="inpost_intl_dimensions[height]"
	>
	<label style="display: block" for="insurance_amounts">
		<?php esc_html_e( 'Weight:', 'inpost-international' ); ?>
	</label>
	<input <?php echo esc_attr( $input_disabled ); ?>
		class="inpost_intl_weight inpost_intl_dimensions inpost_intl_validated_input"
		type="number"
		value="<?php echo esc_attr( $weight ); ?>"
		placeholder="0.0"
		step="0.1"
		min="0"
		max="25"
		id="inpost_intl_weight"
		name="inpost_intl_dimensions[weight]"
	>
	<input
		class="inpost_intl_used_template"
		type="hidden"
		value=""
		id="inpost_intl_used_template"
		name="inpost_intl_dimensions[used_template]"
	>
</p>
