<?php

namespace Modules\Way\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Way\Models\Way;
use Modules\Way\Requests\Admin\WayRequest;
use Modules\Way\Services\WayService;

class WayController extends Controller
{

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

    public function __construct(WayService $wayService)
    {
        $this->wayService = $wayService;
    }

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

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

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

    /**
     * Show the form for creating a new Way.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('ways.store');
        $head= __('ways.new');
        $request = $request->merge(['way_id'=>'main']);
        $ways = $this->wayService->get($request);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Way in storage.
     * @param WayRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(WayRequest $request) :JsonResponse
    {
        $way= $this->wayService->save($request, null);

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

    /**
     * Show the form for editing the specified Way.
     *
     * @param  Way  $way
     * @return View
     */
    public function edit(Request $request,Way $way) :View
    {
        // dd($way->id);
        $action= route('ways.update', $way->id);
        $way= $this->wayService->getItem($way);
        $head= __('ways.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Way in storage.
     * @param WayRequest $request
     * @param Way $way
     * @return RedirectResponse|Redirector
     */
    public function update(WayRequest $request, Way $way) :JsonResponse
    {
        $way= $this->wayService->save($request, $way);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('ways.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Way from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->wayService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several ways by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->wayService->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Way from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->wayService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several ways by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->wayService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
