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

use SabaiApps\Directories\Application;

class DirectoryAdminSettingsFormHelper {

	public function help( Application $application, &$form ) {
		$name                  = 'Payment';
		$form['tabs'][ $name ] = array(
			'#title'  => __( 'Payment', 'directories-pro' ),
			'#weight' => 25,
		);

		$config = $application->getComponent( 'Payment' )->getConfig();

		// Fetch all available payment components
		$payment_components = $payment_components_disabled = $payment_component_settings_form = $payment_component_show_currency = array();
		foreach ( array_keys( $application->Payment_Components() ) as $payment_component_name ) {
			if ( ! $component = $application->Payment_Components_impl( $payment_component_name, true ) ) {
				continue;
			}

			$payment_components[ $payment_component_name ] = $component->paymentComponentInfo( 'label' );
			if ( ! $component->paymentIsEnabled() ) {
				$payment_components_disabled[ $payment_component_name ] = $payment_component_name;
			} else {
				$payment_component_settings                                 = isset( $config['payment']['components'][ $payment_component_name ] ) ? $config['payment']['components'][ $payment_component_name ] : array();
				$payment_component_settings                                += (array) $component->paymentComponentInfo( 'default_settings' );
				$payment_component_settings_form[ $payment_component_name ] = array(
					'#states' => array(
						'visible' => array(
							'[name="' . $name . '[payment][component]"]' => array( 'value' => $payment_component_name ),
						),
					),
				) + $component->paymentSettingsForm( $payment_component_settings, array( $name, 'payment', 'components', $payment_component_name ) );
			}
		}

		// Get current payment component currency symbol
		$currency_symbol = ( $payment_component = $application->getComponent( 'Payment' )->getPaymentComponent() ) ? $payment_component->paymentGetCurrencySymbol() : '$';

		$form['fields'][ $name ] = array(
			'#component' => $name,
			'#tab'       => $name,
			'payment'    => array(
				'#weight'    => 1,
				'#title'     => __( 'General Settings', 'directories-pro' ),
				'component'  => array(
					'#type'             => 'select',
					'#title'            => __( 'Payment component', 'directories-pro' ),
					'#options'          => $payment_components,
					'#options_disabled' => $payment_components_disabled,
					'#default_value'    => isset( $config['payment']['component'] ) ? strtolower( $config['payment']['component'] ) : 'default',
					'#horizontal'       => true,
				),
				'components' => $payment_component_settings_form,
			),
			'plan'       => array(
				'#title'            => __( 'Payment Plan Settings', 'directories-pro' ),
				'#weight'           => 10,
				'expired_action'    => array(
					'#type'          => 'select',
					'#title'         => __( 'Action after payment plan expiration', 'directories-pro' ),
					'#options'       => array(
						'deactivate'     => __( 'Deactivate', 'directories-pro' ),
						'no_plan_author' => __( 'Unassign payment plan and author', 'directories-pro' ),
						'no_plan'        => __( 'Unassign payment plan', 'directories-pro' ),
						'trash'          => __( 'Move to trash', 'directories-pro' ),
						'none'           => __( 'None', 'directories-pro' ),
					),
					'#default_value' => isset( $config['plan']['expired_action'] ) ? $config['plan']['expired_action'] : 'deactivate',
					'#horizontal'    => true,
				),
				'allow_none'        => array(
					'#title'         => __( 'Add "No payment plan" option', 'directories-pro' ),
					'#type'          => 'checkbox',
					'#default_value' => ! empty( $config['plan']['allow_none'] ),
					'#horizontal'    => true,
				),
				'none_label'        => array(
					'#field_prefix'  => __( 'Label', 'directories-pro' ),
					'#type'          => 'textfield',
					'#default_value' => isset( $config['plan']['none_label'] ) ? $config['plan']['none_label'] : __( 'No payment plan (FREE)', 'directories-pro' ),
					'#horizontal'    => true,
					'#states'        => array(
						'visible' => array(
							'input[name="Payment[plan][allow_none]"]' => array(
								'type'  => 'checked',
								'value' => true,
							),
						),
					),
				),
				'none_desc'         => array(
					'#placeholder'   => __( 'Description', 'directories-pro' ),
					'#type'          => 'textarea',
					'#rows'          => 3,
					'#default_value' => isset( $config['plan']['none_desc'] ) ? $config['plan']['none_desc'] : null,
					'#horizontal'    => true,
					'#states'        => array(
						'visible' => array(
							'input[name="Payment[plan][allow_none]"]' => array(
								'type'  => 'checked',
								'value' => true,
							),
						),
					),
				),
				'pricing_table'     => array(
					'show'                => array(
						'#title'         => __( 'Display pricing table', 'directories-pro' ),
						'#type'          => 'checkbox',
						'#default_value' => ! empty( $config['plan']['pricing_table']['show'] ),
						'#horizontal'    => true,
					),
					// 'color' => [
					// '#type' => 'radios',
					// '#default_value' => isset($config['plan']['pricing_table']['color']) ? $config['plan']['pricing_table']['color'] : null,
					// '#options' => $application->System_Color_options(),
					// '#option_no_escape' => true,
					// '#default_value_auto' => true,
					// '#horizontal' => true,
					// '#columns' => 4,
					// '#states' => [
					// 'visible' => [
					// 'input[name="Payment[plan][pricing_table][show]"]' => ['type' => 'checked', 'value' => true],
					// ],
					// ],
					// ],
					'btn_text'            => array(
						'#type'          => 'textfield',
						'#default_value' => isset( $config['plan']['pricing_table']['btn_text'] ) ? $config['plan']['pricing_table']['btn_text'] : null,
						'#placeholder'   => __( 'Choose Plan', 'directories-pro' ),
						'#horizontal'    => true,
						'#field_prefix'  => __( 'Button label', 'directories-pro' ),
						'#states'        => array(
							'visible' => array(
								'input[name="Payment[plan][pricing_table][show]"]' => array(
									'type'  => 'checked',
									'value' => true,
								),
							),
						),
					),
					'no_payment_plan_pos' => array(
						'#type'         => 'select',
						'#field_prefix' => __( 'Append or prepend "No payment plan" option', 'directories-pro' ),
						'#options'      => array(
							0 => __( 'Prepend', 'directories-pro' ),
							1 => __( 'Append', 'directories-pro' ),
						),
						'#states'       => array(
							'visible' => array(
								'input[name="Payment[plan][allow_none]"]' => array(
									'type'  => 'checked',
									'value' => true,
								),
							),
						),
						'#horizontal'   => true,
					),
				),
				'expiring_days'     => array(
					'#title'         => __( 'Renewal period in days', 'directories-pro' ),
					'#type'          => 'slider',
					'#integer'       => true,
					'#min_value'     => 0,
					'#max_value'     => 100,
					'#default_value' => isset( $config['plan']['expiring_days'] )
						? $config['plan']['expiring_days']
						: 7,
					'#horizontal'    => true,
				),
				'grace_period_days' => array(
					'#title'         => __( 'Renewal grace period in days', 'directories-pro' ),
					'#type'          => 'slider',
					'#integer'       => true,
					'#min_value'     => 0,
					'#max_value'     => 100,
					'#default_value' => isset( $config['plan']['grace_period_days'] )
						? $config['plan']['grace_period_days']
						: 30,
					'#horizontal'    => true,
				),
				'reactivation_fee'  => array(
					'#title'         => sprintf( _x( 'Re-activation fee (%s)', 'currency symbol', 'directories-pro' ), $currency_symbol ),
					'#type'          => 'number',
					'#numeric'       => true,
					'#min_value'     => 0,
					'#default_value' => isset( $config['plan']['reactivation_fee'] ) ? $config['plan']['reactivation_fee'] : 0,
					'#horizontal'    => true,
				),
				'switch_plan_fee'   => array(
					'#title'         => sprintf( _x( 'Switch plan fee (%s)', 'currency symbol', 'directories-pro' ), $currency_symbol ),
					'#type'          => 'number',
					'#numeric'       => true,
					'#min_value'     => 0,
					'#default_value' => isset( $config['plan']['switch_plan_fee'] ) ? $config['plan']['switch_plan_fee'] : 0,
					'#horizontal'    => true,
				),
				'prorated_discount' => array(
					'#title'         => __( 'Enable prorated discount', 'directories-pro' ),
					'#type'          => 'checkbox',
					'#default_value' => ! empty( $config['plan']['prorated_discount'] ),
					'#horizontal'    => true,
				),
			),
		);
	}
}
