<?php

namespace Modules\Currency\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Currency\Requests\Admin\CurrencyRequest;
use Modules\Currency\Services\CurrencyService;

class CurrencyController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/currencies';
    /** View folder */
    private $viewDirectory= 'admin.currencies.';

    public function __construct(private CurrencyService $currencyService)
    {
    }

    /**
     * Display a listing of the currencies.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('currencies.head');
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the currencies.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true]);
        $currencies= $this->currencyService->get($request->all());

        return view($this->viewDirectory.'grid',get_defined_vars());
    }

    /**
     * Show the form for creating a new Currency.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('currencies.store');
        $head= __('currencies.new');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Currency in storage.
     * @param CurrencyRequest $request
     * @return JsonResponse
     */
    public function store(CurrencyRequest $request) :JsonResponse
    {
        $this->currencyService->create($request->validated());

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('currencies.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Currency.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $currency = $this->currencyService->getItem($id);
        if (!$currency)
        {
            abort(404);
        }

        $action= route('currencies.update', $currency->id);
        $head= __('currencies.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Currency in storage.
     * @param CurrencyRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(CurrencyRequest $request, int $id) :JsonResponse
    {
        $currency = $this->currencyService->checkItem($id);
        if (!$currency)
        {
            abort(404);
        }

        $this->currencyService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('currencies.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Currency from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->currencyService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('currencies.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several currencies by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->currencyService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('currencies.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified Currency from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->currencyService->destroy($id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('currencies.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several currencies by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->currencyService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('currencies.index'),
        ];
        return ResponseService::response($response);
    }
}
