<?php

if ( ! class_exists( 'GFForms' ) ) {
	die();
}

class Wiaas_Order_Installation_Select extends GF_Field_Select {

	public $type = 'wiaas_order_installation_select';


	public function get_input_type() {
		return 'workflow_assignee_select';
	}

	public function add_button( $field_groups ) {
		$field_groups = Wiaas_Order_Fields::maybe_add_order_field_group($field_groups);

		return parent::add_button( $field_groups );
	}

	public function get_form_editor_button() {
		return array(
			'group' => 'wiaas_order_fields',
			'text'  => $this->get_form_editor_field_title(),
		);
	}

	public function get_field_input( $form, $value = '', $entry = null ) {

		if ( empty($entry) ) {

			return parent::get_field_input($form, $value, $entry);
		}

		// get bundle item field
		$bundle_item_field = GFCommon::get_fields_by_type($form, array( 'wiaas_order_bundle') )[0];

		if (! empty($bundle_item_field) &&
		    $bundle_item = $bundle_item_field->get_bundle_item($entry[$bundle_item_field->id])) {

			$bundled_items = wc_pb_get_bundled_order_items($bundle_item);

			$installation_items = array();

			foreach ($bundled_items as $id => $bundled_item) {

				$product = $bundled_item->get_product();

				if ($product && Wiaas_Product_Category::is_installation($product)) {

					$installation_items[] = $bundled_item;
				}
			}

			$choices = array(
				array( 'value' => '0', 'text' => 'Select installation ...')
			);

			$order_id = $bundle_item->get_order_id();

			foreach ($installation_items as $installation_item) {

				$choices[] = array(
					'value' => 'wiaas_installation_' . $order_id . '|' . $installation_item->get_id(),
					'text' => $installation_item->get_name()
				);
			}

			$this->choices = $choices;
		}

		return parent::get_field_input($form, $value, $entry);
	}

	public function get_form_editor_field_title() {
		return esc_attr__( 'Installation Select', 'wiaas' );
	}

	public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
		return $this->get_selected_installation_display_name($value);
	}


	public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
		return $this->get_selected_installation_display_name($value);
	}


	public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
		return $this->get_selected_installation_display_name($value);
	}

	public function get_selected_installation($value) {

		$value = $value ? str_replace('wiaas_installation_', '', $value) : '';

		list ($order_id, $item_id) = explode('|', $value);

		if (! empty($order_id) && ! empty($item_id) && $order = wc_get_order($order_id)) {

			return $order->get_item($item_id);
		}

		return null;
	}

	public function get_selected_installation_display_name($value) {

		$item = $this->get_selected_installation($value);

		return ! empty($item) ? $item->get_name() : '';
	}

	public function post_convert_field() {

		if ($this->is_form_editor()) {

			$this->choices = array(
				array( 'value' => '0', 'text' => 'Select installation ...')
			);
		}
	}
}


GF_Fields::register( new Wiaas_Order_Installation_Select() );