<?php
namespace SabaiApps\Directories\Component\WordPressContent\FieldRenderer;

use SabaiApps\Directories\Component\Field;
use SabaiApps\Directories\Component\Entity;

class GalleryFieldRenderer extends Field\Renderer\AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'label'            => _x( 'Gallery', 'field renderer', 'directories-pro' ),
			'field_types'      => array( 'wp_image' ),
			'default_settings' => array(
				'cols'    => 4,
				'size'    => 'thumbnail',
				'no_link' => false,
			),
			'separatable'      => false,
		);
	}

	protected function _fieldRendererSettingsForm( Field\IField $field, array $settings, array $parents = array() ) {
		return array(
			'cols'    => array(
				'#title'         => __( 'Number of columns', 'directories-pro' ),
				'#type'          => 'select',
				'#options'       => array(
					0  => __( 'Do not specify', 'directories-pro' ),
					1  => 1,
					2  => 2,
					3  => 3,
					4  => 4,
					6  => 6,
					12 => 12,
				),
				'#default_value' => $settings['cols'],
			),
			'size'    => array(
				'#title'         => __( 'Image size', 'directories-pro' ),
				'#type'          => 'select',
				'#options'       => array( '' => __( 'Do not specify', 'directories-pro' ) ) + $this->_getImageSizeOptions(),
				'#default_value' => $settings['size'],
			),
			'no_link' => array(
				'#title'         => __( 'Do not link', 'directories-pro' ),
				'#type'          => 'checkbox',
				'#default_value' => ! empty( $settings['no_link'] ),
			),
		);
	}

	protected function _fieldRendererRenderField( Field\IField $field, array &$settings, Entity\Type\IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		if ( $is_post = $field->Bundle->entitytype_name === 'post' ) {
			$post = $entity->post();
			setup_postdata( $post );
			$GLOBALS['post'] = $post;
		}
		$size = $settings['size'];
		if ( in_array( $size, array( 'thumbnail', 'thumbnail_scaled' ) ) ) {
			$size = 'drts_' . $size;
		}
		$attachment_ids = array();
		foreach ( $values as $value ) {
			$attachment_ids[] = $value['attachment_id'];
		}
		$ret = do_shortcode(
			sprintf(
				'[gallery%s%s" ids="%s" link="%s"]',
				( $cols = intval( $settings['cols'] ) ) ? ' columns="' . $cols . '"' : '',
				$size ? ' size="' . $size . '"' : '',
				implode( ',', $attachment_ids ),
				empty( $settings['no_link'] ) ? 'file' : 'none'
			)
		);
		if ( $is_post ) {
			wp_reset_postdata();
		}

		return $ret;
	}

	protected function _fieldRendererReadableSettings( Field\IField $field, array $settings ) {
		return array(
			'cols'    => array(
				'label' => __( 'Number of columns', 'directories-pro' ),
				'value' => empty( $settings['cols'] ) ? __( 'Do not specify', 'directories-pro' ) : $settings['cols'],
			),
			'size'    => array(
				'label' => __( 'Image size', 'directories-pro' ),
				'value' => $settings['size'] === '' ? __( 'Do not specify', 'directories-pro' ) : $this->_getImageSizeOptions()[ $settings['size'] ],
			),
			'no_link' => array(
				'label'   => __( 'Do not link', 'directories-pro' ),
				'value'   => ! empty( $settings['no_link'] ),
				'is_bool' => true,
			),
		);
	}
}
