<?php

namespace App\Classes\Modules\Currencies\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Currencies\Services\FetchesCurrency;
use App\Classes\Modules\Currencies\Services\RateCalculatesCurrency;

use App\Http\Resources\RateCalculateCurrencyResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RateCalculateCurrencyLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Rate Calculated Currency',
            'message' => 'You have successfully rate calculated a Currency'
        ];
    }

    /** @var FetchesCurrency */
    private $fetchesCurrency;

    /** @var RateCalculatesCurrency */
    private $rateCalculatesCurrency;

    /**
     * @param FetchesCurrency $fetchesCurrency
     * @param RateCalculatesCurrency $rateCalculatesCurrency
     */
    public function __construct(FetchesCurrency $fetchesCurrency, RateCalculatesCurrency $rateCalculatesCurrency)
    {
        $this->fetchesCurrency = $fetchesCurrency;
        $this->rateCalculatesCurrency = $rateCalculatesCurrency;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function logic(Request $request) : JsonResponse
    {

        $amount = $request->input('amount');
        $conversion_currency = $this->fetchesCurrency->execute(['id' => $request->input('conversion_currency_id')]);
        $convertable_currency = $this->fetchesCurrency->execute(['id' => $request->input('convertable_currency_id')]);
        $this->rateCalculatesCurrency->execute($conversion_currency, $convertable_currency, $amount);

        return $this->resourceResponse(new RateCalculateCurrencyResource($conversion_currency, $convertable_currency, $amount));

    }

}