<?php
namespace Modules\Admin\ImportEntities\Model\Import;

class ImportedOrderEntityMatching extends \Modules\Admin\ImportEntities\Model\ImportAbstract
{
	/**
	 *
	 * @var array
	 */
	protected $fields = ['entityName', 'externalSystemCode', 'externalSystemEntityId', 'tinaEntityId'];

	/**
	 * Array with preset values for all imported orders entities to set
	 * @var array
	 */
	private $availableEntityNames = [
		"serviceType", "user", "office", "customer", "customerType", "customerTitle", "corporate",
		"supplier", "supplierProduct", "city", "station", "serviceCategory", "serviceProductType",
		"serviceSource", "serviceMultiplier", "offerFormOfPayment",
		"serviceCancelReason", "miceProject", "agencyCostCenter",
	];

	/**
	 *
	 * {@inheritDoc}
	 * @see \Modules\Admin\ImportEntities\Model\ImportAbstract::process()
	 */
	public function process()
	{
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		$messagesFromImport = new \App\MessagesCollector();
		$count = 1;

		foreach($this->fileData as $row) {
			$idSystem = null;
			$count++;

			if (!$row['entityName']) {
				$messagesFromImport->addError(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['matching']['error_missing_entityName'], $count));
				continue;
			}
			if (!in_array($row['entityName'], $this->availableEntityNames)) {
				$messagesFromImport->addError(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['matching']['error_invalid_entityName'], $count));
				continue;
			}

			if (!$row['externalSystemCode']) {
				$messagesFromImport->addError(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['matching']['error_missing_idSystem'], $count));
				continue;
			}
			if (!$row['externalSystemEntityId']) {
				$messagesFromImport->addError(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['matching']['error_missing_idEntityExtSys'], $count));
				continue;
			}

			$idSystem = \Entity\ExtResSystem::getSystemIdByCode($row['externalSystemCode']);

			if (!$idSystem) {
				$messagesFromImport->addError(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['matching']['error_externalSystemCodeInvalid'], $count));
				continue;
			}

			try {
				$ent = \App\Config\ImportedOrders_Matches::getAdmin();

				switch ($row['entityName']) {

					case 'serviceType':
						$ent->setServiceType($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'corporate':
						$ent->setCorporate($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'user':
						$ent->setUser($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'supplierProduct':
						$ent->setSupplierProduct($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'office':
						$ent->setOffice($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'customer':
						$ent->setCustomer($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'customerType':
						$ent->setCustomerType($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'customerTitle':
						$ent->setCustomerTitle($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'supplier':
						$ent->setSupplier($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'city':
						$ent->setCity($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'station':
						$ent->setStation($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'serviceCategory':
						$ent->setServiceCategory($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'serviceProductType':
						$ent->setServiceProductType($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'serviceSource':
						$ent->setServiceSource($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'serviceMultiplier':
						$ent->setServiceMultiplier($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'offerFormOfPayment':
						$ent->setOfferFormOfPayment($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'serviceCancelReason':
						$ent->setServiceCancelReason($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'miceProject':
						$ent->setMiceProject($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

					case 'agencyCostCenter':
						$ent->setAgencyCostCenter($idSystem, $row['externalSystemEntityId'], $row['tinaEntityId']);
					break;

				}

				$ent->save();

				$messagesFromImport->addSuccess(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['matching']['success'], $count));

			}
			catch (\App\Config\MessageException $e) {
				$messagesFromImport->addError(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['matching']['error'], $count, $e->getMessage()));
			}
		}

		return $messagesFromImport;

	}

	/**
	 *
	 * @return array|string[]
	 */
	public function getAvailableEntityNames()
	{
		return $this->availableEntityNames;
	}

}

