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

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

class LevelHelper {

	public function settingsForm( Application $application, Bundle $bundle, $levelsKey = '_entity_levels' ) {
		$application->getPlatform()->addJsFile(
			'entity-admin-feature-levels.min.js',
			'drts-entity-admin-feature-levels',
			'drts',
			'directories-pro'
		);
		$application->Form_Scripts( array( 'latinise', 'slider' ) );

		$levels = empty( $bundle->info['entity_levels'] ) ? array() : $bundle->info['entity_levels'];
		if ( ! isset( $levels['default'] ) ) {
			$levels['default'] = array(
				'label' => __( 'Default', 'directories-pro' ),
			);
		}
		uksort(
			$levels,
			function ( $a, $b ) use ( $levels ) {
				if ( $a === 'default' ) {
					return -1;
				}
				if ( $b === 'default' ) {
					return 1;
				}
				return strcasecmp( $levels[ $a ], $levels[ $b ] );
			}
		);
		$add_path    = '/directories/' . $bundle->group . '/content_types/' . $bundle->name . '/add_level';
		$edit_link   = '<span class="' . DRTS_BS_PREFIX . 'btn ' . DRTS_BS_PREFIX . 'btn-sm ' . DRTS_BS_PREFIX . 'btn-outline-secondary drts-directorypro-edit-feature-level"><i class="fas fa-cog fa-fw"></i></span>';
		$delete_link = '<span class="' . DRTS_BS_PREFIX . 'btn ' . DRTS_BS_PREFIX . 'btn-sm ' . DRTS_BS_PREFIX . 'btn-outline-danger drts-directorypro-delete-feature-level"><i class="fas fa-trash fa-fw"></i></span>';
		foreach ( $levels as $level_name => $level ) {
			$links = array( $edit_link );
			if ( $level_name !== 'default' ) {
				$links[] = $delete_link;
			}
			$levels[ $level_name ] = array(
				'name'  => '<span class="">' . $level_name . '</span>',
				'label' => $level['label'],
				'links' => implode( PHP_EOL, $links ),
			);
		}

		return array(
			'entity_level' => array(
				'#title'         => __( 'Enable feature levels', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#horizontal'    => true,
				'#default_value' => ! empty( $bundle->info['entity_level'] ),
			),
			$levelsKey     => array(
				'#title'      => __( 'Feature levels', 'directories-pro' ),
				'#horizontal' => true,
				'#states'     => array(
					'visible' => array(
						'input[name="entity_level"]' => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
				'#tree'       => true,
				0             => array(
					'#type'              => 'grid',
					'#size'              => 'sm',
					'#class'             => 'drts-data-table',
					'#id'                => '__FORM_ID__-entity-levels',
					'#js_ready'          => 'DRTS.Entity.adminFeatureLevels("#__FORM_ID__-entity-levels", {
                        adminUrl: "' . (string) $application->Url( $add_path, array( 'field_id' => '__FORM_ID__-entity-levels' ), '', '&' ) . '"
                    });',
					'#element_validate'  => array(
						function ( Form $form, &$value, $element ) use ( $levelsKey ) {
							foreach ( array_keys( $value ) as $level_name ) {
								if ( ! isset( $_POST[ $levelsKey ][0][ $level_name ] ) ) {
									// was removed
									unset( $value[ $level_name ] );
									continue;
								}

								foreach ( array( 'label' ) as $key ) {
									if ( empty( $value[ $level_name ][ $key ] )
										|| ! strlen( $value[ $level_name ][ $key ] = trim( $value[ $level_name ][ $key ] ) )
									) {
										$form->setError( sprintf( 'Label for feature level (%s) may not be empty.', $level_name ), $element );
										return;
									}
								}
							}
						},
					),
					'label'              => array(
						'#type'  => 'textfield',
						'#title' => __( 'Label', 'directories-pro' ),
					),
					'name'               => array(
						'#type'  => 'markup',
						'#title' => _x( 'Name', 'feature level name', 'directories-pro' ),
					),
					'links'              => array(
						'#type'  => 'markup',
						'#title' => '',
					),
					'#column_attributes' => array(
						'label' => array( 'style' => 'width:50%;' ),
						'name'  => array( 'style' => 'width:30%;' ),
						'links' => array( 'style' => 'width:20%;' ),
					),
					'#row_attributes'    => array(
						'@all' => array(
							'links' => array(
								'style' => 'text-align:' . $application->getPlatform()->isRtl() ? 'left;' : 'right;',
							),
						),
					),
					'#default_value'     => $levels,
					'#default_markup'    => $levels,
				),
				'_add'        => array(
					'#type'   => 'markup',
					'#markup' => $application->LinkTo(
						'<i class="fas fa-plus fa-fw"></i> ' . $application->H( __( 'Add Feature Level', 'directories-pro' ) ),
						$application->Url( $add_path, array( 'field_id' => '__FORM_ID__-entity-levels' ) ),
						array(
							'container' => 'modal',
							'modalSize' => 'xl',
							'no_escape' => true,
						),
						array( 'class' => DRTS_BS_PREFIX . 'btn ' . DRTS_BS_PREFIX . 'btn-sm ' . DRTS_BS_PREFIX . 'btn-outline-secondary ' . DRTS_BS_PREFIX . 'mt-2' )
					),
				),
			),
		);
	}
}
