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

use SabaiApps\Directories\Component\FrontendSubmit;
use SabaiApps\Directories\Context;
use SabaiApps\Directories\Component\Form;
use SabaiApps\Directories\Component\Entity;

class ClaimEntity extends FrontendSubmit\Controller\AddChildEntity {

	protected $_reloadStepsOnNextStep = true;

	protected function _getSteps( Context $context, array &$formStorage ) {
		if ( ! $steps = parent::_getSteps( $context, $formStorage ) ) {
			return array();
		}

		if ( ! $this->_isPaymentEnabled( $context, $formStorage ) ) {
			return $steps;
		}

		if ( ! $this->getUser()->isAnonymous()
			|| $this->_isGuestAllowed( $context, $formStorage )
		) {
			$steps += array(
				'select_plan' => array( 'order' => 6 ),
				'checkout'    => array( 'order' => 99 ),
			);
		}

		return $steps;
	}

	public function _getFormForStepSelectPlan( Context $context, array &$formStorage ) {
		$pricing_table_settings = $this->getComponent( 'Payment' )->getConfig( 'plan', 'pricing_table' );
		$show_pricing_table     = $context->getContainer() !== '#drts-modal' && ! empty( $pricing_table_settings['show'] );
		$parent_bundle          = $this->Entity_Bundle( $context->entity->getBundleName(), '', true );
		$form                   = $this->Payment_Plans_form(
			$parent_bundle->name,
			$this->Filter( 'payment_base_plan_types', array( 'base' ), array( $parent_bundle->name ) ),
			false,
			$show_pricing_table ? $pricing_table_settings : false
		);
		if ( ! $show_pricing_table
			&& empty( $form['plan']['#options'] )
		) {
			$this->_submitable = false;
		} else {
			$form['#action'] = $this->_getAddEntityUrl( $context );
		}
		$this->_cancelUrl = $this->Entity_PermalinkUrl( $context->entity );
		return $form;
	}

	public function _getFormForStepCheckout( Context $context, array &$formStorage ) {
		if ( ! $plan = $this->_getSelectedPlan( $context, $formStorage ) ) {
			return array();
		}

		if ( ! $entity = $this->_getEntity( $context, $formStorage ) ) {
			return false;
		}

		$this->_submitButtons = false;
		return $this->_getPaymentComponent( true )->paymentCheckoutForm( $context, $plan, $entity, 'claim' );
	}

	public function _complete( Context $context, array $formStorage ) {
		if ( false === parent::_complete( $context, $formStorage ) ) {
			return false;
		}

		if ( ! $plan = $this->_getSelectedPlan( $context, $formStorage ) ) {
			return;
		}

		if ( ! $entity = $this->_getEntity( $context, $formStorage ) ) {
			return false;
		}

		return $this->_getPaymentComponent( true )->paymentCheckoutComplete( $context, $plan, $entity, 'claim' );
	}

	protected function _getEntityStatus( Context $context, Form\Form $form, Entity\Model\Bundle $bundle ) {
		if ( ! $this->_isPaymentEnabled( $context, $form->storage )
			|| ! $this->_getSelectedPlan( $context, $form->storage )
		) {
			return parent::_getEntityStatus( $context, $form, $bundle );
		}
		return $this->Entity_Status( $bundle->entitytype_name, 'draft' );
	}

	protected function _getSelectedPlan( Context $context, array $formStorage ) {
		if ( ! empty( $formStorage['values']['select_plan']['plan'] ) ) {
			return $this->_getPaymentComponent( true )
				->paymentGetPlanById( $context->entity->getBundleName(), $formStorage['values']['select_plan']['plan'] );
		}
	}

	protected function _getSubmitButtonForStepAdd( Context $context, array &$formStorage ) {
		if ( ! $this->_isPaymentEnabled( $context, $formStorage )
			|| ! $this->_getSelectedPlan( $context, $formStorage )
		) {
			return parent::_getSubmitButtonForStepAdd( $context, $formStorage );
		}

		return $this->Filter(
			'payment_checkout_btn_text',
			__( 'Proceed to checkout', 'directories-pro' ),
			array( $this->_getBundle( $context, $formStorage )->name, 'claim', $formStorage['values']['select_plan']['plan'] )
		);
	}

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

		return $ret;
	}

	protected function _getPaymentComponent( $throwError = true ) {
		return $this->getComponent( 'Payment' )->getPaymentComponent( $throwError );
	}

	protected function _isGuestAllowed( Context $context, array $formStorage ) {
		return $this->Filter( 'claiming_is_guest_claimable', false, array( $this->_getBundle( $context, $formStorage ) ) );
	}

	protected function _isPaymentEnabled( Context $context, array $formStorage ) {
		if ( $this->getUser()->isAnonymous()
			&& ! $this->_isGuestAllowed( $context, $formStorage )
		) {
			return false;
		}

		if ( ( ! $parent_bundle = $this->Entity_Bundle( $context->entity->getBundleName() ) )
			|| empty( $parent_bundle->info['payment_enable'] )
		) {
			return false;
		}

		return ( $component = $this->_getPaymentComponent( false ) ) ? $component : false;
	}

	protected function _getSubmitEntityFormOptions( Context $context, array &$formStorage, $entityOrBundle, $wrap = null ) {
		$ret = parent::_getSubmitEntityFormOptions( $context, $formStorage, $entityOrBundle, $wrap );
		if ( $this->_isPaymentEnabled( $context, $formStorage )
			&& ( $plan = $this->_getSelectedPlan( $context, $formStorage ) )
		) {
			$ret['payment_plan'] = $plan;
		}
		return $ret;
	}
}
