<?php
namespace SabaiApps\Directories\Component\Display\Button;

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

class CustomButton extends AbstractButton {

	protected function _displayButtonInfo( Bundle $bundle ) {
		$info = array(
			'label'            => __( 'Custom buttons', 'directories-pro' ),
			'default_settings' => array(
				'_label'          => '',
				'_color'          => 'secondary',
				'_icon'           => '',
				'link_type'       => 'current',
				'url'             => null,
				'path'            => null,
				'fragment'        => null,
				'link_field'      => null,
				'link_field_qstr' => null,
				'target'          => '',
				'rel'             => null,
			),
			'multiple'         => array(),
			'weight'           => 50,
		);
		foreach ( $this->_application->Filter( 'entity_button_custom_button_num', range( 1, 3 ), array( $bundle ) ) as $num ) {
			$info['multiple'][ $num ] = array(
				'default_checked' => $num === 1,
				'label'           => sprintf( __( 'Custom button #%d', 'directories-pro' ), $num ),
			);
		}

		return $info;
	}

	public function displayButtonSettingsForm( Bundle $bundle, array $settings, $displayName, array $parents = array() ) {
		$settings = $this->_getSettingsFor13Compat( $settings );
		return $this->_application->Entity_Link_SettingsForm( $bundle, $settings, $parents );
	}

	public function displayButtonLink( Bundle $bundle, IEntity $entity, array $settings, $displayName ) {
		$settings = $this->_getSettingsFor13Compat( $settings );
		return $this->_application->Entity_Link(
			$entity,
			$settings,
			$settings['_label'],
			array( 'icon' => $settings['_icon'] ),
			array(
				'class' => $settings['_class'],
				'style' => $settings['_style'],
			)
		);
	}

	protected function _getSettingsFor13Compat( array $settings ) {
		foreach ( array( 'url', 'path', 'fragment', 'target', 'rel' ) as $key ) {
			if ( isset( $settings[ $key ] )
				&& ! isset( $settings[ 'link_' . $key ] )
			) {
				$settings[ 'link_' . $key ] = $settings[ $key ];
				unset( $settings[ $key ] );
			}
		}
		return $settings;
	}
}
