<?php
namespace SabaiApps\Directories\Component\FrontendSubmit\Restrictor;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Model\Bundle;

abstract class AbstractRestrictor implements IRestrictor {

	protected $_application, $_name, $_info, $_limitMax, $_limitStep, $_assignEntityLevel = true;

	public function __construct( Application $application, $name ) {
		$this->_application = $application;
		$this->_name        = $name;
		if ( $limit_max = (int) $application->Filter( 'frontendsubmit_restrictor_limit_max', null ) ) {
			$this->_limitMax = $limit_max;
			if ( $this->_limitMax >= 200 ) {
				$this->_limitStep = 10;
			} elseif ( $this->_limitMax >= 100 ) {
				$this->_limitStep = 5;
			}
		} else {
			$this->_limitMax  = 50;
			$this->_limitStep = 1;
		}
	}

	public function getName() {
		return $this->_name;
	}

	public function frontendsubmitRestrictorInfo( $key = null ) {
		if ( ! isset( $this->_info ) ) {
			$this->_info = (array) $this->_frontendsubmitRestrictorInfo();
		}

		return isset( $key ) ? ( isset( $this->_info[ $key ] ) ? $this->_info[ $key ] : null ) : $this->_info;
	}

	public function frontendsubmitRestrictorEnabled() {
		return true;
	}

	protected function _maybeFixRestrictorSettings( array $settings ) {
		return $settings;
	}

	public function frontendsubmitRestrictorSettingsForm( array $bundles, array $settings, array $parents = array() ) {
		$form     = array();
		$settings = $this->_maybeFixRestrictorSettings( $settings );

		foreach ( array_keys( $bundles ) as $bundle_name ) {
			if ( ! $bundle = $this->_application->Entity_Bundle( $bundle_name ) ) {
				continue;
			}

			$bundles[ $bundle_name ] = array(
				'label'  => $bundles[ $bundle_name ],
				'parent' => empty( $bundle->info['parent'] ) ? null : $bundle->info['parent'],
			);

			if ( $this->_assignEntityLevel
				&& ( $levels = $this->_application->Entity_Levels_options( $bundle ) )
				&& ( count( $levels ) > 1 || ! empty( $bundle->info['payment_enable'] ) )
			) {
				$bundles[ $bundle_name ]['entity_levels'] = $levels;
				// Add option to not assign any level if payment is enabled
				if ( ! empty( $bundle->info['payment_enable'] ) ) {
					$bundles[ $bundle_name ]['entity_levels'] = array( '' => __( '— Select —', 'directories-pro' ) ) + $bundles[ $bundle_name ]['entity_levels'];
				}
			}
		}

		foreach ( $this->_getRestrictionGroups() as $group_name => $group_label ) {
			$form[ $group_name ] = array(
				'#title' => $group_label,
				'#class' => 'drts-form-label-lg',
			) + $this->_getRestrictorSettingsForm( $group_name, $bundles, empty( $settings[ $group_name ] ) ? array() : $settings[ $group_name ] );
			if ( ! empty( $form[ $group_name ]['limit'] ) ) {
				$form[ $group_name ]['limit'] += array(
					'#title'      => __( 'Max number of submissions allowed', 'directories-pro' ),
					'#weight'     => 1,
					'#horizontal' => true,
				);
			}
			if ( ! empty( $form[ $group_name ]['entity_level'] ) ) {
				$form[ $group_name ]['entity_level'] += array(
					'#title'      => __( 'Assign feature level', 'directories-pro' ),
					'#weight'     => 5,
					'#horizontal' => true,
				);
			}
		}

		return $form;
	}

	protected function _getRestrictorSettingsForm( $group, array $bundles, array $settings ) {
		$form = array();
		foreach ( array_keys( $bundles ) as $bundle_name ) {
			$form['limit'][ $bundle_name ] = array(
				'#type'          => 'slider',
				'#title'         => $bundles[ $bundle_name ]['label'],
				'#default_value' => isset( $settings['limit'][ $bundle_name ] ) ? $settings['limit'][ $bundle_name ] : ( empty( $bundles[ $bundle_name ]['parent'] ) ? -1 : 1 ),
				'#min_value'     => -1,
				'#max_value'     => $this->_limitMax,
				'#step'          => $this->_limitStep,
				'#min_text'      => __( 'Unlimited', 'directories-pro' ),
				'#integer'       => true,
				'#horizontal'    => true,
			);
			if ( ! empty( $bundles[ $bundle_name ]['entity_levels'] ) ) {
				$form['entity_level'][ $bundle_name ] = array(
					'#type'          => 'select',
					'#title'         => $bundles[ $bundle_name ]['label'],
					'#default_value' => empty( $settings['entity_level'][ $bundle_name ] ) ? '' : $settings['entity_level'][ $bundle_name ],
					'#options'       => $bundles[ $bundle_name ]['entity_levels'],
					'#horizontal'    => true,
				);
				if ( isset( $form['entity_level'][ $bundle_name ]['#options'][''] ) ) {
					$form['entity_level'][ $bundle_name ]['#description'] = __( 'Payment features are enabled for this content item. Selecting a feature level will skip the payment process during submission.', 'directories-pro' );
				}
			}
		}
		return $form;
	}

	public function frontendsubmitRestrictorLimit( Bundle $bundle, array $settings, $userId ) {
		$limit = -1; // start with no limit
		foreach ( $this->_getRestrictionGroupsByUser( $userId, $settings ) as $group ) {
			if ( isset( $settings[ $group ]['limit'][ $bundle->name ] )
				&& $settings[ $group ]['limit'][ $bundle->name ] > $limit
			) {
				$limit = $settings[ $group ]['limit'][ $bundle->name ];
			}
		}
		return $limit;
	}

	public function frontendsubmitRestrictorHasEntityLevel( Bundle $bundle, array $settings, $userId ) {
		if ( ! $this->_assignEntityLevel
			// || !empty($bundle->info['payment_enable'])
		) {
			return false;
		}

		foreach ( $this->_getRestrictionGroupsByUser( $userId, $settings ) as $group ) {
			if ( isset( $settings[ $group ]['entity_level'][ $bundle->name ] ) ) {
				return $settings[ $group ]['entity_level'][ $bundle->name ];
			}
		}
		return false;
	}

	abstract protected function _frontendsubmitRestrictorInfo();
	abstract protected function _getRestrictionGroups();
	abstract protected function _getRestrictionGroupsByUser( $userId, $settings );
}
