<?php

namespace App\Classes\Modules\Bookings\Services;

use App\Models\Booking;
use Illuminate\Support\Facades\Log;

class CalculatesBookingOutstanding
{

    /** @var CalculatesBookingPayableAmount */
    private $calculatesBookingPayableAmount;

    /** @var CalculatesBookingFloatingAmount */
    private $calculatesBookingFloatingAmount;

    /** @var CalculatesBookingRefundAmount */
    private $calculatesBookingRefundAmount;

    /**
     * CalculatesBookingOutstanding constructor.
     * @param CalculatesBookingPayableAmount $calculatesBookingPayableAmount
     * @param CalculatesBookingFloatingAmount $calculatesBookingFloatingAmount
     * @param CalculatesBookingRefundAmount $calculatesBookingRefundAmount
     */
    public function __construct(CalculatesBookingPayableAmount $calculatesBookingPayableAmount, CalculatesBookingFloatingAmount $calculatesBookingFloatingAmount, CalculatesBookingRefundAmount $calculatesBookingRefundAmount)
    {
        $this->calculatesBookingPayableAmount = $calculatesBookingPayableAmount;
        $this->calculatesBookingFloatingAmount = $calculatesBookingFloatingAmount;
        $this->calculatesBookingRefundAmount = $calculatesBookingRefundAmount;
    }


    public function execute(Booking $booking){
        $originalBookingAmount = $booking->fix_amount;
        $refundedAmount = $this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id);
        if($originalBookingAmount  === $refundedAmount){
            return $originalBookingAmount - $this->calculatesBookingFloatingAmount->execute($booking, $booking->fix_currency_id) - $this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id);
        }
        else{
            return $originalBookingAmount - $this->calculatesBookingFloatingAmount->execute($booking, $booking->fix_currency_id) - $this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id) + $refundedAmount;
        }
    }

    public function executeWithoutFloatingAmount(Booking $booking){
        return $booking->fix_amount - $this->calculatesBookingPayableAmount->execute($booking, $booking->fix_currency_id) + $this->calculatesBookingRefundAmount->execute($booking, $booking->fix_currency_id);
    }
}
