<?php
namespace SabaiApps\Directories\Component\View\Mode;

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

class ListMode extends AbstractMode {

	protected function _viewModeInfo() {
		return array(
			'label'            => _x( 'List', 'view mode label', 'directories-pro' ),
			'default_settings' => array(
				'template'                  => 'view_entities_list',
				'display'                   => 'summary',
				'list_grid'                 => true,
				'list_no_row'               => false,
				'list_grid_cols'            => array(
					'num'            => 'responsive',
					'num_responsive' => array(
						'xs'  => 1,
						'sm'  => 2,
						'md'  => 3,
						'lg'  => 4,
						'xxl' => 6,
					),
				),
				'list_grid_gutter_width'    => 'sm',
				'list_layout_switch_cookie' => 'drts-entity-view-list-layout',
				'list_grid_default'         => false,
			),
			'default_display'  => 'summary',
			'displays'         => array(
				'summary' => array(
					'label'  => _x( 'Summary', 'display name', 'directories-pro' ),
					'weight' => 1,
				),
			),
			'mapable'          => true,
		);
	}

	public function viewModeSupports( Bundle $bundle ) {
		return parent::viewModeSupports( $bundle ) && ! empty( $bundle->info['public'] );
	}

	public function viewModeSettingsForm( Bundle $bundle, array $settings, array $parents = array() ) {
		$list_grid_selector = sprintf( 'input[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'list_grid' ) ) ) );
		return array(
			'list_grid'              => array(
				'#title'         => __( 'Enable grid layout', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#default_value' => ! empty( $settings['list_grid'] ),
				'#horizontal'    => true,
			),
			'list_no_row'            => array(
				'#title'         => __( 'Disable row layout', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#default_value' => ! empty( $settings['list_no_row'] ),
				'#horizontal'    => true,
				'#states'        => array(
					'visible' => array(
						$list_grid_selector => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
			),
			'list_grid_default'      => array(
				'#title'         => __( 'Set grid layout as default', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#default_value' => ! empty( $settings['list_grid_default'] ),
				'#horizontal'    => true,
				'#states'        => array(
					'visible' => array(
						$list_grid_selector => array(
							'type'  => 'checked',
							'value' => true,
						),
						sprintf( 'input[name="%s"]', $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'list_no_row' ) ) ) ) => array(
							'type'  => 'checked',
							'value' => false,
						),

					),
				),
			),
			'list_grid_cols'         => $this->_getGridColumnSettingsForm( $settings['list_grid_cols'], array_merge( $parents, array( 'list_grid_cols' ) ), __( 'Grid layout columns', 'directories-pro' ), 'num' ) + array(
				'#states' => array(
					'visible' => array(
						$list_grid_selector => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
			),
			'list_grid_gutter_width' => array(
				'#title'         => __( 'Gutter width', 'directories-pro' ),
				'#type'          => 'select',
				'#default_value' => empty( $settings['list_grid_gutter_width'] ) ? 'sm' : $settings['list_grid_gutter_width'],
				'#options'       => array(
					'none' => __( 'None', 'directories-pro' ),
					'xs'   => __( 'Small', 'directories-pro' ),
					'sm'   => __( 'Default', 'directories-pro' ),
					'md'   => __( 'Medium', 'directories-pro' ),
					'lg'   => __( 'Large', 'directories-pro' ),
				),
				'#horizontal'    => true,
			),
		);
	}

	public function viewModeNav( Bundle $bundle, array $settings ) {
		return array(
			array(
				array( array( 'filters' ) ),
				array( array( 'search', 'filter', 'num' ), array(), array( 'layout_switch', 'sort', 'add' ) ),
			), // header
			array(
				array( array(), array(), array() ),
				array( array(), array( 'load_more' ), array( 'perpages', 'pagination' ) ),
			), // footer
		);
	}

	public function getGridClass( $cols, $dw = false ) {
		$prefix = $dw ? 'drts-dw-' : 'drts-col-';
		if ( ! is_array( $cols ) ) {
			return $prefix . intval( 12 / $cols );
		}

		$classes = array();
		if ( isset( $cols['xs'] ) ) {
			$_size = 12 / $cols['xs'];
			unset( $cols['xs'] );
		} else {
			$_size = 12;
		}
		$classes[] = $prefix . $_size;
		foreach ( array_keys( $cols ) as $_width ) {
			if ( ! is_numeric( $cols[ $_width ] ) ) {
				continue;
			}

			$classes[] = $prefix . $_width . '-' . intval( 12 / $cols[ $_width ] );
		}
		return implode( ' ', $classes );
	}
}
