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

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

abstract class AbstractButton implements IButton {

	protected $_application, $_name, $_info;

	public function __construct( Application $application, $name ) {
		$this->_application = $application;
		$this->_name        = $name;
	}

	public function displayButtonInfo( Bundle $bundle, $key = null ) {
		if ( ! isset( $this->_info ) ) {
			$this->_info = $this->_displayButtonInfo( $bundle );
		}
		if ( ! is_array( $this->_info ) ) {
			return false;
		}

		return isset( $key ) ? ( isset( $this->_info[ $key ] ) ? $this->_info[ $key ] : null ) : $this->_info;
	}

	public function displayButtonSettingsForm( Bundle $bundle, array $settings, $displayName, array $parents = array() ) {}

	abstract protected function _displayButtonInfo( Bundle $bundle );

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

	public function displayButtonPreRender( Bundle $bundle, array $settings, array $entities = array() ) {}

	protected function _getLoginButton( $label, $redirect, $options, $attr ) {
		$_attr = array(
			'data-bs-content' => sprintf(
				_x( 'You must %s to perform this action.', 'login', 'directories-pro' ),
				$this->_getLoginLink( $redirect )
			),
			'data-bs-title'   => __( 'Login required', 'directories-pro' ),
		);
		return $this->_application->LinkTo(
			$label,
			'',
			array(
				'btn'       => true,
				'container' => 'popover',
			) + $options,
			$_attr + $attr
		);
	}

	protected function _getLoginLink( $redirect ) {
		return $this->_application->LinkTo(
			__( 'login', 'directories-pro' ),
			$this->_application->System_Url_login( $redirect ),
			array(),
			array( 'rel' => 'nofollow' )
		);
	}
}
