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

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

class PaymentCSVImporter extends AbstractImporter {

	public function csvImporterColumns( 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::csvImporterColumns( $field );
	}

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

	public function csvImporterSettingsForm( 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 csvImporterDoImport( Field $field, array $settings, $column, $value, &$formStorage, array &$logs ) {
		switch ( $this->_name ) {
			case 'payment_plan':
				if ( in_array( $column, array( 'expires_at', 'deactivated_at' ) ) ) {
					if ( false === ( $value = $this->_strToTime( $value, $settings ) ) ) {
						return null;
					}
				} elseif ( in_array( $column, array( 'addon_features', 'extra_data' ) ) ) {
					$value = @unserialize( $value );
					if ( ! is_array( $value ) ) {
						$value = array();
					}
				}
				return array( array( $column => $value ) );
		}
	}
}
