<?php
namespace SabaiApps\Directories\Component\Comparison;

use SabaiApps\Directories\Component\AbstractComponent;
use SabaiApps\Directories\Component\View;
use SabaiApps\Directories\Component\System;
use SabaiApps\Directories\Component\Display;
use SabaiApps\Directories\Context;
use SabaiApps\Directories\Component\Entity\Model\Bundle;

class ComparisonComponent extends AbstractComponent implements View\IModes, System\IMainRouter, Display\IButtons {

	const VERSION = '2.0.6', PACKAGE = 'directories-pro';

	public static function description() {
		return 'Provides entity comparison functionality.';
	}

	public function viewGetModeNames() {
		return array( 'comparison' );
	}

	public function viewGetMode( $name ) {
		switch ( $name ) {
			case 'comparison':
				return new ViewMode\ComparisonViewMode( $this->_application, $name );
		}
	}

	public function systemMainRoutes( $lang = null ) {
		$routes = array();
		foreach ( $this->_application->Entity_Bundles() as $bundle ) {
			if ( empty( $bundle->info['comparison_enable'] ) ) {
				continue;
			}

			$routes[ $bundle->getPath( false, $lang ) . '/compare' ] = array(
				'controller'      => 'CompareEntities',
				'title_callback'  => true,
				'access_callback' => true,
				'callback_path'   => 'compare_entities',
				'priority'        => 3,
			);
		}

		return $routes;
	}
	public function systemOnAccessMainRoute( Context $context, $path, $accessType, array &$route ) {
		switch ( $path ) {
			case 'compare_entities':
				return isset( $context->bundle ) && ! empty( $context->bundle->info['comparison_enable'] );
		}
	}

	public function systemMainRouteTitle( Context $context, $path, $titleType, array $route ) {
		switch ( $path ) {
			case 'compare_entities':
				return $context->bundle->getLabel( 'compare' );
		}
	}

	public function onDirectoryContentTypeSettingsFormFilter( &$form, $directoryType, $contentType, $info, $settings, $parents, $submitValues ) {
		if ( empty( $info['comparison_enable'] )
			|| ! empty( $info['is_taxonomy'] )
			|| ! empty( $info['parent'] )
			|| empty( $info['public'] )
		) {
			return;
		}

		$form['comparison_enable'] = array(
			'#type'          => 'checkbox',
			'#title'         => __( 'Enable comparison', 'directories-pro' ),
			'#default_value' => ! empty( $settings['comparison_enable'] ) || is_null( $settings ),
			'#horizontal'    => true,
			'#weight'        => 30,
		);
	}

	public function onDirectoryContentTypeInfoFilter( &$info, $contentType, $settings = null ) {
		if ( ! isset( $info['comparison_enable'] ) ) {
			return;
		}

		if ( ! empty( $info['is_taxonomy'] )
			|| ! empty( $info['parent'] )
			|| empty( $info['public'] )
		) {
			unset( $info['comparison_enable'] );
		}

		if ( isset( $settings['comparison_enable'] ) && ! $settings['comparison_enable'] ) {
			$info['comparison_enable'] = false;
		}
	}

	public function onEntityBundleInfoKeysFilter( &$keys ) {
		$keys[] = 'comparison_enable';
	}

	public function onEntityBundleInfoUserKeysFilter( &$keys ) {
		$keys[] = 'comparison';
	}

	public function onEntityAdminBundleSettingsFormFilter( array &$form, Bundle $bundle, $submitValues ) {
		if ( empty( $bundle->info['comparison_enable'] ) ) {
			return;
		}

		// Add search settings
		$form['#tabs'][ $this->_name ] = array(
			'#title'  => __( 'Comparison', 'directories-pro' ),
			'#weight' => 30,
		);
		$form['comparison']            = array(
			'#tab'  => $this->_name,
			'#tree' => true,
		);
		$settings                      = empty( $bundle->info['comparison'] ) ? array() : $bundle->info['comparison'];
		$form['comparison']            = array(
			'#tab'  => $this->_name,
			'#tree' => true,
		) + $this->_application->View_SettingsForm_mode( $bundle, 'comparison', $settings, array( 'comparison' ) );

		// Add label setting
		$form['general']['labels']['label_compare'] = array(
			'#type'          => 'textfield',
			'#title'         => __( 'Compare items label', 'directories-pro' ),
			'#default_value' => $bundle->getLabel( 'compare' ),
			'#horizontal'    => true,
			'#placeholder'   => ( $placeholder = $this->_application->Entity_BundleTypeInfo( $bundle, 'label_compare' ) ) ? $placeholder : __( 'Compare', 'directories-pro' ),
			'#required'      => true,
		);
	}

	public function displayGetButtonNames( Bundle $bundle ) {
		return array( 'comparison_compare' );
	}

	public function displayGetButton( $name ) {
		return new DisplayButton\CompareDisplayButton( $this->_application, $name );
	}
}
