<?php

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

/**
 * Class Wiaas_Admin_Linked_Packages
 */

class Wiaas_Admin_Linked_Packages {

	public static function init() {
		add_action( 'woocommerce_product_data_tabs', array( __CLASS__, 'package_data_tabs' ) );
		add_action( 'woocommerce_product_data_panels', array( __CLASS__, 'package_data_panel' ) );

		add_action( 'wp_ajax_wiaas_json_search_addons', array(__CLASS__, 'json_search_addons') );
		add_action( 'wp_ajax_wiaas_json_search_options', array(__CLASS__, 'json_search_options') );

		add_action( 'wp_ajax_wiaas_create_empty_option_group', array(__CLASS__, 'create_empty_option_group') );

		add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_meta_box' ));
	}

	/**
     * Redirects default woocommerce linked packages tab to wiaas linked packages tab
	 * @param $tabs
	 *
	 * @return array
	 */
	public static function package_data_tabs($tabs) {
		$tabs[ 'linked_product' ]['label'] = __( 'Linked Packages', 'wiaas' );
		$tabs[ 'linked_product' ]['target'] = 'wiaas_linked_packages';
		$tabs[ 'linked_product' ]['class'] = array('show_if_bundle', 'hide_if_simple');

		return $tabs;
	}

	/**
	 * Renders wiaas linked packages tab
	 */
	public static function package_data_panel() {

		global $post;

		$package           = wc_get_product( $post->ID );
		$addons = Wiaas_Package_Addon::get_package_addons($package);
		$option_groups = Wiaas_Package_Option_Groups::get_package_option_groups($package);

		include 'views/html-linked-packages.php';
	}

	/**
	 * Implements ajax search for package addons
	 */
	public static function json_search_addons() {
		self::_json_search_packages('add_on');
	}

	/**
	 * Implements ajax searc for package options
	 */
	public static function json_search_options() {

		self::_json_search_packages('option');
	}

	/**
	 * Creates and renders new optional packages group
	 */
	public static function create_empty_option_group() {
		$group = array(
			'name' => 'Untitled',
			'id' => uniqid('option_'),
			'default' => false,
			'options' => array()
		);

		?>
		<script type="text/javascript">
            $( document.body ).trigger( 'wc-enhanced-select-init' );
		</script>
		<?php
		$group_options = isset($group['options']) ? $group['options'] : array();
		$id = $group['id'];

		include 'views/html-package-options-group.php';
		?>
		<?php

		die();
	}

	/**
     * Saves posted linked packages data
     *
	 * @param $package_id
	 */
	public static function process_meta_box($package_id) {

		$package = wc_get_product($package_id);

		// Handle linked addons

		$addons_ids = isset( $_POST['wiaas_addon_packages'] ) ?
			array_filter( array_map( 'intval', (array) $_POST['wiaas_addon_packages'] ) ) :
			array();

		Wiaas_Package_Addon::set_package_addons(
			$package,
			$addons_ids);

		// Handle linked options

		$posted_option_groups = isset( $_POST['wiaas_option_groups'] ) ? $_POST['wiaas_option_groups'] : array();

		$option_groups = array();

		foreach ($posted_option_groups as $id => $posted_option_group) {
			$option_group = array(
				'id' => $posted_option_group['id'],
				'name' => $posted_option_group['name'],
				'default' => $posted_option_group['default'],
				'options' => array()
			);
			$option_group['options'] = isset( $posted_option_group['options'] ) ?
				array_filter( array_map( 'intval', (array) $posted_option_group['options'] ) ) :
				array();

			$option_groups[] = $option_group;
		}

		Wiaas_Package_Option_Groups::set_package_option_groups(
			$package,
			$posted_option_groups);
	}

	/**
     * Implements search for packages of provided type
	 * @param $package_type
	 */
	private static function _json_search_packages($package_type) {
		check_ajax_referer( 'search-products', 'security' );

		$term = wc_clean( empty( $term ) ? wp_unslash( $_GET['term'] ) : $term );

		if ( empty( $term ) ) {
			wp_die();
		}

		$data_store = WC_Data_Store::load( 'product' );
		$ids        = $data_store->search_products( $term );

		$packages= array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_readable' );
		$result        = array();

		foreach ( $packages as $package ) {

			if (Wiaas_Package_Type::get_package_type($package->get_id()) === $package_type) {
				$result[ $package->get_id() ] = rawurldecode( $package->get_formatted_name() );
			}
		}

		wp_send_json( $result );
	}
}

Wiaas_Admin_Linked_Packages::init();