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

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Exception;
use SabaiApps\Directories\Context;

class SubmissionHelper {

	public function bundles( Application $application, $bundleType ) {
		$bundles = $application->Entity_Bundles_addable( $bundleType );
		foreach ( array_keys( $bundles ) as $bundle_name ) {
			if ( $bundle = $application->Entity_Bundle( $bundle_name ) ) {
				if ( empty( $bundle->info['parent'] )
					&& isset( $bundle->info['frontendsubmit_enable'] )
					&& empty( $bundle->info['frontendsubmit_enable'] )
				) {
					unset( $bundles[ $bundle_name ] );
				}
			} else {
				unset( $bundles[ $bundle_name ] );
			}
		}
		asort( $bundles );
		return $application->Filter( 'frontendsubmit_submittable_bundles', $bundles, array( $bundleType ) );
	}

	public function addEntityLink( Application $application, $bundle, ?array $options = null ) {
		if ( ( ! $bundle = $application->Entity_Bundle( $bundle ) )
			|| ! empty( $bundle->info['parent'] )
			|| $bundle->entitytype_name !== 'post'
		) {
			throw new Exception\RuntimeException( 'Invalid bundle.' );
		}

		$params = array( 'bundle' => $bundle->name );
		$slug   = '/' . $application->FrontendSubmit_AddEntitySlug( $bundle );
		if ( $application->getUser()->isAnonymous()
			&& ( ( ! $submittable_bundles = $application->FrontendSubmit_Submission_bundles( $bundle->type ) )
				|| ! isset( $submittable_bundles[ $bundle->name ] )
				|| $application->getComponent( 'FrontendSubmit' )->getConfig( 'guest', 'collect_privacy' )
			)
		) {
			$params['redirect_action'] = 'add';
			$params['redirect_bundle'] = $bundle->name;
			$url                       = $application->System_Url_login( (string) $application->Url( $slug, $params ) );
		} else {
			$url = $application->Url( $slug, $params );
		}
		$label        = isset( $options['label'] ) ? $options['label'] : $bundle->getLabel( 'add' );
		$link_options = $attr = array();
		if ( ! isset( $options['button'] ) ) {
			return $application->LinkTo( $label, $url, $link_options, $attr );
		}

		$application->getPlatform()->loadDefaultAssets( false, true );
		$link_options['icon'] = empty( $options['icon'] ) ? 'fas fa-pen-to-square' : $options['icon'];
		$attr['class']        = $application->System_Button_htmlClass(
			$options['button'] + array(
				'color' => 'primary',
				'size'  => 'lg',
			),
			'',
			true
		);

		return $application->LinkTo( $label, $url, $link_options, $attr );
	}

	public function isGuestRedirectRequired( Application $application, $bundleType, ?Context $context = null, ?array &$formStorage = null ) {
		if ( ! $application->Filter( 'frontendsubmit_redirect_guest_check', true, array( $bundleType ) ) ) {
			return false;
		}

		if ( isset( $formStorage ) ) {
			if ( ! isset( $formStorage['_guest'] )
				&& isset( $context )
				&& ( $guest_info = $context->getRequest()->get( '_guest' ) )
			) {
				$formStorage['_guest'] = is_array( $guest_info ) ? $guest_info : array();
			}
			if ( isset( $formStorage['_guest'] ) ) {
				return false; // came by clicking "Continue as Guest"
			}
		}

		$ret = $application->getComponent( 'FrontendSubmit' )->hasCustomLoginRegistrationPage();

		if ( ! $application->Filter( 'frontendsubmit_redirect_guest', $ret ) ) {
			return false;
		}

		return true;
	}
}
