<?php

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

class CityController extends Controller
{

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

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

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

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

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

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

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

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

    public function citiesByCountry(Request $request)
    {
        $cities = $this->cityService->get($request);
        return view( $this->viewDirectory.'cities', get_defined_vars() );
    }

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

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

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

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

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

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