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

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

class VoteFormDisplayButton extends AbstractButton {

	protected $_type;

	public function __construct( Application $application, $name ) {
		parent::__construct( $application, $name );
		$this->_type = substr( $this->_name, 12 ); // remove voting_form_ prefix
	}

	protected function _displayButtonInfo( Bundle $bundle ) {
		$info = $this->_application->Voting_Types_impl( $this->_type )->votingTypeInfo();
		return array(
			'label'            => $info['label_button'],
			'default_settings' => array(
				'_color' => $info['color'] ?? 'outline-secondary',
				'modal'  => true,
				'_icon'  => $info['icon'], // required for overlay button
			),
			'labellable'       => false,
			'iconable'         => false,
		);
	}

	public function displayButtonSettingsForm( Bundle $bundle, array $settings, $displayName, array $parents = array() ) {
		return array(
			'modal' => array(
				'#type'          => 'checkbox',
				'#title'         => __( 'Show form in modal window', 'directories-pro' ),
				'#default_value' => $settings['modal'],
				'#horizontal'    => true,
			),
		) + $this->_application->Voting_Types_impl( $this->_type )->votingTypeButtonSettingsForm( $bundle, $settings, $parents );
	}

	public function displayButtonLink( Bundle $bundle, IEntity $entity, array $settings, $displayName ) {
		$type_impl = $this->_application->Voting_Types_impl( $this->_type );
		if ( ! $type_impl->votingTypeIsEnabled() ) {
			return;
		}

		$type_info    = $type_impl->votingTypeInfo();
		$label_action = sprintf( $type_info['label_action'], $bundle->getLabel( 'singular' ) );
		$label_btn    = isset( $type_info['label_btn'] ) ? sprintf( $type_info['label_btn'], $bundle->getLabel( 'singular' ) ) : $label_action;
		$icon         = $type_info['icon'];

		if ( ! $this->_application->Voting_CanVote( $entity, $this->_type ) ) {
			if ( ! $this->_application->getUser()->isAnonymous() ) {
				return;
			}

			return $this->_getLoginButton(
				$label_btn,
				$this->_getVoteUrl( $bundle, $entity ),
				array(
					'no_escape' => true,
					'icon'      => $icon,
				),
				array(
					'class' => $settings['_class'],
					'style' => $settings['_style'],
				)
			);
		}

		return $this->_application->LinkTo(
			$label_btn,
			$this->_getVoteUrl( $bundle, $entity ),
			array(
				'icon'      => $icon,
				'btn'       => true,
				'container' => empty( $settings['modal'] ) ? null : 'modal',
				'modalSize' => 'lg',
			),
			array(
				'class'            => $settings['_class'],
				'style'            => $settings['_style'],
				'data-modal-title' => $label_action,
			)
		);
	}

	protected function _getVoteUrl( Bundle $bundle, IEntity $entity ) {
		return $this->_application->Url(
			$bundle->getPath( false ) . '/vote',
			array(
				'entity_id'   => $entity->getId(),
				'voting_type' => $this->_type,
			)
		);
	}
}
