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

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

class MasonryMode extends ListMode {

	protected function _viewModeInfo() {
		return array(
			'label'            => _x( 'Masonry', 'view mode label', 'directories-pro' ),
			'default_settings' => array(
				'template'   => 'view_entities_masonry',
				'display'    => 'summary',
				'no_js'      => false,
				'no_js_grid' => array(
					'masonry_cols'            => 'responsive',
					'masonry_cols_responsive' => array(
						'xs' => 2,
						'lg' => 3,
						'xl' => 4,
					),
				),
				'js_grid'    => array(
					'masonry_cols' => 3,
				),
			),
		) + parent::_viewModeInfo();
	}

	public function viewModeAssets( Bundle $bundle, array $settings ) {
		if ( ! empty( $settings['no_js'] )
			|| isset( $settings['masonry_cols'] ) // compat with <1.3.12
		) {
			return array(
				'css_files' => array(
					'driveway' => array( 'driveway.min.css', null, null, null, true ),
				),
			);
		}

		return array(
			'js_files'         => array(
				'masonry'           => array( 'masonry.pkgd.min.js', array(), 'directories-pro', true, true ),
				'drts-view-masonry' => array( 'view-masonry.min.js', array( 'drts', 'masonry' ), 'directories-pro', true ),
			),
			'images_loaded_js' => true,
		);
	}

	public function viewModeSettingsForm( Bundle $bundle, array $settings, array $parents = array() ) {
		$form = array(
			'no_js'      => array(
				'#title'         => __( 'Do not use JavaScript', 'directories-pro' ),
				'#default_value' => isset( $settings['masonry_cols'] ) ? true : ! empty( $settings['no_js'] ),
				'#type'          => 'checkbox',
				'#horizontal'    => true,
			),
			'no_js_grid' => array(
				'#states' => array(
					'visible' => array(
						sprintf( '[name="%s"]', $no_js_name = $this->_application->Form_Form_fieldName( array_merge( $parents, array( 'no_js' ) ) ) ) => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
			) + $this->_getGridColumnSettingsForm( isset( $settings['masonry_cols'] ) ? $settings : $settings['no_js_grid'], array_merge( $parents, array( 'no_js_grid' ) ), null, 'masonry_cols' ),
			'js_grid'    => array(
				'#states' => array(
					'visible' => array(
						sprintf( '[name="%s"]', $no_js_name ) => array(
							'type'  => 'checked',
							'value' => false,
						),
					),
				),
			) + $this->_getGridColumnSettingsForm( $settings['js_grid'], array_merge( $parents, array( 'js_grid' ) ), null, 'masonry_cols', array( 2, 3, 4, 5, 6 ), false ),
		);

		return $form;
	}

	public function getNoJsGridClass( Bundle $bundle, array $settings ) {
		$settings = isset( $settings['no_js_grid'] ) ? $settings['no_js_grid'] : $settings; // compat with <1.3.12
		if ( isset( $settings['masonry_cols'] ) ) {
			if ( $settings['masonry_cols'] === 'responsive' ) {
				if ( ! empty( $settings['masonry_cols_responsive'] ) ) {
					$cols = $settings['masonry_cols_responsive'];
				}
			} else {
				$cols = $settings['masonry_cols'];
			}
		}
		if ( ! isset( $cols )
			&& ( ! $cols = $this->_application->Entity_BundleTypeInfo( $bundle, 'view_masonry_cols' ) )
		) {
			$cols = empty( $bundle->info['is_taxonomy'] ) ? array(
				'sm' => 1,
				'md' => 2,
				'lg' => 3,
				'xl' => 4,
			) : array(
				'xs' => 2,
				'lg' => 3,
				'xl' => 4,
			);
		}
		return $this->getGridClass( $cols, true );
	}
}
