<?php
namespace SabaiApps\Directories\Component\Fieldset\CSVImporter;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\CSV\Importer\AbstractImporter;
use SabaiApps\Directories\Component\Entity\Model\Field;

class FieldsetCSVImporter extends AbstractImporter {

	public function csvImporterColumns( Field $field ) {
		$field_settings = $field->getFieldSettings();
		if ( empty( $field_settings['bundle'] )
			|| ( ! $bundle = $this->_application->Entity_Bundle( $field_settings['bundle'] ) )
			|| ( ! $fieldset_fields = $this->_application->Entity_Field( $bundle->name ) )
		) {
			return;
		}

		$columns = array();
		foreach ( $fieldset_fields as $f_field ) {
			if ( in_array( $f_field->getFieldType(), array( 'entity_title', 'entity_slug', 'entity_published' ) ) ) {
				continue;
			}

			if ( ! $importer = $this->_getImporter( $f_field->getFieldType() ) ) {
				continue;
			}

			if ( $f_field->getFieldType() !== $this->_name
				&& ( $_columns = $importer->csvImporterColumns( $f_field ) )
			) {
				foreach ( $_columns as $_column_name => $_column_label ) {
					$columns[ $f_field->getFieldName() . '__' . $_column_name ] = $f_field->getFieldLabel() . ' - ' . $_column_label;
				}
			} else {
				$columns[ $f_field->getFieldName() ] = $f_field->getFieldLabel();
			}
		}
		return $columns;
	}

	protected function _getImporter( $fieldType ) {
		$importers = $this->_application->CSV_Importers( true );
		if ( ! isset( $importers[ $fieldType ] ) ) {
			return false;
		}

		return $this->_application->CSV_Importers_impl( $importers[ $fieldType ], true );
	}

	public function csvImporterSettingsForm( Field $field, array $settings, $column, $enclosure, array $parents = array() ) {
		$field_settings = $field->getFieldSettings();
		if ( empty( $field_settings['bundle'] )
			|| ( ! $bundle = $this->_application->Entity_Bundle( $field_settings['bundle'] ) )
			|| ( ! $fieldset_fields = $this->_application->Entity_Field( $bundle->name ) )
		) {
			return;
		}

		$column_arr   = explode( '__', $column );
		$f_field_name = array_shift( $column_arr );
		if ( ! isset( $fieldset_fields[ $f_field_name ] ) ) {
			return;
		}

		$f_field = $fieldset_fields[ $f_field_name ];
		if ( in_array( $f_field->getFieldType(), array( 'fieldset_fieldset' ) )
			|| ( ! $importer = $this->_getImporter( $f_field->getFieldType() ) )
		) {
			return;
		}

		return $importer->csvImporterSettingsForm(
			$f_field,
			$settings,
			empty( $column_arr ) ? '' : implode( '__', $column_arr ),
			$enclosure,
			$parents
		);
	}

	public function csvImporterDoImport( Field $field, array $settings, $column, $value, &$formStorage, array &$logs ) {
		$field_settings = $field->getFieldSettings();
		if ( empty( $field_settings['bundle'] )
			|| ( ! $bundle = $this->_application->Entity_Bundle( $field_settings['bundle'] ) )
			|| ( ! $fieldset_fields = $this->_application->Entity_Field( $bundle->name ) )
		) {
			return;
		}

		$column_arr   = explode( '__', $column );
		$f_field_name = array_shift( $column_arr );
		if ( ! isset( $fieldset_fields[ $f_field_name ] ) ) {
			return;
		}

		$f_field = $fieldset_fields[ $f_field_name ];
		if ( in_array( $f_field->getFieldType(), array( 'fieldset_fieldset' ) )
			|| ( ! $importer = $this->_getImporter( $f_field->getFieldType() ) )
		) {
			return;
		}

		$ret     = array();
		$_column = empty( $column_arr ) ? '' : implode( '__', $column_arr );
		foreach ( explode( '%%', $value ) as $_value ) {
			$ret[] = array(
				$column => $importer->csvImporterDoImport( $f_field, $settings, $_column, $_value, $formStorage, $logs ),
			);
		}
		return $ret;
	}

	public static function onCSVImportFieldValuesFilter( Application $application, &$values, $bundle ) {
		foreach ( array_keys( $values ) as $field_name ) {
			if ( ( ! $field = $application->Entity_Field( $bundle, $field_name ) )
				|| $field->getFieldType() !== 'fieldset_fieldset'
				|| empty( $values[ $field_name ] )
			) {
				continue;
			}

			foreach ( array_keys( $values[ $field_name ] ) as $i ) {
				foreach ( array_keys( $values[ $field_name ][ $i ] ) as $csv_column_name ) {
					if ( ! strpos( $csv_column_name, '__' )
						|| ( ! $_csv_column_name = explode( '__', $csv_column_name ) )
					) {
						if ( $csv_column_name === 'fieldset_id' ) {
							$values[ $field_name ][ $i ]['_fieldset_id'] = $values[ $field_name ][ $i ]['fieldset_id'];
							unset( $values[ $field_name ][ $i ]['fieldset_id'] );
						}
						continue;
					}

					$f_field_name   = array_shift( $_csv_column_name );
					$f_field_column = implode( '__', $_csv_column_name );
					foreach ( array_keys( $values[ $field_name ][ $i ][ $csv_column_name ] ) as $j ) {
						$values[ $field_name ][ $i ][ $f_field_name ][ $j ][ $f_field_column ] = $values[ $field_name ][ $i ][ $csv_column_name ][ $j ][ $f_field_column ];
					}
					unset( $values[ $field_name ][ $i ][ $csv_column_name ] );
				}
			}
		}
	}
}
