<?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;
use SabaiApps\Directories\Component\Entity\Model\Bundle;

class AddEntity extends FrontendSubmit\Controller\AddEntity {

	protected $_reloadStepsOnNextStep = true;

	protected function _hideSelectPlan( Context $context, array &$formStorage ) {
		if ( ! isset( $formStorage['hide_select_plan'] ) ) {
			$formStorage['hide_select_plan'] = false;
			$plan_id                         = trim( $context->getRequest()->asStr( 'plan' ) );
			if ( strlen( $plan_id )
				&& ( $bundle = $this->_getBundle( $context, $formStorage ) )
			) {
				if ( ! empty( $plan_id ) ) {
					if ( ( $payment_component = $this->_getPaymentComponent( false ) )
						&& ( $plan = $payment_component->paymentGetPlanById( $bundle->name, $plan_id ) )
						&& in_array( $plan->paymentPlanType(), $this->Filter( 'payment_base_plan_types', array( 'base' ), array( $bundle->name ) ) )
					) {
						$formStorage['hide_select_plan'] = $plan_id;
					}
				} elseif ( $this->Payment_Plans_noPaymentPlanEnabled( $bundle->name ) ) {
						$formStorage['hide_select_plan'] = 0;
				}
			}
		}
		return $formStorage['hide_select_plan'];
	}

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

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

		if ( ! $this->getUser()->isAnonymous()
			|| $this->_isGuestAllowed()
		) {
			if ( false !== ( $plan_id = $this->_hideSelectPlan( $context, $formStorage ) ) ) {
				$formStorage['values']['select_plan']['plan'] = $plan_id;
			} else {
				$steps['select_plan'] = array( 'order' => 6 );
			}
		}

		$steps['checkout'] = array( 'order' => 99 );

		return $steps;
	}

	protected function _getPageTitle( Context $context, Bundle $bundle, array $formStorage ) {
		$title = parent::_getPageTitle( $context, $bundle, $formStorage );
		if ( $this->_isPaymentEnabled( $context, $formStorage )
			&& ( $plan = $this->_getSelectedPlan( $context, $formStorage ) )
		) {
			$title .= ' - ' . $plan->paymentPlanTitle();
		}

		return $title;
	}

	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'] );
		$bundle                 = $this->_getBundle( $context, $formStorage );
		$form                   = $this->Payment_Plans_form(
			$bundle->name,
			$this->Filter( 'payment_base_plan_types', array( 'base' ), array( $bundle->name ) ),
			false,
			$show_pricing_table ? $pricing_table_settings : false
		);
		if ( ! $show_pricing_table
			&& empty( $form['plan']['#options'] )
		) {
			$this->_submitable = false;
		}

		$this->_assignPageTitle( $context, $bundle, $formStorage );

		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;
		$context->is_dashboard = false;
		return $this->_getPaymentComponent( true )->paymentCheckoutForm( $context, $plan, $entity, 'add' );
	}

	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, 'add' );
	}

	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'] )
			&& ( $bundle = $this->_getBundle( $context, $formStorage ) )
		) {
			return $this->_getPaymentComponent( true )
				->paymentGetPlanById( $bundle->name, $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, 'add', $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() {
		if ( ! $payment_component = $this->_getPaymentComponent( false ) ) {
			return false;
		}

		return $payment_component->paymentIsGuestCheckoutEnabled()
			|| $payment_component->paymentIsGuestSignupEnabled()
			|| $payment_component->paymentIsGuestLoginEnabled();
	}

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

		try {
			$bundle = $this->_getBundle( $context, $formStorage );
		} catch ( \Exception $e ) {
			return false;
		}
		if ( empty( $bundle->info['payment_enable'] ) ) {
			return false;
		}

		if ( $this->_hasEntityLevel( $context, $formStorage, $bundle ) ) {
			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;
	}
}
