<?php
namespace SabaiApps\Directories\Component\User\DisplayElement;

use SabaiApps\Directories\Component\View\DisplayElement\AbstractEntitiesDisplayElement;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Display\Model\Display;
use SabaiApps\Directories\Component\Display\Model\Element;
use SabaiApps\Directories\Exception\IException;
class UserPostsDisplayElement extends AbstractEntitiesDisplayElement {

	protected function _displayElementInfo( Bundle $bundle ) {
		$info                               = parent::_displayElementInfo( $bundle );
		$info['label']                      = __( 'User Posts', 'directories-pro' );
		$info['description']                = __( 'Displays posts submitted by the user', 'directories-pro' );
		$info['default_settings']['bundle'] = null;

		return $info;
	}

	public function displayElementSupports( Bundle $bundle, Display $display ) {
		return parent::displayElementSupports( $bundle, $display ) && $bundle->type === 'user__user';
	}

	public function displayElementSettingsForm( Bundle $bundle, array $settings, Display $display, array $parents = array(), $tab = null, $isEdit = false, array $submitValues = array() ) {
		$form = array();
		// Add content type selection field
		$options = array();
		foreach ( $this->_application->Entity_Bundles() as $_bundle ) {
			if ( $_bundle->entitytype_name !== 'post' ) {
				continue;
			}

			$options[ $_bundle->name ] = $_bundle->getGroupLabel() . ' - ' . $_bundle->getLabel( 'singular' );
		}
		$form['bundle'] = array(
			'#type'          => 'select',
			'#title'         => __( 'Content type', 'directories-pro' ),
			'#options'       => $options,
			'#horizontal'    => true,
			'#default_value' => $settings['bundle'],
		);
		// Add view selection field
		$views = array();
		foreach ( $this->_application->getModel( 'View', 'View' )->bundleName_in( array_keys( $options ) )->fetch() as $view ) {
			$views[ $view->bundle_name ][ $view->name ] = $view->getLabel();
		}
		foreach ( array_keys( $views ) as $bundle_name ) {
			$form[ 'view_' . $bundle_name ] = array(
				'#type'          => 'select',
				'#title'         => __( 'Select view', 'directories-pro' ),
				'#options'       => $views[ $bundle_name ],
				'#horizontal'    => true,
				'#default_value' => isset( $settings[ 'view_' . $bundle_name ] ) ? $settings[ 'view_' . $bundle_name ] : null,
				'#states'        => array(
					'visible' => array(
						sprintf( 'select[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'bundle' ) ) ) ) => array( 'value' => $bundle_name ),
					),
				),
			);
		}

		return $form;
	}

	protected function _getEntitiesBundle( $entityOrBundle, array $settings ) {
		try {
			return $this->_application->Entity_Bundle( $settings['bundle'], '', true ); } catch ( IException $e ) {
			}
			return null;
	}

	protected function _getListEntitiesSettings( Bundle $bundle, array $element, IEntity $entity ) {
		if ( empty( $element['settings'][ 'view_' . $bundle->name ] ) ) {
			return;
		}

		$settings                                      = array();
		$settings['load_view']                         = $element['settings'][ 'view_' . $bundle->name ];
		$settings['view_settings']['query']['user_id'] = $entity->getId();

		return $settings;
	}

	protected function _displayElementReadableInfo( Bundle $bundle, Element $element ) {
		$settings = $element->data['settings'];
		if ( empty( $settings['bundle'] )
			|| ( ! $bundle = $this->_getEntitiesBundle( $settings['bundle'], $settings ) )
		) {
			return;
		}

		$ret = array(
			'bundle' => array(
				'label' => __( 'Content type', 'directories-pro' ),
				'value' => $bundle->getGroupLabel() . ' - ' . $bundle->getLabel( 'singular' ),
			),
		);
		if ( ! empty( $settings[ 'view_' . $bundle->name ] )
			&& ( $views = $this->_getViewOptions( $bundle, $settings ) )
			&& isset( $views[ $settings[ 'view_' . $bundle->name ] ] )
		) {
			$ret['view'] = array(
				'label' => __( 'Select view', 'directories-pro' ),
				'value' => $views[ $settings[ 'view_' . $bundle->name ] ],
			);
		}

		return array( 'settings' => array( 'value' => $ret ) );
	}
}
