<?php

namespace App\OfferProfileRules;

class ApplyProfile
{

	/**
	 * Commission details.
	 * @var \App\OfferProfileRules\OfferDetails_Commission
	 */
	protected $commissionDetails;

	/**
	 * Discount details.
	 * @var \App\OfferProfileRules\OfferDetails_Discount
	 */
	protected $discountDetails;

	/**
	 * Service fee details.
	 * @var \App\OfferProfileRules\OfferDetails_ServiceFee
	 */
	protected $serviceFeeDetails;

	/**
	 * Sets the commission details object.
	 * @param OfferDetails_Commission $commissionDetails
	 */
	public function setCommissionDetails(OfferDetails_Commission $commissionDetails)
	{
		$this->commissionDetails = $commissionDetails;
	}

	/**
	 * Sets the discount details object.
	 * @param OfferDetails_Discount $discountDetails
	 */
	public function setDiscountDetails(OfferDetails_Discount $discountDetails)
	{
		$this->discountDetails = $discountDetails;
	}

	/**
	 * Sets the service fee details object.
	 * @param OfferDetails_ServiceFee $serviceFeeDetails
	 */
	public function setServiceFeeDetails(OfferDetails_ServiceFee $serviceFeeDetails)
	{
		$this->serviceFeeDetails = $serviceFeeDetails;
	}


	/**
	 * Populates the inserting/updating entities (offer + offer item) with the details from the commission/discount/sf details.
	 * @param \Entity\Offer $offer
	 * @param \Entity\OfferItem $offerItem
	 * @throws \Exception
	 */
	public function populateOfferFields(\Entity\Offer $offer, \Entity\OfferItem $offerItem)
	{

		// check input
		if ($offer->getOperation()!='insert' and $offer->getOperation()!='update') {
			throw new \Exception('The offer operation must be insert/update.');
		}
		if ($offerItem->getOperation()!='insert' and $offerItem->getOperation()!='update') {
			throw new \Exception('The offer item operation must be insert/update.');
		}

		// get the details from the offer components profiles
		$this->populateOfferFields_commission($offer, $offerItem);
		$this->populateOfferFields_serviceFee($offer, $offerItem); // the service fee must be calculated before discount
		$this->populateOfferFields_discount($offer, $offerItem);

	}

	/**
	 * Populates the inserting/updating offer + offer item with commission details.
	 * @param \Entity\Offer $offer
	 * @param \Entity\OfferItem $offerItem
	 */
	private function populateOfferFields_commission(\Entity\Offer $offer, \Entity\OfferItem $offerItem)
	{

		// if no commission - skip this step
		if (!$this->commissionDetails) {
			return ;
		}

		$offer->commissionable = $this->commissionDetails->included;

		// commission is percent
		if ($this->commissionDetails->percent!==null) {
			$offerItem->commissionPercent = $this->commissionDetails->percent;
			$offerItem->commissionInitialValue = null;
		}
		// commission is absolute value
		elseif ($this->commissionDetails->currency) {
			$offerItem->commissionInitialCurrency = $this->commissionDetails->currency;
			$offerItem->commissionInitialValue = $this->commissionDetails->value;
		}

	}


	/**
	 * Populates the inserting/updating offer + offer item with service fee details.
	 * @param \Entity\Offer $offer
	 * @param \Entity\OfferItem $offerItem
	 */
	private function populateOfferFields_serviceFee(\Entity\Offer $offer, \Entity\OfferItem $offerItem)
	{

		// if no commission - skip this step
		if (!$this->serviceFeeDetails) {
			return ;
		}

		// service fee main params
		if ($this->serviceFeeDetails->mode) {
			$offer->serviceFeeMode = $this->serviceFeeDetails->mode;
		}
		if ($this->serviceFeeDetails->percentSource) {
			$offer->serviceFeePercentSource = $this->serviceFeeDetails->percentSource;
		}

		// service fee mode = value ---------------------------------------------------------------
		if (in_array($this->serviceFeeDetails->mode, ['value', 'segment', 'duration'])) {
			$offerItem->serviceFeeInitialValue = (Float)$this->serviceFeeDetails->value;
			$offerItem->serviceFeeInitialCurrency = $this->serviceFeeDetails->currency;
		}

		// service fee mode = percent -------------------------------------------------------------
		if ($this->serviceFeeDetails->mode=='percent') {
			$offerItem->serviceFeePercent = $this->serviceFeeDetails->percent;
			$offerItem->serviceFeeInitialValue = null;
		}

	}


	/**
	 * Populates the inserting/updating offer + offer item with discount details.
	 * @param \Entity\Offer $offer
	 * @param \Entity\OfferItem $offerItem
	 */
	private function populateOfferFields_discount(\Entity\Offer $offer, \Entity\OfferItem $offerItem)
	{

		// if no commission - skip this step
		if (!$this->discountDetails) {
			return ;
		}

		$offerItemFields = $offerItem->getFields();

		if ($this->discountDetails->percentSource) {
			$offer->discountPercentSource = $this->discountDetails->percentSource;
		}

		if ($this->discountDetails->percent!==null) {
			$offerItem->discountPercent = $this->discountDetails->percent;
			$offerItem->discountInitialCurrency = $offerItemFields['supplierInitialCurrency'];
			$offerItem->discountInitialValue = null;
		} else {
			$offerItem->discountInitialValue = $this->discountDetails->value;
			$offerItem->discountInitialCurrency = $this->discountDetails->currency;
		}

	}

}

