<?php

namespace App\Http\Controllers\Admin;

use App\Helper\BreadcrumbsRegister;
use App\DataTables\Admin\CurrencyRateDataTable;
use App\Http\Requests\Admin;
use App\Http\Requests\Admin\CreateCurrencyRateRequest;
use App\Http\Requests\Admin\UpdateCurrencyRateRequest;
use App\Repositories\Admin\CurrencyRateRepository;
use App\Http\Controllers\AppBaseController;
use Laracasts\Flash\Flash;
use Illuminate\Http\Response;

class CurrencyRateController extends AppBaseController
{
    /** ModelName */
    private $ModelName;

    /** BreadCrumbName */
    private $BreadCrumbName;

    /** @var  CurrencyRateRepository */
    private $currencyRateRepository;

    public function __construct(CurrencyRateRepository $currencyRateRepo)
    {
        $this->currencyRateRepository = $currencyRateRepo;
        $this->ModelName              = 'currency-rates';
        $this->BreadCrumbName         = 'Currency Rates';
    }

    /**
     * Display a listing of the CurrencyRate.
     *
     * @param CurrencyRateDataTable $currencyRateDataTable
     * @return Response
     */
    public function index(CurrencyRateDataTable $currencyRateDataTable)
    {
        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName);
        return $currencyRateDataTable->render('admin.currency_rates.index', ['title' => $this->BreadCrumbName]);
    }

    /**
     * Show the form for creating a new CurrencyRate.
     *
     * @return Response
     */
    public function create()
    {
        $currency = ['AUD' => 'AUD', 'CAD' => 'CAD', 'GBP' => 'GBP'];
        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName);
        return view('admin.currency_rates.create')->with(['title' => $this->BreadCrumbName, 'currency' => $currency]);
    }

    /**
     * Store a newly created CurrencyRate in storage.
     *
     * @param CreateCurrencyRateRequest $request
     *
     * @return Response
     */
    public function store(CreateCurrencyRateRequest $request)
    {
        $currencyRate = $this->currencyRateRepository->saveRecord($request);

        Flash::success($this->BreadCrumbName . ' saved successfully.');
        if (isset($request->continue)) {
            $redirect_to = redirect(route('admin.currency-rates.create'));
        } elseif (isset($request->translation)) {
            $redirect_to = redirect(route('admin.currency-rates.edit', $currencyRate->id));
        } else {
            $redirect_to = redirect(route('admin.currency-rates.index'));
        }
        return $redirect_to->with([
            'title' => $this->BreadCrumbName
        ]);
    }

    /**
     * Display the specified CurrencyRate.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $currencyRate = $this->currencyRateRepository->findWithoutFail($id);

        if (empty($currencyRate)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.currency-rates.index'));
        }

        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName, $currencyRate);
        return view('admin.currency_rates.show')->with(['currencyRate' => $currencyRate, 'title' => $this->BreadCrumbName]);
    }

    /**
     * Show the form for editing the specified CurrencyRate.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $currencyRate = $this->currencyRateRepository->findWithoutFail($id);

        if (empty($currencyRate)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.currency-rates.index'));
        }
        $currency = ['AUD' => 'AUD', 'CAD' => 'CAD', 'GBP' => 'GBP'];
        BreadcrumbsRegister::Register($this->ModelName, $this->BreadCrumbName, $currencyRate);
        return view('admin.currency_rates.edit')->with(['currencyRate' => $currencyRate, 'title' => $this->BreadCrumbName, 'currency' => $currency]);
    }

    /**
     * Update the specified CurrencyRate in storage.
     *
     * @param  int $id
     * @param UpdateCurrencyRateRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateCurrencyRateRequest $request)
    {
        $currencyRate = $this->currencyRateRepository->findWithoutFail($id);

        if (empty($currencyRate)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.currency-rates.index'));
        }

        $currencyRate = $this->currencyRateRepository->updateRecord($request, $currencyRate);

        Flash::success($this->BreadCrumbName . ' updated successfully.');
        if (isset($request->continue)) {
            $redirect_to = redirect(route('admin.currency-rates.create'));
        } else {
            $redirect_to = redirect(route('admin.currency-rates.index'));
        }
        return $redirect_to->with(['title' => $this->BreadCrumbName]);
    }

    /**
     * Remove the specified CurrencyRate from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $currencyRate = $this->currencyRateRepository->findWithoutFail($id);

        if (empty($currencyRate)) {
            Flash::error($this->BreadCrumbName . ' not found');
            return redirect(route('admin.currency-rates.index'));
        }

        $this->currencyRateRepository->deleteRecord($id);

        Flash::success($this->BreadCrumbName . ' deleted successfully.');
        return redirect(route('admin.currency-rates.index'))->with(['title' => $this->BreadCrumbName]);
    }
}
