<?php
namespace SabaiApps\Directories\Component\Search\WordPressBlock;

use SabaiApps\Directories\Component\WordPress\Block\AbstractBlock;
use SabaiApps\Directories\Exception\RuntimeException;

class SearchWordPressBlock extends AbstractBlock {

	protected function _wordpressBlockInfo() {
		return array(
			'title'       => _x( 'Search Form', 'WordPress block name', 'directories-pro' ),
			'description' => __( 'Renders a search form for your directory.', 'directories-pro' ),
			'package'     => 'directories-pro',
			'icon'        => 'search',
		);
	}

	public function wordpressBlockEditorVars() {
		$bundles       = array(
			array(
				'value' => 'auto',
				'label' => __( 'Auto-detect', 'directories-pro' ),
			),
		);
		$extra_bundles = array();
		foreach ( $this->_application->Entity_Bundles() as $bundle ) {
			if ( ! empty( $bundle->info['search_enable'] ) ) {
				$bundles[] = $extra_bundles[] = array(
					'value' => $bundle->name,
					'label' => empty( $bundle->info['is_primary'] ) ? $bundle->getGroupLabel() . ' - ' . $bundle->getLabel() : $bundle->getGroupLabel(),
				);
			}
		}

		return array(
			'bundles'       => $bundles,
			'extra_bundles' => $extra_bundles,
			'colors'        => $this->_toSelectControlOptions( $this->_getColors() ),
			'sizes'         => $this->_toSelectControlOptions( $this->_getSizes() ),
			'responsives'   => $this->_toSelectControlOptions( $this->_getResponsives() ),
			'bgcolors'      => $this->_toSelectControlOptions( $this->_getBgColors() ),
		);
	}

	protected function _getColors() {
		return array(
			'primary'   => 'Primary',
			'secondary' => 'Secondary',
			'success'   => 'Success',
			'warning'   => 'Warning',
			'danger'    => 'Danger',
			'info'      => 'Info',
			'dark'      => 'Dark',
			'light'     => 'Light',
		);
	}

	protected function _getBgColors() {
		return array(
			''      => __( 'None', 'directories-pro' ),
			'dark'  => 'Dark',
			'light' => 'Light',
		);
	}

	protected function _getSizes() {
		return array(
			''   => __( 'Normal', 'directories-pro' ),
			'lg' => __( 'Large', 'directories-pro' ),
		);
	}

	protected function _getResponsives() {
		return array(
			1  => __( 'Responsive', 'directories-pro' ),
			-1 => __( 'Display inline', 'directories-pro' ),
			0  => __( 'Stacked', 'directories-pro' ),
		);
	}

	public function wordpressBlockRender( array $attributes, $content, $isBlockEditorRequest ) {
		if ( empty( $attributes['bundle'] ) ) {
			throw new RuntimeException( 'No bundle specified.' );
		}
		if ( $attributes['bundle'] === 'auto' ) {
			if ( $isBlockEditorRequest ) {
				return;
			}

			if ( $directory_name = $this->_getCurrentDirectory() ) {
				foreach ( $this->_application->Entity_Bundles( null, $directory_name ) as $_bundle ) {
					if ( ! empty( $_bundle->info['search_enable'] ) ) {
						$bundle = $_bundle;
						break;
					}
				}
			}
			if ( empty( $bundle ) ) {
				return '';
			}
		} elseif ( ( ! $bundle = $this->_application->Entity_Bundle( $attributes['bundle'] ) )
				|| empty( $bundle->info['search_enable'] )
			) {

				throw new RuntimeException( 'Invalid bundle specified: ' . $attributes['bundle'] );
		}

		$settings = array();
		if ( isset( $attributes['btnText'] ) ) {
			if ( '' === $settings['btn_text'] = trim( $attributes['btnText'] ) ) {
				$settings['btn_icon'] = 'fas fa-magnifying-glass';
			}
		} else {
			$settings['btn_text'] = '';
			$settings['btn_icon'] = 'fas fa-magnifying-glass';
		}
		$colors = $this->_getColors();
		foreach ( array(
			'btnColor'     => 'btn_color',
			'navLinkColor' => 'nav_link_color',
		) as $attr_key => $setting_key ) {
			if ( ! empty( $attributes[ $attr_key ] )
				&& isset( $colors[ $attributes[ $attr_key ] ] )
			) {
				$settings[ $setting_key ] = $attributes[ $attr_key ];
			}
		}
		$bgcolors = $this->_getBgColors();
		foreach ( array(
			'bgColor'     => 'bg_color',
			'shadowColor' => 'shadow_color',
		) as $attr_key => $setting_key ) {
			if ( ! empty( $attributes[ $attr_key ] ) ) {
				if ( isset( $bgcolors[ $attributes[ $attr_key ] ] ) ) {
					$settings[ $setting_key ] = $attributes[ $attr_key ];
				}
			}
		}

		if ( isset( $attributes['size'] )
			&& ( '' !== $size = trim( $attributes['size'] ) )
		) {
			$sizes = $this->_getSizes();
			if ( isset( $sizes[ $size ] ) ) {
				$settings['size'] = $size;
			}
		}
		if ( isset( $attributes['responsive'] )
			&& is_numeric( $attributes['responsive'] )
		) {
			$responsive         = intval( $attributes['responsive'] );
			$responsive_options = $this->_getResponsives();
			if ( isset( $responsive_options[ $responsive ] ) ) {
				$settings['responsive'] = $responsive;
			}
		}
		$settings['filter']     = empty( $attributes['filter'] ) ? 0 : 1;
		$settings['delay_show'] = empty( $attributes['delayShow'] ) ? 0 : 1;
		if ( isset( $attributes['style'] )
			&& ( '' !== $style = trim( $attributes['style'] ) )
		) {
			$settings['style'] = $style;
		}

		if ( isset( $attributes['cacheLifetime'] )
			&& ( $attributes['cacheLifetime'] = intval( $attributes['cacheLifetime'] ) )
		) {
			$settings['cache'] = $attributes['cacheLifetime'];
		}

		$directories = array( $bundle->group => $bundle->group );
		if ( ! empty( $attributes['extraBundles'] )
			&& is_array( $attributes['extraBundles'] )
		) {
			foreach ( $attributes['extraBundles'] as $extra_bundle_name ) {
				if ( ( ! $extra_bundle = $this->_application->Entity_Bundle( $extra_bundle_name ) )
					|| empty( $extra_bundle->info['search_enable'] )
				) {
					continue;
				}

				$directories[ $extra_bundle->group ] = $extra_bundle->group;
			}
		}
		$settings['directory'] = implode( ',', $directories );

		$shortcode = sprintf( '[drts-directory-search%s]', $this->_application->Attr( $settings ) );

		return do_shortcode( $shortcode );
	}
}
