<?php

namespace Modules\Language\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\Language\Enums\LanguageAdminStatusEnum;
use Modules\Language\Enums\LanguageStatusEnum;
use Modules\Language\Requests\Admin\LanguageRequest;
use Modules\Language\Services\LanguageService;

class LanguageController extends Controller
{

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

    public function __construct(
        private LanguageService $languageService,
    )
    {}

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Language.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $language = $this->languageService->getItem($id);
        if (!$language)
        {
            abort(404);
        }
        $action= route('languages.update', $language->id);
        $head= __('languages.edit');
        $statuses = LanguageStatusEnum::values();
        $admin_statuses = LanguageAdminStatusEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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