<?php

namespace Modules\Region\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\Region\Models\Region;
use Modules\Region\Requests\Admin\RegionRequest;
use Modules\Region\Services\RegionService;

class RegionController extends Controller
{

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

    public function __construct(RegionService $regionService)
    {
        $this->regionService = $regionService;
    }

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Region.
     *
     * @param  Region  $region
     * @return View
     */
    public function edit(Region $region) :View
    {
        // dd($region->id);
        $action= route('regions.update', $region->id);
        $data= $this->regionService->getItem($region);
        $head= __('regions.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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