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

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class NumberFormatHelper {

	/**
	 * Returns the settings form for entity number formatting.
	 *
	 * @param Application $application
	 * @param array       $settings
	 * @param array       $parents
	 * @return array
	 */
	public function settingsForm( Application $application, array $settings, array $parents ) {
		return array(
			'format'      => array(
				'#type'          => 'textfield',
				'#title'         => __( 'Number format', 'directories-pro' ),
				'#default_value' => isset( $settings['format'] ) ? $settings['format'] : '%label% #%counter%',
				'#horizontal'    => true,
				'#description'   => $application->System_Util_availableTags( array( '%label%', '%counter%', '%day%', '%month%', '%year%', '%user_id%', '%rand%' ) ),
			),
			'counter'     => array(
				'#type'                     => 'select',
				'#title'                    => __( 'Counter number type', 'directories-pro' ),
				'#options'                  => array(
					'id'     => __( 'ID', 'directories-pro' ),
					'custom' => __( 'Custom', 'directories-pro' ),
				),
				'#default_value'            => isset( $settings['counter'] ) ? $settings['counter'] : 'id',
				'#horizontal'               => true,
				'#horizontal_label_padding' => false,
			),
			'custom_init' => array(
				'#type'          => 'number',
				'#integer'       => true,
				'#title'         => __( 'Start counter from', 'directories-pro' ),
				'#default_value' => isset( $settings['custom_init'] ) ? $settings['custom_init'] : 1,
				'#horizontal'    => true,
				'#min_value'     => 1,
				'#states'        => $custom_states = array(
					'visible' => array(
						$application->Form_Form_fieldName( array( $parents, 'counter' ), true ) => array( 'value' => 'custom' ),
					),
				),
			),
			'custom_add'  => array(
				'#type'          => 'number',
				'#integer'       => true,
				'#title'         => __( 'Increment counter by', 'directories-pro' ),
				'#default_value' => isset( $settings['custom_add'] ) ? $settings['custom_add'] : 1,
				'#horizontal'    => true,
				'#min_value'     => 1,
				'#states'        => $custom_states,
			),
			'pad'         => array(
				'#type'          => 'checkbox',
				'#integer'       => true,
				'#title'         => __( 'Pad counter number with zeros', 'directories-pro' ),
				'#default_value' => ! empty( $settings['pad'] ),
				'#horizontal'    => true,
			),
			'length'      => array(
				'#type'          => 'number',
				'#integer'       => true,
				'#title'         => __( 'Counter number length', 'directories-pro' ),
				'#default_value' => isset( $settings['length'] ) ? $settings['length'] : 6,
				'#horizontal'    => true,
				'#min_value'     => 2,
				'#states'        => array(
					'visible' => array(
						$application->Form_Form_fieldName( array( $parents, 'pad' ), true ) => array(
							'type'  => 'checked',
							'value' => true,
						),
					),
				),
			),
		);
	}

	/**
	 * Formats an entity number using the given settings.
	 *
	 * @param Application $application
	 * @param IEntity     $entity
	 * @param string      $label
	 * @param array       $settings
	 * @return string
	 */
	public function format( Application $application, IEntity $entity, $label, array $settings ) {
		$counter = $entity->getId();
		$format  = '%label% #%counter%';
		if ( isset( $settings['counter'] ) ) {
			switch ( $settings['counter'] ) {
				case 'custom':
					if ( ! $counter = $application->getPlatform()->getOption( $option_name = 'paymentgateway_number_counter_' . $entity->getBundleName() ) ) {
						$counter = isset( $settings['custom_init'] ) ? $settings['custom_init'] : 1;
					} else {
						$counter += isset( $settings['custom_add'] ) ? $settings['custom_add'] : 1;
					}
					$application->getPlatform()->setOption( $option_name, $counter, false );
					break;
				default:
			}
		}
		if ( ! empty( $settings['pad'] )
			&& ! empty( $settings['length'] )
		) {
			$counter = str_pad( $counter, $settings['length'], '0', STR_PAD_LEFT );
		}
		if ( isset( $settings['format'] ) && strlen( $settings['format'] ) ) {
			$format = $settings['format'];
		}

		return strtr(
			$format,
			array(
				'%label%'   => $label,
				'%counter%' => $counter,
				'%user_id%' => $entity->getAuthorId(),
				'%day%'     => date( 'd', $current_time = $application->getPlatform()->getSystemToSiteTime( time() ) ),
				'%month%'   => date( 'm', $current_time ),
				'%year%'    => date( 'Y', $current_time ),
				'%rand%'    => substr( md5( microtime() ), rand( 0, 26 ), 5 ),
			)
		);
	}
}
