<?php

namespace Modules\ShippingWay\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\ShippingWay\Requests\Admin\ShippingWayRequest;
use Modules\ShippingWay\Services\ShippingWayService;
use Modules\Currency\Services\CurrencyService;

class ShippingWayController extends Controller
{

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

    public function __construct(
        private ShippingWayService $shippingWayService,
    )
    {}

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

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

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

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

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

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

    /**
     * Show the form for editing the specified ShippingWay.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $shippingWay = $this->shippingWayService->getItem($id);
        if (!$shippingWay)
        {
            abort(404);
        }
        $action= route('shipping_ways.update', $shippingWay->id);
        $head= __('shipping_ways.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified ShippingWay in storage.
     * @param ShippingWayRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(ShippingWayRequest $request, int $id) :JsonResponse
    {
        $shipping_way = $this->shippingWayService->checkItem($id);
        if (!$shipping_way)
        {
            abort(404);
        }
        $this->shippingWayService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('shipping_ways.index'),
        ];
        return ResponseService::response($response);
    }

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

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

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

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