<?php
namespace SabaiApps\Directories\Component\Display\SystemWidget;

use SabaiApps\Directories\Component\System\Widget\AbstractWidget;

class ElementSystemWidget extends AbstractWidget {

	protected $_cacheable = false;

	protected function _systemWidgetInfo() {
		return array(
			'title'   => __( 'Display Element', 'directories-pro' ),
			'summary' => __( 'Display content rendered by a display element.', 'directories-pro' ),
		);
	}

	protected function _getElementOptions( $bundle, array &$options, array $elements ) {
		foreach ( array_keys( $elements ) as $element_id ) {
			$element =& $elements[ $element_id ];
			if ( ! empty( $element['visibility']['globalize'] ) ) {
				$options[ $bundle->name . ',' . $element_id ] = $bundle->getGroupLabel() . ' - '
					. $bundle->getLabel( 'singular' ) . ' - '
					. ( strlen( $element['title'] ) ? strip_tags( $element['title'] ) : $element['label'] )
					. ' (ID: ' . $element['id'] . ')';
			} elseif ( ! empty( $element['children'] ) ) {
					$this->_getElementOptions( $bundle, $options, $element['children'] );
			}
		}
	}

	protected function _getWidgetSettings( array $settings ) {
		$options = array();
		foreach ( $this->_application->Entity_Bundles_sort() as $bundles ) {
			foreach ( $bundles as $bundle_name => $bundle ) {
				if ( $display = $this->_application->Display_Display( $bundle_name, 'detailed' ) ) {
					$this->_getElementOptions( $bundle, $options, $display['elements'] );
				}
			}
		}

		return array(
			'element' => array(
				'#title'         => __( 'Select display element', 'directories-pro' ),
				'#options'       => $options,
				'#type'          => 'select',
				'#default_value' => null,
			),
		);
	}

	protected function _getWidgetContent( array $settings ) {
		if ( empty( $settings['element'] )
			|| ! isset( $GLOBALS['drts_entity'] )
			|| ( ! $element = explode( ',', $settings['element'] ) )
			|| $GLOBALS['drts_entity']->getBundleName() !== $element[0]
			|| ! isset( $GLOBALS['drts_display_elements'][ $element[0] ][ $element[1] ] )
			|| ! strlen( $GLOBALS['drts_display_elements'][ $element[0] ][ $element[1] ] )
		) {
			return;
		}

		return $GLOBALS['drts_display_elements'][ $element[0] ][ $element[1] ];
	}
}
