<?php
namespace SabaiApps\Directories\Component\Faker\Generator;

use SabaiApps\Directories\Component\Field\IField;
use SabaiApps\Directories\Component\Entity\Type\IEntity;

class SocialGenerator extends AbstractGenerator {

	protected function _fakerGeneratorInfo() {
		$info = parent::_fakerGeneratorInfo();
		switch ( $this->_name ) {
			case 'social_accounts':
				$info += array(
					'add_probability'  => false,
					'default_settings' => array(),
				);
				foreach ( $this->_application->Social_Medias() as $media_name => $media ) {
					if ( isset( $media['default'] ) ) {
						$info['default_settings'][ $media_name ]['content'] = $media['default'];
					}
					if ( isset( $media['faker_probability'] ) ) {
						$info['default_settings'][ $media_name ]['probability'] = $media['faker_probability'];
					}
				}
				break;
		}
		return $info;
	}

	public function fakerGeneratorSettingsForm( IField $field, array $settings, $mode, array $parents = array() ) {
		switch ( $this->_name ) {
			case 'social_accounts':
				$form = array();
				foreach ( $this->_application->Social_Medias() as $media_name => $media ) {
					$form[ $media_name ]                    = array(
						'#title' => $media['label'],
					);
					$form[ $media_name ]['probability']     = array(
						'#title'         => __( 'Probability of generating value for this field', 'directories-pro' ),
						'#type'          => 'slider',
						'#field_suffix'  => '%',
						'#min_value'     => 0,
						'#max_value'     => 100,
						'#integer'       => true,
						'#default_value' => $settings[ $media_name ]['probability'] ?? 80,
					);
					$form[ $media_name ]['content']         = array(
						'#group' => true,
					);
					$form[ $media_name ]['content'][0]      = array(
						'#type'          => isset( $media['type'] ) ? $media['type'] : 'url',
						'#field_prefix'  => isset( $media['icon'] ) ? sprintf( '<i class="%s"></i>', $media['icon'] ) : null,
						'#default_value' => $settings[ $media_name ]['content'] ?? null,
						'#regex'         => isset( $media['regex'] ) ? $media['regex'] : null,
						'#placeholder'   => isset( $media['placeholder'] ) ? $media['placeholder'] : '',
						'#description'   => __( 'Enter dummy data for this social media account.', 'directories-pro' ),
					);
					$form[ $media_name ]['content']['_add'] = array(
						'#type'       => 'addmore',
						'#next_index' => 1,
					);
				}
				return $form;
		}
	}

	public function fakerGeneratorGenerate( IField $field, array $settings, array &$formStorage, ?IEntity $entity = null ) {
		switch ( $this->_name ) {
			case 'social_accounts':
				$ret = array();
				foreach ( $this->_application->Social_Medias() as $media_name => $media ) {
					if ( empty( $settings[ $media_name ]['content'] )
						|| empty( $settings[ $media_name ]['probability'] )
						|| mt_rand( 0, 100 ) > $settings[ $media_name ]['probability']
					) {
						continue;
					}

					$ret[ $media_name ] = $settings[ $media_name ]['content'][ mt_rand( 0, count( $settings[ $media_name ]['content'] ) - 1 ) ];
				}
				return empty( $ret ) ? null : $ret;
		}
	}
}
