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

use SabaiApps\Directories\Component\CSV\Exporter\AbstractExporter;
use SabaiApps\Directories\Component\Entity\Model\Field;

class FieldsetCSVExporter extends AbstractExporter {

	public function csvExporterColumns( 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 ( ! $exporter = $this->_getExporter( $f_field->getFieldType() ) ) {
				continue;
			}

			if ( $f_field->getFieldType() !== $this->_name
				&& ( $_columns = $exporter->csvExporterColumns( $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 _getExporter( $fieldType ) {
		$exporters = $this->_application->CSV_Exporters( true );
		if ( ! isset( $exporters[ $fieldType ] ) ) {
			return false;
		}

		return $this->_application->CSV_Exporters_impl( $exporters[ $fieldType ], true );
	}

	public function csvExporterSettingsForm( 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       = explode( '__', $column );
		$f_field_name = $column[0];
		if ( ! isset( $fieldset_fields[ $f_field_name ] ) ) {
			return;
		}

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

		return $exporter->csvExporterSettingsForm(
			$f_field,
			$settings,
			isset( $column[1] ) ? $column[1] : '',
			$enclosure,
			$parents
		);
	}

	public function csvExporterDoExport( Field $field, array $settings, $value, array $columns, array &$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 ) )
			|| ( ! $fieldsets = $this->_application->Entity_Entities( 'post', $value, true, true ) )
		) {
			return;
		}

		$export_fields = $_columns = array();
		foreach ( $columns as $column ) {
			if ( ! $column = explode( '__', $column ) ) {
				continue;
			}

			$f_field_name = array_shift( $column );
			$column       = array_shift( $column );

			if ( ! isset( $fieldset_fields[ $f_field_name ] ) ) {
				continue;
			}
			$f_field = $fieldset_fields[ $f_field_name ];

			if ( ! isset( $export_fields[ $f_field_name ] ) ) {
				if ( ! $exporter = $this->_getExporter( $f_field->getFieldType() ) ) {
					continue;
				}

				$export_fields[ $f_field_name ] = $f_field_name;
			}
			if ( strlen( $column ) ) {
				$_columns[ $f_field_name ][ $column ] = $column;
			}
		}

		$ret = array();
		foreach ( $export_fields as $f_field_name ) {
			$f_field   = $fieldset_fields[ $f_field_name ];
			$exporter  = $this->_getExporter( $f_field->getFieldType() );
			$_settings = isset( $settings[ $f_field_name ] ) ? $settings[ $f_field_name ] : array();
			foreach ( $fieldsets as $fieldset ) {
				$f_field_value = $f_field_name === 'fieldset_id' ? $fieldset->getId() : $fieldset->getFieldValue( $f_field_name );
				if ( ! $f_field_value ) {
					if ( ! empty( $_columns[ $f_field_name ] ) ) {
						foreach ( $_columns[ $f_field_name ] as $column ) {
							$ret[ $f_field_name . '__' . $column ][] = '';
						}
					} else {
						$ret[ $f_field_name ][] = '';
					}
				} elseif ( ! empty( $_columns[ $f_field_name ] ) ) {
						$exported = $exporter->csvExporterDoExport( $f_field, $_settings, $f_field_value, $_columns[ $f_field_name ], $formStorage, $logs );
					foreach ( $_columns[ $f_field_name ] as $column ) {
						$ret[ $f_field_name . '__' . $column ][] = isset( $exported[ $column ] ) ? $exported[ $column ] : '';
					}
				} elseif ( $f_field->getFieldType() === $this->_name ) {
						$ret[ $f_field_name ][] = implode( ';', $f_field_value );
				} else {
					$ret[ $f_field_name ][] = $exporter->csvExporterDoExport( $f_field, $_settings, $f_field_value, array(), $formStorage, $logs );
				}
			}
		}
		foreach ( array_keys( $ret ) as $key ) {
			$ret[ $key ] = implode( '%%', $ret[ $key ] );
		}
		return $ret;
	}
}
