<?php
namespace SabaiApps\Directories\Component\Entity\Controller\Admin;

use SabaiApps\Directories\Context;
use SabaiApps\Directories\Component\Form;

class AddLevel extends Form\AbstractMultiStepController {

	protected function _getSteps( Context $context, array &$formStorage ) {
		return array(
			'general_settings' => array( 'order' => 3 ),
			'level_features'   => array( 'order' => 5 ),
		);
	}

	public function _getFormForStepGeneralSettings( Context $context, array &$formStorage ) {
		$form = array(
			'label'    => array(
				'#type'       => 'textfield',
				'#title'      => __( 'Label', 'directories-pro' ),
				'#horizontal' => true,
				'#required'   => true,
			),
			'name'     => array(
				'#type'       => 'textfield',
				'#title'      => _x( 'Name', 'taxonomy name', 'directories-pro' ),
				'#horizontal' => true,
			),
			'field_id' => array(
				'#type'          => 'hidden',
				'#default_value' => $context->getRequest()->asStr( 'field_id' ),
			),
		);
		if ( isset( $context->level ) ) {
			$form['label']['#default_value'] = $context->level_data['label'];
			$form['name']['#default_value']  = $context->level;
			$form['name']['#disabled']       = true;
			$form['level']                   = array(
				'#type'          => 'hidden',
				'#default_value' => $context->level,
			);
		} else {
			$form['name'] += array(
				'#required'            => true,
				'#regex'               => '/^[a-z0-9]([a-z0-9_]*[a-z0-9])?$/',
				'#max_length'          => 15,
				'#regex_error_message' => __( 'Name must start with an alphabet, may contain alphanumeric and underscore characters only, and may not end with an underscore.', 'directories-pro' ),
				'#element_validate'    => array(
					function ( Form\Form $form, &$value, $element ) {
						if ( $value === 'default'
						|| isset( $context->bundle->info['entity_levels'][ $value ] )
								) {
								$form->setError( 'Level name already exists.', $element );
								return;
						}
					},
				),
				'#states'              => array(
					'slugify' => array(
						'input[name="label"]' => array(
							'type'  => 'filled',
							'value' => true,
						),
					),
				),
			);
		}

		return $form;
	}

	public function _getFormForStepLevelFeatures( Context $context, array &$formStorage ) {
		$js   = array( 'function (result, target, trigger) {' );
		$js[] = 'if (target.attr("id") === "drts-modal") {
        target.find(".drts-modal-close").click();
    } else {
        target.hide();
    }';
		if ( isset( $context->level ) ) {
			$settings = array();
			if ( ! empty( $context->bundle->info['entity_levels'][ $context->level ]['features'] ) ) {
				$features = $context->bundle->info['entity_levels'][ $context->level ]['features'];
				$settings = (array) $features['enabled'] + (array) $features['disabled'];
			}
			$btn_label = __( 'Save Changes', 'directories-pro' );
			$btn_color = 'primary';
		} else {
			$settings  = array();
			$btn_label = __( 'Add Feature Level', 'directories-pro' );
			$btn_color = 'success';
			$js[]      = 'DRTS.Entity.onAddFeatureLevel("#' . $this->H( $context->getRequest()->asStr( 'field_id' ) ) . '", result);';
		}
		$js[]                         = '}';
		$this->_ajaxOnSuccess         = implode( PHP_EOL, $js );
		$this->_ajaxOnSuccessRedirect = $this->_ajaxOnErrorRedirect = false;
		$this->_submitButtons[ $context->currentStep ] = array(
			'#btn_label' => $btn_label,
			'#btn_color' => $btn_color,
			'#btn_size'  => 'lg',
		);
		$form = array();
		if ( isset( $context->level ) ) {
			$form['level'] = array(
				'#type'          => 'hidden',
				'#default_value' => $context->level,
			);
		}
		$form['features'] = $this->Entity_Features_form( $context->bundle, $settings, array( 'features' ) );
		return $form;
	}

	protected function _complete( Context $context, array $formStorage ) {
		$info        = $context->bundle->info;
		$level_name  = isset( $context->level ) ? $context->level : $formStorage['values']['general_settings']['name'];
		$level_label = $formStorage['values']['general_settings']['label'];
		$info['entity_levels'][ $level_name ]['label']    = $level_label;
		$info['entity_levels'][ $level_name ]['features'] = $formStorage['values']['level_features']['features'];
		$context->bundle->setInfo( $info )->commit();
		$context->setSuccess(
			null,
			array(
				'name'  => $level_name,
				'label' => $level_label,
			)
		);
	}
}
