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

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

class PaymentCSVExporter extends AbstractExporter {

	public function csvExporterColumns( Field $field ) {
		switch ( $this->_name ) {
			case 'payment_plan':
				return array(
					'expires_at'     => __( 'Exp. Date', 'directories-pro' ),
					'plan_id'        => __( 'Plan ID', 'directories-pro' ),
					'addon_features' => __( 'Additional Features', 'directories-pro' ),
					'component'      => __( 'Payment component', 'directories-pro' ),
					'deactivated_at' => __( 'Date Deactivated', 'directories-pro' ),
					'extra_data'     => __( 'Extra Data', 'directories-pro' ),
				);
		}
		return parent::csvExporterColumns( $field );
	}

	public function csvExporterSupports( Bundle $bundle, Field $field ) {
		switch ( $this->_name ) {
			case 'payment_plan':
				return ! empty( $bundle->info['payment_enable'] );
		}
		return true;
	}

	public function csvExporterSettingsForm( Field $field, array $settings, $column, $enclosure, array $parents = array() ) {
		switch ( $this->_name ) {
			case 'payment_plan':
				switch ( $column ) {
					case 'expires_at':
					case 'deactivated_at':
						return $this->_getDateFormatSettingsForm( $parents );
				}
		}
	}

	public function csvExporterDoExport( Field $field, array $settings, $value, array $columns, array &$formStorage, array &$logs ) {
		switch ( $this->_name ) {
			case 'payment_plan':
				$ret                   = parent::csvExporterDoExport( $field, $settings, $value, $columns, $formStorage, $logs );
				$ret['addon_features'] = serialize( $ret['addon_features'] );
				$ret['extra_data']     = serialize( $ret['extra_data'] );
				foreach ( array( 'expires_at', 'deactivated_at' ) as $date_field ) {
					if ( isset( $settings[ $date_field ] ) && $settings[ $date_field ]['date_format'] === 'string' ) {
						if ( ! empty( $ret[ $date_field ] )
							&& false !== ( $date = @date( $settings[ $date_field ]['date_format_php'], $ret[ $date_field ] ) )
						) {
							$ret[ $date_field ] = $date;
						} else {
							$ret[ $date_field ] = '';
						}
					}
				}
				return $ret;
		}
	}
}
