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

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

class AddEntityDisplayButton extends Display\Button\AbstractButton {

	protected $_bundleType;

	public function __construct( Application $application, $name ) {
		parent::__construct( $application, $name );
		$this->_bundleType = substr( $name, 19 ); // remove 'frontendsubmit_add_' prefix
	}

	protected function _displayButtonInfo( Bundle $bundle ) {
		if ( $child_bundle = $this->_application->Entity_Bundle( $this->_bundleType, $bundle->group ) ) {
			$label = $child_bundle->getLabel( 'singular' );
		} else {
			$this->_application->LogError( 'Failed fetching child bundle: ' . $this->_bundleType );
			$label = 'N/A';
		}
		return array(
			'label'            => sprintf( __( 'Add %s button', 'directories-pro' ), strtolower( $label ), $label ),
			'default_settings' => array(
				'modal'  => false,
				'_color' => 'outline-primary',
			),
			'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,
			),
		);
	}

	public function displayButtonLink( Bundle $bundle, IEntity $entity, array $settings, $displayName ) {
		if ( ! $this->_application->Filter( 'frontendsubmit_display_button_add_entity_link', true, array( $this->_name, $entity ) ) ) {
			return;
		}

		if ( ! $child_bundle = $this->_application->Entity_Bundle( $this->_bundleType, $bundle->group ) ) {
			return;
		}

		if ( $this->_application->Entity_IsRoutable( $child_bundle, 'add', $entity ) ) {
			if ( ( $show_in_modal = ! empty( $settings['modal'] ) )
				&& $this->_application->getUser()->isAnonymous()
				&& $this->_application->FrontendSubmit_Submission_isGuestRedirectRequired( $child_bundle->type )
			) {
				$show_in_modal = false; // do not show in modal if guest redirect is required
			}
			if ( $show_in_modal ) {
				$this->_application->Entity_Form_loadAssets( $child_bundle );
			}
			return $this->_application->LinkTo(
				$child_bundle->getLabel( 'add' ),
				$this->_application->Url( '/' . $this->_application->FrontendSubmit_AddEntitySlug( $child_bundle ), array( 'entity_id' => $entity->getId() ) ),
				array(
					'icon'      => $this->_application->Entity_BundleTypeInfo( $child_bundle, 'icon' ),
					'btn'       => true,
					'container' => $show_in_modal ? 'modal' : null,
					'modalSize' => 'lg',
				),
				array(
					'class' => $settings['_class'],
					'style' => $settings['_style'],
				)
			);
		}

		if ( ! $this->_application->getUser()->isAnonymous() ) {
			return;
		}

		return $this->_getLoginButton(
			$child_bundle->getLabel( 'add' ),
			$this->_application->Url( '/' . $this->_application->FrontendSubmit_AddEntitySlug( $child_bundle ), array( 'entity_id' => $entity->getId() ) ),
			array(
				'no_escape' => true,
				'icon'      => $this->_application->Entity_BundleTypeInfo( $child_bundle, 'icon' ),
				'btn'       => true,
			),
			array(
				'class' => $settings['_class'],
				'style' => $settings['_style'],
			)
		);
	}
}
