<?php

namespace LaraBB\Translate\UI\Web\Handlers;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use LaraBB\Translate\Actions\TranslatePageAction;

/**
 * Class TranslatePageHandler
 * @package LaraBB\Translate\UI\Web\Handlers
 */
class TranslatePageHandler extends Controller
{
    /**
     * @var TranslatePageAction
     */
    private $translatePageAction;

    /**
     * TranslatePageHandler constructor.
     * @param TranslatePageAction $translatePageAction
     */
    public function __construct(TranslatePageAction $translatePageAction)
    {
        $this->translatePageAction = $translatePageAction;
    }

    /**
     * @param Request $request
     * @param string $lang
     * @return Application|Factory|RedirectResponse|View
     */
    public function __invoke(Request $request, string $lang = 'de')
    {
        if ($request->has('lang')) {
            return redirect()->route('translate-index', [$request->input('lang')]);
        }

        $return = $this->translatePageAction->run($request, $lang);

        return view('translate::translate', $return);
    }
}
