<?php

namespace App\Classes\Modules\Transactions\DataTransferObjects;

use App\Classes\General\Interfaces\DataTransferObject;

use App\Models\Transaction;
use App\Models\Booking;
use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyConversionObject;

class TransactionRefundCalculationObject implements DataTransferObject
{
    private $booking;

    private $transaction;

    private $amount;

    private $currency_rate;

    private $unrequested_amount;

    private $conversionObject;

    private $configurations;

    private $refund_amount;

    private $refund_service_charge;

    private $refund_tax;

    private $refund_total_amount;

    private $refund_conversion_amount;

    /**
     * TransactionRefundCalculationObject constructor.
     * @param float $price
     */
    public function __construct(Booking $booking, Transaction $transaction, ?float $amount = 0.00, ?float $currency_rate = 0.00)
    {
        $this->booking = $booking;
        $this->transaction = $transaction;
        $this->amount = $amount;
        $this->currency_rate = $currency_rate;
    }

    /**
     * @return Booking
     */
    public function getBooking(): Booking
    {
        return $this->booking;
    }

    /**
     * @return Transaction
     */
    public function getTransaction(): Transaction
    {
        return $this->transaction;
    }

    public function getAmount(): float
    {
        return $this->amount;
    }

    public function getCurrencyRate(): float
    {
        return $this->currency_rate;
    }

    /**
     * @return float
     */
    public function setUnrequestedAmount()
    {
        $this->unrequested_amount = $this->transaction->original_amount == $this->amount ? $this->transaction->original_amount : $this->transaction->original_amount - $this->amount;
        return $this;
    }

    public function getUnrequestedAmount()
    {
        return $this->unrequested_amount;
    }

    public function setConversionObject()
    {
        $this->conversionObject = new CurrencyConversionObject(floatval(str_replace(',', '', $this->getUnrequestedAmount())), $this->booking->convertible_currency_id, $this->booking->service_id,  $this->booking->fix_currency_id === 1 ? 0 : 1);
        return $this;
    }

    public function getConversionObject()
    {
        return $this->conversionObject;
    }

    public function setConfigurations()
    {
        $this->configurations = app('App\Classes\Modules\Bookings\Services\FetchesBookingQuotation')->execute($this->booking->company, $this->getConversionObject());
        return $this;
    }

    public function getConfigurations()
    {
        return $this->configurations;
    }

    public function setRefundAmount()
    {
        $this->refund_amount = $this->transaction->amount == $this->getConfigurations()->getLocalTotal() ? $this->transaction->amount : $this->transaction->amount - $this->getConfigurations()->getLocalTotal();
        return $this;
    }

    public function getRefundAmount()
    {
        return $this->refund_amount;
    }

    public function setRefundServiceCharge()
    {
        $this->refund_service_charge = $this->transaction->service_charge == $this->getConfigurations()->getServiceCharge() ? $this->transaction->service_charge : $this->transaction->service_charge - $this->getConfigurations()->getServiceCharge();
        return $this;
    }

    public function getRefundServiceCharge()
    {
        return $this->refund_service_charge;
    }

    public function setRefundTax()
    {
        $this->refund_tax = $this->transaction->tax == $this->getConfigurations()->getTax() ? $this->transaction->tax : $this->transaction->tax - $this->getConfigurations()->getTax();
        return $this;
    }

    public function getRefundTax()
    {
        return $this->refund_tax;
    }

    public function setRefundTotalAmount()
    {
        $this->refund_total_amount = $this->getRefundAmount() + $this->getRefundServiceCharge() + $this->getRefundTax();
        return $this;
    }

    public function getRefundTotalAmount()
    {
        return $this->refund_total_amount;
    }

    public function setRefundConversionAmount()
    {
        $this->refund_conversion_amount = $this->getCurrencyRate() ? $this->transaction->original_amount * (1 / $this->getCurrencyRate()) : $this->transaction->original_amount;
        return $this;
    }

    public function getRefundConversionAmount()
    {
        return $this->refund_conversion_amount;
    }

    public function init(){
        $this->setUnrequestedAmount();
        $this->setConversionObject();
        $this->setConfigurations();
        $this->setRefundAmount();
        $this->setRefundServiceCharge();
        $this->setRefundTax();
        $this->setRefundTotalAmount();
        $this->setRefundConversionAmount();
    }
}
