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

use SabaiApps\Directories\Controller;
use SabaiApps\Directories\Context;

class PricingTable extends Controller {

	protected function _doExecute( Context $context ) {
		$settings = $this->_getSettings( $context );
		$plans    = $this->Payment_Plans( $context->bundle->name, $this->Filter( 'payment_base_plan_types', array( 'base' ), array( $context->bundle->name ) ), true );
		// Specific plans only?
		if ( isset( $settings['plans'] ) ) {
			if ( is_string( $settings['plans'] ) ) {
				$settings['plans'] = explode( ',', trim( $settings['plans'] ) );
			}
			if ( ! empty( $settings['plans'] ) ) {
				$_plans = array();
				// Filter and sort as specified
				foreach ( $settings['plans'] as $plan_id ) {
					if ( isset( $plans[ $plan_id ] ) ) {
						$_plans[ $plan_id ] = $plans[ $plan_id ];
					}
				}
				$plans = $_plans;
			}
		}

		$max_feature_count = 0;
		$has_featured      = false;
		foreach ( array_keys( $plans ) as $plan_id ) {
			$plan     = $plans[ $plan_id ];
			$features = $this->Payment_Features_render( $this->Payment_Plan_features( $plan ), $context->bundle, $plan );
			if ( $feature_list = $plan->paymentPlanFeatureList() ) {
				$features[] = $feature_list;
			}
			$feature_count = array_sum( array_map( 'count', $features ) );
			if ( $feature_count > $max_feature_count ) {
				$max_feature_count = $feature_count;
			}
			if ( ! $has_featured ) {
				$has_featured = $plan->paymentPlanIsFeatured();
			}
			$plans[ $plan_id ] = array(
				'title'        => $plan->paymentPlanTitle(),
				'description'  => $plan->paymentPlanDescription(),
				'price'        => $this->Payment_Plan_formattedPrice( $plan ),
				'featured'     => $plan->paymentPlanIsFeatured(),
				'features'     => $features,
				'order_url'    => $this->Url(
					'/' . $this->FrontendSubmit_AddEntitySlug( $context->bundle ),
					array(
						'bundle' => $context->bundle->name,
						'plan'   => $plan_id,
					)
				),
				'has_discount' => $this->getComponent( 'Payment' )->getPaymentComponent()->paymentPlanHasDiscount( $plan ),
			);
		}

		// Add no payment plan option?
		if ( $settings['add_no_payment_plan'] !== false ) {
			$features      = $this->Payment_Features_render( $this->Payment_Plan_defaultFeatures( $context->bundle ), $context->bundle );
			$feature_count = array_sum( array_map( 'count', $features ) );
			if ( $feature_count > $max_feature_count ) {
				$max_feature_count = $feature_count;
			}
			$no_payment_plan_label = $this->Payment_Plans_noPaymentPlanLabel( $context->bundle );
			$no_payment_plan       = array(
				'title'       => isset( $settings['no_payment_plan_title'] ) ? $settings['no_payment_plan_title'] : $no_payment_plan_label[0],
				'description' => isset( $settings['no_payment_plan_desc'] ) ? $settings['no_payment_plan_desc'] : $no_payment_plan_label[1],
				'price'       => _x( 'Free', 'payment plan price', 'directories-pro' ),
				'featured'    => false,
				'features'    => $features,
				'order_url'   => $this->Url(
					'/' . $this->FrontendSubmit_AddEntitySlug( $context->bundle ),
					array(
						'bundle' => $context->bundle->name,
						'plan'   => 0,
					)
				),
			);
			$no_payment_plan_pos   = isset( $settings['no_payment_plan_pos'] ) ? (int) $settings['no_payment_plan_pos'] : $settings['add_no_payment_plan'];
			if ( $no_payment_plan_pos === 0 ) {
				$plans = array( 0 => $no_payment_plan ) + $plans; // prepend
			} else {
				$plans[0] = $no_payment_plan; // append
			}
		}

		$context->addTemplate( 'payment_pricing_table' )
			->setAttributes(
				array(
					'plans'             => $this->Filter( 'payment_pricing_table_plans', $plans, array( $context->bundle ) ),
					'max_feature_count' => $max_feature_count,
					'has_featured'      => $has_featured,
				) + $settings
			);
	}

	protected function _getSettings( Context $context ) {
		$settings = $context->settings ?: array();
		if ( ! isset( $settings['color'] )
			|| ! in_array( $settings['color'], array( 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark' ) )
		) {
			$settings['color'] = 'primary';
		}
		$settings          += array(
			'plans'                 => null,
			'btn_text'              => null,
			'btn_color'             => in_array( $settings['color'], array( 'dark', 'light' ) ) ? 'outline-secondary' : 'outline-' . $settings['color'],
			'featured_text_color'   => in_array( $settings['color'], array( 'dark', 'light' ) ) ? 'secondary' : 'white',
			'featured_border_color' => $settings['color'],
			'featured_bg_color'     => $settings['color'],
			'featured_btn_color'    => in_array( $settings['color'], array( 'dark', 'light' ) ) ? 'outline-secondary' : 'outline-light',
		);
		$settings['layout'] = 'group';
		if ( ! isset( $settings['btn_text'] )
			|| ! strlen( $settings['btn_text'] )
		) {
			$settings['btn_text'] = __( 'Choose Plan', 'directories-pro' );
		}
		// Add no payment plan option?
		if ( $this->Payment_Plans_noPaymentPlanEnabled( $context->bundle->name )
			&& isset( $settings['add_no_payment_plan'] )
		) {
			$settings['add_no_payment_plan'] = (int) $settings['add_no_payment_plan'];
		} else {
			$settings['add_no_payment_plan'] = false;
		}

		return $settings;
	}
}
