<?php

namespace Modules\PaymentWay\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\PaymentWay\Requests\Admin\PaymentWayRequest;
use Modules\PaymentWay\Services\PaymentWayService;
use Modules\Currency\Services\CurrencyService;

class PaymentWayController extends Controller
{

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

    public function __construct(
        private PaymentWayService $paymentWayService,
    )
    {}

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

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

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

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

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

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

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

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

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

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

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

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