<?php

namespace Inspire_Labs\Alsendo_Connect;

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

/**
 * FSHooks
 */
class FSHooks {

	/**
	 * Instance
	 */
	public static $instance = null;

	/**
	 * Shipping_methods
	 */
	private $fs_shipping_methods = null;

	/**
	 * Constructor
	 */
	private function __construct() {
		add_filter(
			'flexible_shipping_integration_options',
			array( $this, 'integration_options' )
		);
		add_filter(
			'flexible_shipping_method_integration_col',
			array( $this, 'method_integration_col' ),
			10,
			2
		);
	}


	/**
	 * Get instance
	 *
	 * @return self
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Get shipping methods
	 *
	 * @return array
	 */
	private function fs_get_shipping_methods() {
		if ( null === $this->fs_shipping_methods ) {
			$this->fs_shipping_methods = WC()->shipping->get_shipping_methods();
		}

		return $this->fs_shipping_methods;
	}

	/**
	 * Integration options
	 *
	 * @param array $options $options.
	 *
	 * @return array
	 */
	function integration_options( $options ) {
		$shipping_methods = $this->fs_get_shipping_methods();
		if ( $shipping_methods['alsendo_connect']->enabled === 'yes' ) {
			$options['alsendo_connect'] = esc_html__( 'Alsendo', 'alsendo-connect' );
		}

		return $options;
	}

	/**
	 * Integration column
	 *
	 * @param string $col $col.
	 * @param string $shipping_method $shipping_method.
	 *
	 * @return string
	 */
	public function method_integration_col( $col, $shipping_method ) {
		if ( isset( $shipping_method['method_integration'] ) && 'alsendo_connect' === $shipping_method['method_integration'] ) {
			$services = implode( ',', Shipping_Method_Alsendo_Connect::$services );

			ob_start();
			?>
			<td width="1%" class="integration default">
				<span class="tips" data-tip="<?php echo esc_attr( $services ); ?>">
					<?php echo esc_html( ucfirst( $shipping_method['method_integration'] ) ); ?>
				</span>
			</td>
			<?php
			$col = ob_get_contents();
			ob_end_clean();
		}

		return $col;
	}
}
