<?php
namespace SabaiApps\Directories\Component\DirectoryPro\PaymentFeature;

use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Payment;

class PhotosPaymentFeature extends Payment\Feature\AbstractFeature implements Payment\Feature\IAddonFeature {

	protected function _paymentFeatureInfo() {
		return array(
			'label'            => __( 'Photo Settings', 'directories-pro' ),
			'weight'           => 9,
			'default_settings' => array(
				'unlimited' => false,
				'num'       => 5,
			),
		);
	}

	public function paymentFeatureSupports( Bundle $bundle, $planType = 'base' ) {
		return $bundle->type === 'directory__listing';
	}

	public function paymentFeatureSettingsForm( Bundle $bundle, array $settings, $planType = 'base', array $parents = array() ) {
		return array(
			'unlimited' => array(
				'#title'         => $this->_maxNumAllowedLabel( __( 'Photos', 'directories-pro' ) ),
				'#on_label'      => __( 'Unlimited', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#switch'        => false,
				'#default_value' => ! empty( $settings['unlimited'] ),
				'#horizontal'    => true,
			),
			'num'       => array(
				'#type'          => 'slider',
				'#default_value' => $settings['num'],
				'#integer'       => true,
				'#min_value'     => 0,
				'#max_value'     => 50,
				'#states'        => array(
					'invisible' => array(
						sprintf( 'input[name="%s[unlimited][]"]', $this->_application->Form_Form_fieldName( $parents ) ) => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
				'#horizontal'    => true,
			),
		);
	}

	public function paymentFeatureApply( IEntity $entity, $featureName, array $featureSettings, array &$values, $orderId, $isAddon = false ) {
		if ( ! $isAddon ) {
			return;
		}

		return $this->_applyAddonFeature( $entity, $featureName, $featureSettings, $values, $orderId );
	}

	public function paymentFeatureUnapply( IEntity $entity, $featureName, array $featureSettings, array &$values, $orderId, $isAddon = false ) {
		if ( ! $isAddon ) {
			return;
		}

		return $this->_unapplyAddonFeature( $entity, $featureName, $featureSettings, $values, $orderId );
	}

	public function paymentFeatureOnEntityForm( Bundle $bundle, array $settings, array &$form, ?IEntity $entity = null, $isAdmin = false ) {
		if ( $isAdmin && $this->_application->IsAdministrator() ) {
			return; // do not restrict for administrators
		}

		if ( empty( $settings['unlimited'] ) ) {
			$limit = $settings['num'];
			if ( empty( $limit ) ) {
				// No photos allowed
				unset( $form['directory_photos'] );
				return;
			}

			if ( empty( $form['directory_photos']['#max_num_files'] )
				|| $limit < $form['directory_photos']['#max_num_files']
			) {
				$form['directory_photos']['#max_num_files'] = $limit;
			}
		} else {
			$form['directory_photos']['#max_num_files'] = 0;
		}
	}

	public function paymentFeatureOnEntityFieldValues( IEntity $entity, Bundle $bundle, array $settings ) {
		if ( $bundle->type !== 'directory__listing'
			|| ( ! $directory_photos = $entity->getFieldValue( 'directory_photos' ) )
		) {
			return;
		}

		if ( ! empty( $settings['unlimited'] ) ) {
			return;
		}

		if ( ! isset( $settings['num'] ) ) {
			$max_num_allowed = 5;
		} else {
			$max_num_allowed = empty( $settings['num'] ) ? 0 : $settings['num'];
		}

		$current_num = count( $directory_photos );
		if ( $current_num <= $max_num_allowed ) {
			return;
		}

		$entity->setFieldValue( 'directory_photos', array_slice( $directory_photos, 0, $max_num_allowed ) );
	}

	public function paymentFeatureRender( Bundle $bundle, array $settings, ?Payment\IPlan $plan = null ) {
		if ( empty( $settings['unlimited'] ) ) {
			$label = sprintf( $this->_application->H( _n( '%s Photo', '%s Photos', $settings['num'], 'directories-pro' ) ), '<em>' . $settings['num'] . '</em>' );
		} else {
			$label = sprintf( $this->_application->H( _n( '%s Photo', '%s Photos', 10, 'directories-pro' ) ), '<em>' . $this->_application->H( __( 'Unlimited', 'directories-pro' ) ) . '</em>' );
		}
		return array(
			array(
				'icon'     => 'fas fa-camera',
				'html'     => $label,
				'settings' => $settings,
			),
		);
	}

	public function paymentAddonFeatureSupports( Bundle $bundle ) {
		return $bundle->type === 'directory__listing';
	}

	protected function _getAddonSettingsForm( Bundle $bundle, array $settings, array $parents = array(), $horizontal = true ) {
		return array(
			'num' => array(
				'#title'         => $this->_additionalNumAllowedLabel( __( 'Photos', 'directories-pro' ) ),
				'#type'          => 'slider',
				'#default_value' => isset( $settings['num'] ) ? $settings['num'] : 0,
				'#integer'       => true,
				'#min_value'     => 0,
				'#max_value'     => 30,
				'#horizontal'    => $horizontal,
			),
		);
	}

	public function paymentAddonFeatureSettingsForm( Bundle $bundle, array $settings, array $parents = array() ) {
		return $this->_getAddonSettingsForm( $bundle, $settings, $parents );
	}

	public function paymentAddonFeatureExtraSettingsForm( Bundle $bundle, array $planFeatures, array $currentExtraFeatures, array $parents = array() ) {
		return $this->_getAddonSettingsForm( $bundle, $currentExtraFeatures, $parents, false );
	}

	public function paymentAddonFeatureIsEnabled( Bundle $bundle, array $settings ) {
		return ! empty( $settings['num'] ) && intval( $settings['num'] ) > 0;
	}

	public function paymentAddonFeatureIsOrderable( array $settings, array $currentFeatures ) {
		return $this->paymentAddonFeatureExtraIsOrderable( $currentFeatures );
	}

	public function paymentAddonFeatureExtraIsOrderable( array $planFeatures ) {
		return empty( $planFeatures[ $this->_name ]['unlimited'] );
	}

	public function paymentAddonFeatureExtraApply( $feature, $extra ) {
		if ( empty( $feature['unlimited'] )
			&& ! empty( $extra['num'] )
		) {
			$feature['num'] += $extra['num'];
		}
		return $feature;
	}
}
