<?php
namespace SabaiApps\Directories\Component\Field\Renderer;

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

class VideoThumbnailRenderer extends ImageRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'label'       => __( 'Video Thumbnail', 'directories-pro' ),
			'field_types' => array( 'video' ),
		) + parent::_fieldRendererInfo();
	}

	public function fieldRendererSettingsForm( IField $field, array $settings, array $parents = array() ) {
		$form         = parent::fieldRendererSettingsForm( $field, $settings, $parents );
		$form['size'] = array(
			'#type'  => 'hidden',
			'#value' => 'thumbnail',
		);
		unset( $form['link_image_size'] );
		return $form;
	}

	protected function _getImageLinkTypeOptions( Bundle $bundle ) {
		$options          = parent::_getImageLinkTypeOptions( $bundle );
		$options['video'] = __( 'Video', 'directories-pro' );
		return $options;
	}

	protected function _getImageLinkTargetableOptions() {
		$options   = parent::_getImageLinkTargetableOptions();
		$options[] = 'video';
		return $options;
	}

	protected function _getImageLinkUrl( IField $field, array $settings, $value, $permalinkUrl, $imageUrl ) {
		if ( $settings['link'] === 'video' ) {
			switch ( $value['provider'] ) {
				case 'vimeo':
					return sprintf( '//vimeo.com/%1$s', $this->_application->H( $value['id'] ) );
				case 'youtube':
					return sprintf( '//www.youtube.com/watch?v=%1$s', $this->_application->H( $value['id'] ) );
				default:
					return;
			}
		}

		return parent::_getImageLinkUrl( $field, $settings, $value, $permalinkUrl, $imageUrl );
	}

	protected function _getImageUrl( IField $field, array $settings, $value, $size ) {
		return $value['thumbnail_url'];
	}

	protected function _getImageAlt( IField $field, array $settings, $value ) {
		return $value['title'];
	}

	protected function _getImageTitle( IField $field, array $settings, $value ) {
		return $value['title'];
	}

	protected function _fieldRendererReadableSettings( IField $field, array $settings ) {
		$settings = parent::_fieldRendererReadableSettings( $field, $settings );
		unset( $settings['link_image_size'] );
		return $settings;
	}
}
