<?php
namespace SabaiApps\Directories\Component\Comparison\DisplayButton;

use SabaiApps\Directories\Component\Entity\Model\Bundle;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Display\Button\AbstractButton;

class CompareDisplayButton extends AbstractButton {

	protected static $_jsAdded = array();

	protected function _displayButtonInfo( Bundle $bundle ) {
		return array(
			'label'            => __( 'Compare button', 'directories-pro' ),
			'description'      => __( 'Add selected items to comparison list.', 'directories-pro' ),
			'default_settings' => array(
				'_label' => __( 'Compare', 'directories-pro' ),
				'_color' => 'outline-secondary',
				'_icon'  => 'fas fa-arrow-right-arrow-left',
			),
			'labellable'       => false,
			'iconable'         => false,
		);
	}

	public function displayButtonIsPreRenderable( Bundle $bundle, array $settings ) {
		return true;
	}

	public function displayButtonPreRender( Bundle $bundle, array $settings, array $entities = array() ) {
		if ( empty( self::$_jsAdded ) ) {
			$this->_application->getPlatform()
				->addJsFile( 'comparison.min.js', 'drts-comparison', array( 'drts' ), 'directories-pro', true );
		}
		if ( empty( self::$_jsAdded[ $bundle->name ] ) ) {
			$this->_application->getPlatform()
				->addJs(
					sprintf(
						'DRTS.Comparison.Tray.getInstance("%s", %s);',
						$bundle->name,
						$this->_application->System_Util_jsonEncode(
							array(
								'comparePageUrl'           => (string) $this->_application->Url( $bundle->getPath( false ) . '/compare' ),
								'maxCompareItems'          => $bundle->info['comparison']['compare_max_items'] ?? 5,
								'maxItemsMessage'          => sprintf( __( 'You can compare up to %s items.', 'directories-pro' ), '{max}' ),
								'trayTitle'                => $bundle->getLabel( 'compare' ),
								'clearAllLabel'            => __( 'Clear All', 'directories-pro' ),
								'compareButtonLabel'       => __( 'Compare', 'directories-pro' ),
								'emptyListMessage'         => __( 'Please select items to compare.', 'directories-pro' ),
								'removeItemConfirmMessage' => __( 'This item already exists in the comparison list. Do you want to remove it?', 'directories-pro' ),
								'compareOpenNewTab'        => ! empty( $bundle->info['comparison']['compare_open_newtab'] ),
							)
						)
					)
				);
			self::$_jsAdded[ $bundle->name ] = true;
		}
	}

	public function displayButtonLink( Bundle $bundle, IEntity $entity, array $settings, $displayName ) {
		return $this->_application->LinkTo(
			$settings['_label'],
			'#',
			array( 'icon' => $settings['_icon'] ),
			array(
				'class'              => $settings['_class'],
				'style'              => $settings['_style'],
				'data-compare-id'    => $entity->getId(),
				'data-compare-title' => $entity->getTitle(),
				'data-compare-thumb' => $this->_application->Entity_Image( $entity, 'thumbnail' ),
				'data-compare-name'  => $bundle->name,
			)
		);
	}
}
