<?php

namespace Modules\Area\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\Area\Models\Area;
use Modules\Area\Requests\Admin\AreaRequest;
use Modules\Area\Services\AreaService;
use Modules\City\Services\CityService;
use Modules\Region\Services\RegionService;

class AreaController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/areas';
    /** View folder */
    private $viewDirectory= 'admin.areas.';
    //
    private AreaService $areaService;
    private RegionService $regionService;
    private CityService $cityService;

    public function __construct(AreaService $areaService,RegionService $regionService,CityService $cityService)
    {
        $this->areaService = $areaService;
        $this->regionService = $regionService;
        $this->cityService = $cityService;
    }

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Area.
     *
     * @param  Area  $area
     * @return View
     */
    public function edit(Area $area) :View
    {
        // dd($area->id);
        $action= route('areas.update', $area->id);
        $data= $this->areaService->getItem($area);
        $head= __('areas.edit');
//        $regions = $this->regionService->get(request());
        $cities = $this->cityService->get(request());

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

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

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

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

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

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