<?php
namespace SabaiApps\Directories\Component\FrontendSubmit\Controller;

use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Context;
use SabaiApps\Directories\Exception;

class AddEntity extends AbstractAddEntity {

	protected function _getSteps( Context $context, array &$formStorage ) {
		// Fetch submittable bundle options
		$submittable_bundles = $this->FrontendSubmit_Submission_bundles( $context->bundle_type );

		if ( $this->getUser()->isAnonymous() ) {
			if ( empty( $submittable_bundles )
				|| $this->callHelper( 'FrontendSubmit_Submission_isGuestRedirectRequired', array( $context->bundle_type, $context, &$formStorage ) )
			) {
				$redirect_bundle = null;
				if ( ! empty( $submittable_bundles ) ) {
					$redirect_bundle = $context->bundle_type;
				}
				return $this->_redirectGuest( $context, $formStorage, $redirect_bundle );
			}
		} elseif ( empty( $submittable_bundles ) ) {
				$context->setError( __( 'There is no submittable content.', 'drts' ) );
				return array();
		}

		$steps = parent::_getSteps( $context, $formStorage );
		if ( ( $bundle_name = $this->_hideSelectBundle( $context, $formStorage ) )
			&& ( $bundle = $this->Entity_Bundle( $bundle_name ) )
		) {
			$formStorage['values']['select_bundle']['bundle'] = $bundle_name;
			$context->popInfo();
			$context->setInfo( $bundle->getLabel( 'add' ) );
			return $steps;
		}
		return $steps + array(
			'select_bundle' => array( 'order' => 5 ),
		);
	}

	protected function _hideSelectBundle( Context $context, array &$formStorage ) {
		if ( ! isset( $formStorage['hide_select_bundle'] ) ) {
			$bundles = $this->FrontendSubmit_Submission_bundles( $context->bundle_type );
			if ( $bundle = $context->getRequest()->asStr( 'bundle' ) ) {
				if ( isset( $bundles[ $bundle ] ) ) {
					$formStorage['hide_select_bundle'] = $bundle;
				} else {
					// Requested bundle is not submittable, so do not hide select bundle form
					$formStorage['hide_select_bundle'] = false;
				}
			} else {
				$formStorage['hide_select_bundle'] = count( $bundles ) === 1 ? current( array_keys( $bundles ) ) : false;
			}
		}
		return $formStorage['hide_select_bundle'];
	}

	public function _getFormForStepSelectBundle( Context $context, array &$formStorage ) {
		$form = array(
			'bundle' => array(
				'#title'              => __( 'Select content type', 'directories-pro' ),
				'#type'               => 'radios',
				'#options'            => $this->FrontendSubmit_Submission_bundles( $context->bundle_type ),
				'#required'           => true,
				'#default_value_auto' => true,
			),
		);
		return $this->Filter( 'frontendsubmit_add_entity_select_bundle_form', $form, array( $context->bundle_type ) );
	}

	/*
	 * @return SabaiApps\Directories\Component\Entity\Model\Bundle
	 */
	protected function _getBundle( Context $context, array $formStorage ) {
		if ( ! empty( $formStorage['values']['select_bundle']['bundle'] ) ) {
			return $this->Entity_Bundle( $formStorage['values']['select_bundle']['bundle'], '', true );
		}

		throw new Exception\RuntimeException( 'No bundle selected!' );
	}

	protected function _getPageTitle( Context $context, Bundle $bundle, array $formStorage ) {
		return sprintf( $bundle->getLabel( 'add' ), $bundle->getGroupLabel() );
	}

	protected function _getRedirectGuestUrlParams( Context $context, array $formStorage ) {
		$ret   = parent::_getRedirectGuestUrlParams( $context, $formStorage );
		$ret[] = 'bundle';

		return $ret;
	}

	protected function _getSubmitEntityFormOptions( Context $context, array &$formStorage, $entityOrBundle, $wrap = null ) {
		$ret = parent::_getSubmitEntityFormOptions( $context, $formStorage, $entityOrBundle, $wrap );
		if ( $level = $this->_hasEntityLevel( $context, $formStorage, $this->_getBundle( $context, $formStorage ) ) ) {
			$ret['entity_level'] = $level;
		}
		return $ret;
	}
}
