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

use SabaiApps\Directories\Application;

class ElementLabelSettingsFormHelper {

	public function help( Application $application, array $settings, array $parents, $isFormField = true, $weight = -10 ) {
		$label_options = array(
			'custom'      => __( 'Custom label', 'directories-pro' ),
			'custom_icon' => __( 'Icon', 'directories-pro' ) . ' + ' . __( 'Custom label', 'directories-pro' ),
			'icon'        => __( 'Icon', 'directories-pro' ),
			'none'        => __( 'None', 'directories-pro' ),
		);
		if ( $isFormField ) {
			$label_options = array(
				'form'      => __( 'Default label', 'directories-pro' ),
				'form_icon' => __( 'Icon', 'directories-pro' ) . ' + ' . __( 'Default label', 'directories-pro' ),
			) + $label_options;
			if ( ! isset( $settings['label'] ) ) {
				$settings['label'] = 'form';
			}
		} elseif ( ! isset( $settings['label'] ) ) {
				$settings['label'] = 'none';
		}
		$settings += array(
			'label_custom'    => null,
			'label_icon'      => null,
			'label_icon_size' => '',
		);

		return array(
			'#tree'                => true,
			'#tree_allow_override' => false,
			'label'                => array(
				'#title'         => __( 'Label', 'directories-pro' ),
				'#type'          => 'select',
				'#options'       => $label_options,
				'#default_value' => $settings['label'],
				'#horizontal'    => true,
				'#weight'        => $weight,
			),
			'label_custom'         => array(
				'#title'         => __( 'Custom label', 'directories-pro' ),
				'#type'          => 'textfield',
				'#default_value' => $settings['label_custom'],
				'#states'        => array(
					'visible' => array(
						sprintf( 'select[name="%s[label]"]', $application->Form_Form_fieldName( $parents ) ) => array(
							'type'  => 'one',
							'value' => array( 'custom', 'custom_icon' ),
						),
					),
				),
				'#required'      => array( array( __CLASS__, '_isLabelRequired' ), array( $parents, array( 'custom', 'custom_icon' ) ) ),
				'#horizontal'    => true,
				'#weight'        => ++$weight,
			),
			'label_icon'           => array(
				'#title'         => __( 'Icon', 'directories-pro' ),
				'#type'          => 'iconpicker',
				'#default_value' => $settings['label_icon'],
				'#states'        => array(
					'visible' => array(
						sprintf( 'select[name="%s[label]"]', $application->Form_Form_fieldName( $parents ) ) => array(
							'type'  => 'one',
							'value' => array( 'form_icon', 'custom_icon', 'icon' ),
						),
					),
				),
				'#required'      => array( array( __CLASS__, '_isLabelRequired' ), array( $parents, array( 'form_icon', 'custom_icon', 'icon' ) ) ),
				'#horizontal'    => true,
				'#weight'        => ++$weight,
			),
			'label_icon_size'      => array(
				'#title'         => __( 'Icon size', 'directories-pro' ),
				'#type'          => 'select',
				'#options'       => array(
					''       => __( 'Normal', 'directories-pro' ),
					'fa-lg'  => __( 'Large', 'directories-pro' ),
					'fa-2x'  => '2x',
					'fa-3x'  => '3x',
					'fa-5x'  => '5x',
					'fa-7x'  => '7x',
					'fa-10x' => '10x',
				),
				'#default_value' => $settings['label_icon_size'],
				'#horizontal'    => true,
				'#weight'        => ++$weight,
				'#states'        => array(
					'visible' => array(
						sprintf( 'select[name="%s[label]"]', $application->Form_Form_fieldName( $parents ) ) => array(
							'type'  => 'one',
							'value' => array( 'form_icon', 'custom_icon', 'icon' ),
						),
					),
				),
			),
		);
	}

	public static function _isLabelRequired( $form, $parents, $types ) {
		$form_values = $form->getValue( $parents );
		return in_array( $form_values['label'], $types );
	}

	public function registerLabel( Application $application, array $settings, $stringId ) {
		if ( ! in_array( $settings['label'], array( 'custom', 'custom_icon' ) ) ) {
			return;
		}

		$application->getPlatform()->registerString( $settings['label_custom'], $stringId, 'display_element' );
	}
}
