<?php
namespace SabaiApps\Directories\Component\Location\EntityFeature;

use SabaiApps\Directories\Component\Entity\Feature\AbstractFeature;
use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class LocationsEntityFeature extends AbstractFeature {

	protected function _entityFeatureInfo() {
		return array(
			'label'            => __( 'Location Settings', 'directories-pro' ),
			'weight'           => 7,
			'default_settings' => array(
				'unlimited' => false,
				'num'       => 1,
			),
		);
	}

	public function entityFeatureSupports( Bundle $bundle, $planType = 'base' ) {
		return ! empty( $bundle->info['location_enable'] );
	}

	public function entityFeatureSettingsForm( Bundle $bundle, array $settings, array $parents = array() ) {
		$label = $this->_application->Entity_Bundle( 'location_location', $bundle->group )->getLabel();
		return array(
			'unlimited' => array(
				'#title'         => $this->_maxNumAllowedLabel( $label ),
				'#on_label'      => __( 'Unlimited', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#switch'        => false,
				'#default_value' => ! empty( $settings['unlimited'] ),
				'#horizontal'    => true,
			),
			'num'       => array(
				'#type'          => 'slider',
				'#default_value' => isset( $settings['num'] ) ? $settings['num'] : 1,
				'#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 entityFeatureOnEntityForm( Bundle $bundle, array $settings, array &$form, ?IEntity $entity = null, $isAdmin = false ) {
		if ( $isAdmin && $this->_application->IsAdministrator() ) {
			return; // do not restrict for administrators
		}

		if ( empty( $bundle->info['location_enable'] ) ) {
			return;
		}

		if ( ! isset( $form['location_address'][0] ) ) {
			return; // Field does not exist
		}

		if ( empty( $settings['unlimited'] ) ) {
			$limit = $settings['num'];

			if ( empty( $limit ) ) {
				// No locations allowed
				unset( $form['location_address'] );
				return;
			}

			if ( isset( $form['#entity_field_max_num_items']['location_address'] ) ) {
				if ( empty( $form['#entity_field_max_num_items']['location_address'] )
					|| $form['#entity_field_max_num_items']['location_address'] > $limit
				) {
					$form['#entity_field_max_num_items']['location_address'] = $limit;
				} else {
					return;
				}
			} else {
				$form['#entity_field_max_num_items']['location_address'] = $limit;
			}

			// Remove fields over limit
			$current_num = 0;
			foreach ( array_keys( $form['location_address'] ) as $key ) {
				if ( is_numeric( $key ) ) {
					++$current_num;
					if ( $current_num > $limit ) {
						// over limit
						unset( $form['location_address'][ $key ] );
					}
				}
			}
			if ( $limit > 1 ) {
				// Add add more button
				$form['location_address']['_add'] = array(
					'#type'       => 'addmore',
					'#next_index' => $current_num,
					'#max_num'    => $limit,
					'#hidden'     => $current_num >= $limit,
				);
			} else {
				// May already be added if max number of values for the field is > 1
				unset( $form['location_address']['_add'] );
			}
		} else {
			if ( ! isset( $form['#entity_field_max_num_items']['location_address'] ) ) {
				$form['#entity_field_max_num_items']['location_address'] = 0;
			} elseif ( $form['#entity_field_max_num_items']['location_address'] !== 0 ) {
					return;
			}

			$current_num = 0;
			foreach ( array_keys( $form['location_address'] ) as $key ) {
				if ( is_numeric( $key ) ) {
					++$current_num;
				}
			}
			// Add add more button
			$form['location_address']['_add'] = array(
				'#type'       => 'addmore',
				'#next_index' => $current_num,
			);
		}
	}
}
