<?php

class Wiaas_Field_Order_Bundle extends GF_Field_Text {

	public $type = 'wiaas_order_bundle';

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

	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_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
		return $this->get_bundle_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_bundle_display_name($value);
	}


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

	public function get_bundle_item($value) {

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

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

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

		return null;
	}

	public function get_bundle_display_name($value) {

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

		if (! empty($item)) {

			return $item->get_name();
		}

		return '';
	}
}

GF_Fields::register( new Wiaas_Field_Order_Bundle() );