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

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

class SubmitPost extends Dashboard\Controller\SubmitPost {

	protected $_showPricingTable = false;

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

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

		return array(
			'select_plan' => array( 'order' => 3 ),
			'checkout'    => array( 'order' => 99 ),
		);
	}

	public function _getFormForStepSelectPlan( Context $context, array &$formStorage ) {
		if ( $this->Payment_Util_hasPendingOrder( $context->entity ) ) {
			$context->setError(
				__( 'There are currently one or more pending orders for the item selected.', 'directories-pro' ),
				'/' . $this->getPlatform()->getSlug( 'Dashboard', 'dashboard' )
			);
			return false;
		}
		if ( $this->Payment_Util_isSuspended( $context->entity ) ) {
			$context->setError(
				__( 'The selected item is currently suspended.', 'directories-pro' ),
				'/' . $this->getPlatform()->getSlug( 'Dashboard', 'dashboard' )
			);
			return false;
		}

		if ( $context->action === 'submit' ) {
			$bundle = $this->_getBundle( $context, $formStorage );

			// Check if submission is restricted
			if ( ! $this->_isSubmitAllowed( $context, $formStorage, $bundle ) ) {
				return false;
			}
		}

		if ( $this->_showPricingTable ) {
			$pricing_table_settings = $this->getComponent( 'Payment' )->getConfig( 'plan', 'pricing_table' );
			$show_pricing_table     = ! empty( $pricing_table_settings['show'] )
				&& $context->getContainer() !== '#drts-modal'
				&& ! $this->Payment_Plan( $context->entity ); // show if no current payment plan
		} else {
			$show_pricing_table = false;
		}
		$form = $this->Payment_Plans_form(
			$context->entity,
			$this->Filter( 'payment_base_plan_types', array( 'base' ), array( $context->entity->getBundleName() ) ),
			$context->action === 'upgrade',
			$show_pricing_table ? $pricing_table_settings : false,
			$this->Filter( 'payment_submit_post_add_free_plan', $context->action !== 'upgrade', array( $context->action, $context->entity ) )
		);
		if ( ! $show_pricing_table ) {
			if ( empty( $form['plan']['#options'] ) ) {
				$this->_submitable = false;
			} else {
				$this->_ajaxSubmit      = false;
				$this->_submitButtons[] = array(
					'#btn_color'  => 'primary',
					'#btn_label'  => $this->Filter(
						'payment_checkout_btn_text',
						__( 'Proceed to checkout', 'directories-pro' ),
						array( $context->entity->getBundleName(), 'submit' )
					),
					'#btn_size'   => 'lg',
					'#attributes' => array( 'data-modal-title' => '' ), // prevents modal title from changing on submit
				);
			}
		}
		$context->is_dashboard = false;

		return $form;
	}

	public function _submitFormForStepSelectPlan( Context $context, Form\Form $form ) {
		$bundle = $this->_getBundle( $context, $form->storage );
		$status = $this->_getEntityStatus( $context, $form, $bundle );
		$values = $this->_getEntityValues( $context, $form, $bundle );
		$this->_updateSubmittedEntity( $context, $status, $values );
	}

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

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

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

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

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

	protected function _getSelectedPlan( Context $context, array $formStorage ) {
		if ( ! empty( $formStorage['values']['select_plan']['plan'] )
			&& ( $bundle = $this->_getBundle( $context, $formStorage ) )
		) {
			// Get plan by ID
			return $this->_getPaymentComponent( true )
				->paymentGetPlanById( $bundle->name, $formStorage['values']['select_plan']['plan'] );
		}
	}

	protected function _getEntityValues( Context $context, Form\Form $form, Entity\Model\Bundle $bundle ) {
		$values = parent::_getEntityValues( $context, $form, $bundle );
		if ( ! $this->_isPaymentEnabled( $context, $form->storage )
			|| ( ! $plan = $this->_getSelectedPlan( $context, $form->storage ) )
		) {
			// Make sure payment plan is unassigned
			$values['payment_plan'] = false;
		}
		return $values;
	}

	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 _getPaymentComponent( $throwError = true ) {
		return $this->getComponent( 'Payment' )->getPaymentComponent( $throwError );
	}

	protected function _isPaymentEnabled( Context $context, array $formStorage ) {
		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;
	}
}
