<?php

namespace Modules\Admin\ImportEntities\Model\Import;

class OfferProfileRulesUpdate extends \Modules\Admin\ImportEntities\Model\ImportAbstract
{

	/**
	 * The list of fields available for this import.
	 * @var array
	 */
	protected $fields = [
		'id', 'name', 'active', 'priority',
		'commissionIncluded', 'commissionValue', 'commissionCurrency', 'commissionPercent',
		'serviceFeeMode', 'serviceFeeAppliedFrom', 'serviceFeeValue', 'serviceFeeCurrency', 'serviceFeePercent',
		'discountPercentSource', 'discountValue', 'discountCurrency', 'discountPercent',
	];

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

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

		foreach($this->fileData as $row) {

			$count++;

			try {

				\DB::startTransaction();

				// get old rule details
				$offerProfileRuleOldDetailsEnt = \Entity\OfferProfileRule::selectForUpdate($row['id']);

				$offerProfileRuleEnt = \Entity\OfferProfileRule::update($row['id']);

				// main fields
				if (strlen($row['name'])) {
					$offerProfileRuleEnt->name = $row['name'];
				}
				if (strlen($row['active'])) {
					$offerProfileRuleEnt->active = $row['active'];
				}
				if (strlen($row['priority'])) {
					$offerProfileRuleEnt->priority = $row['priority'];
				}

				// commission
				if (strlen($row['commissionPercent']) or strlen($row['commissionValue'])) {
					$offerProfileRuleEnt->commissionDetails = $this->createOfferDetailsCommission($row);
				}
				else {
					$offerProfileRuleEnt->commissionDetails = $offerProfileRuleOldDetailsEnt->commissionDetails;
				}

				// service fee
				if ($row['serviceFeeMode']) {
					$offerProfileRuleEnt->serviceFeeDetails = $this->createOfferDetailsServiceFee($row);
				}
				else {
					$offerProfileRuleEnt->serviceFeeDetails = $offerProfileRuleOldDetailsEnt->serviceFeeDetails;
				}

				// discount
				if ($row['discountPercentSource']) {
					$offerProfileRuleEnt->discountDetails = $this->createOfferDetailsDiscount($row);
				}
				else {
					$offerProfileRuleEnt->discountDetails = $offerProfileRuleOldDetailsEnt->discountDetails;
				}

				$offerProfileRuleEnt->save();

				\DB::commitTransaction();
				$messagesFromImport->addSuccess(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['updateOfferProfileRules']['saveOk'], $row['id']));
			}
			catch (\App\Entity\Exception $e) {
				\DB::rollbackTransaction();
				$messagesFromImport->addWarning(sprintf($_LANG['Admin']['ImportEntities']['index']['msgs']['updateOfferProfileRules']['saveError'], $row['id']));
				$messagesFromImport->appendMessages($e->getMessagesCollector());
			}
		}

		return $messagesFromImport;

	}

	/**
	 * Create offerDetailsCommission object
	 * @param Array $row
	 * @return \App\OfferProfileRules\OfferDetails_Commission()
	 */
	private function createOfferDetailsCommission(Array $row)
	{
		$commissionDet = new \App\OfferProfileRules\OfferDetails_Commission();
		$commissionDet->included =	$row['commissionIncluded'];

		if (strlen($row['commissionPercent'])) {
			$commissionDet->percent = $row['commissionPercent'];
		}
		elseif (strlen($row['commissionValue'])) {
			$commissionDet->currency = 	$row['commissionCurrency'];
			$commissionDet->value = 		$row['commissionValue'];
		}

		return $commissionDet;
	}

	/**
	 * Create offer details service fee object
	 * @param array $row
	 * @return \App\OfferProfileRules\OfferDetails_ServiceFee()
	 */
	private function createOfferDetailsServiceFee(Array $row)
	{
		$serviceFeeDet = new \App\OfferProfileRules\OfferDetails_ServiceFee();

		$serviceFeeDet->mode 			= $row['serviceFeeMode'];
		$serviceFeeDet->percentSource 	= $row['serviceFeeAppliedFrom'];

		if ($row['serviceFeeMode'] == 'percent') {
			$serviceFeeDet->percent = $row['serviceFeePercent'];
		}
		else {
			$serviceFeeDet->currency = $row['serviceFeeCurrency'];
			$serviceFeeDet->value = $row['serviceFeeValue'];
		}

		return $serviceFeeDet;
	}

	/**
	 * Create offer details discount object
	 * @param array $row
	 * @return \App\OfferProfileRules\OfferDetails_ServiceFee()
	 */
	private function createOfferDetailsDiscount(Array $row)
	{
		$discountDet = new \App\OfferProfileRules\OfferDetails_Discount();

		$discountDet->percentSource 	= $row['discountPercentSource'];

		// DISCOUNT = bulk ------------------------------------------------------------------------
		// set the value+currency / percent
		if (strlen($row['discountPercent'])) {
			$discountDet->percent = $row['discountPercent'];
		}
		elseif (strlen($row['discountValue'])) {
			$discountDet->value = $row['discountValue'];
			$discountDet->currency = $row['discountCurrency'];
		}

		return $discountDet;
	}

}

