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

use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Form;
use SabaiApps\Directories\Context;
use SabaiApps\Framework\Application\HttpRequest;

abstract class AbstractSubmitEntity extends Form\AbstractMultiStepController {

	protected function _getSubmitEntityForm( Context $context, array &$formStorage, $entityOrBundle, $btnLabel = null, $wrap = null ) {
		$context->addTemplate( 'entity_form' );
		$btn = is_array( $btnLabel ) ? $btnLabel : array( '#btn_label' => $btnLabel );
		$this->_submitButtons[ $context->currentStep ] = $btn + array(
			'#btn_color'  => 'primary',
			'#btn_size'   => 'lg',
			'#attributes' => array( 'data-modal-title' => 'false' ), // prevent modal title from changing
		);

		return $this->Entity_Form( $entityOrBundle, $this->_getSubmitEntityFormOptions( $context, $formStorage, $entityOrBundle, $wrap ) );
	}

	protected function _getSubmitEntityFormOptions( Context $context, array &$formStorage, $entityOrBundle, $wrap = null ) {
		return array(
			'values'             => $this->_getSubimttedValues( $context, $formStorage ),
			'pre_render_display' => true,
			'wrap'               => isset( $wrap ) ? $wrap : 'drts',
		);
	}

	protected function _getBundle( Context $context, array $formStorage ) {
		return $this->Entity_Bundle( $context->entity->getBundleName(), '', true );
	}

	protected function _hasEntityLevel( Context $context, array $formStorage, Bundle $bundle ) {
		if ( ! empty( $bundle->info['parent'] )
			|| ! empty( $bundle->info['parent_field'] )
		) {
			return false;
		}

		if ( $this->getUser()->isAnonymous() ) {
			$identity = isset( $formStorage['_guest']['email'] ) ? $formStorage['_guest']['email'] : HttpRequest::ip();
		} else {
			$identity = $this->getUser()->getIdentity();
		}
		$result = $this->FrontendSubmit_Restrictors_hasEntityLevel( $bundle, $identity );

		return $this->Filter( 'frontendsubmit_submit_entity_feature_level', $result, array( $bundle, $identity ) );
	}

	protected function _isSubmitAllowed( Context $context, array $formStorage, Bundle $bundle ) {
		if ( $this->getUser()->isAnonymous() ) {
			if ( ! $identity = $this->_application->Dashboard_GuestAccess_isGranted() ) {
				$identity = isset( $formStorage['_guest']['email'] ) ? $formStorage['_guest']['email'] : HttpRequest::ip();
			}
		} else {
			$identity = $this->getUser()->getIdentity();
		}
		$parent_entity_id = ( $parent_entity = $this->_getParentEntity( $context, $formStorage ) ) ? $parent_entity->getId() : null;
		if ( ! $this->FrontendSubmit_Restrictors_isAllowed( $bundle, $identity, $parent_entity_id ) ) {
			$error_message = sprintf(
				__( 'You have already reached the maximum number of %1$s allowed.', 'directories-pro' ),
				$bundle->getLabel()
			);
			$context->setError(
				$this->Filter( 'frontendsubmit_submit_restriction_error_message', $error_message, array( $bundle ) ),
				( $url = $context->getErrorUrl() ) ? $url : $bundle->getPath()
			);
			return false;
		}
		return $this->Filter( 'frontendsubmit_submit_allowed', true, array( $bundle, $identity, $parent_entity_id ) );
	}

	/**
	 * @param Context $context
	 * @param array   $formStorage
	 * @return IEntity
	 */
	protected function _getParentEntity( Context $context, array $formStorage ) {}
}
