<?php

namespace Ilabs\Inpost_International\Integration\Flexible_Shipping;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly.


use Ilabs\Inpost_International\Helpers\Helper;

/**
 * Flexible_Shipping_Integration
 */
class Flexible_Shipping_Integration {


	/**
	 * Add custom fields to each shipping method.
	 */
	public function add_settings_to_flexible_shipping(): void {
		$helper = Helper::init();

		$shipping_methods = $helper->get_shipping_methods();
		foreach ( $shipping_methods as $shipping_method ) {
			if ( 'flexible_shipping_single' === $shipping_method->id ) {
				add_filter(
					'woocommerce_shipping_instance_form_fields_' . $shipping_method->id,
					array(
						$this,
						'add_map_field',
					)
				);
			}
		}
	}


	/**
	 * Add map field
	 *
	 * @param array $settings $settings.
	 *
	 * @return array
	 */
	public function add_map_field( $settings ): array {

		$has_integration = false;

		$find_key = '';
		$i        = 1;
		foreach ( $settings as $key => $setting ) {

			// divide fs settings array and put our settings before this field.
			if ( 'method_integration' === $key ) {
				$find_key        = $i;
				$has_integration = true;
			}
			++$i;
		}
		// show our field near with native integration field.
		if ( $has_integration ) {

			$position = (int) $find_key - 1;

			$settings_begin = array_slice( $settings, 0, $position, true );

			$addtitional_settings = $this->settings_block_for_flexible_shipping();

			$settings_end = array_slice( $settings, $position, count( $settings ) - $position, true );

			return array_merge( $settings_begin, $addtitional_settings, $settings_end );

		} else {

			return $settings + $this->settings_block_for_flexible_shipping();
		}
	}


	/**
	 * Settings for Flexible Shipping plugin
	 *
	 * @return array
	 */
	public function settings_block_for_flexible_shipping(): array {
		$settings = array();

		$settings['fs_inpost_international_method'] = array(
			'title'   => esc_html__( "Integration with 'InPost INTL' plugin", 'inpost-international' ),
			'type'    => 'select',
			'default' => 'all',
			'options' => array(
				'0' => esc_html__( 'None', 'inpost-international' ),
				'inpost_international_automated_locker_point' => esc_html__( 'InPost International', 'inpost-international' ),
			),
		);

		$settings['fs_insurance_inpost_intl'] = array(
			'title'    => esc_html__( 'Insurance', 'inpost-international' )
							. ' InPost international: '
							. esc_html__( 'set from order amount', 'inpost-international' ),
			'type'     => 'checkbox',
			'default'  => 'no',
			'desc_tip' => false,
			'class'    => 'fs-inpost-pl-insurance',
		);

		$settings['fs_insurance_value_inpost_intl'] = array(
			'title'             => esc_html__( 'Default insurance amount for InPost International', 'inpost-international' ),
			'type'              => 'number',
			'custom_attributes' => array(
				'step' => 'any',
				'min'  => '0',
			),
			'default'           => '0.00',
			'desc_tip'          => false,
			'class'             => 'fs-inpost-intl-insurance-value',
		);

		return $settings;
	}
}
