<?php

namespace App\Http\Controllers\Cabel;

use App\Helpers\Logger;
use App\Helpers\PaginationHelper;
use App\Http\Controllers\Controller;
use App\Models\CabelCategory;
use App\Models\Permission;
use Illuminate\Auth\Access\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CabelCategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function index(Request $request)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_viewAny'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $search_types = [];
        array_push($search_types,array("value" => "name", "name" => "name"));

        $PerPagination = $request->input('p') ?? 10;
        $search_term =  $request->input('search_term');
        $search_type = $request->input('search_type');
        $search_compare = $request->input('search_compare');

        if($search_term != ""){
            switch ($search_type){
                case "name":
                    switch($search_compare){
                        case("="):
                            $categories = CabelCategory::where('name','=',$search_term)->paginate($PerPagination);
                        break;
                        default:
                            $categories = CabelCategory::where('name','like','%' . $search_term . '%')->paginate($PerPagination);
                        break;
                    }
                break;
            }
        }
        else{
            $categories = CabelCategory::Paginate($PerPagination);
        }

        return view('cabelCategories.index')
            ->with('search_types',$search_types)
            ->with('data',$categories)
            ->with('data_name','cabelCategory')
            ->with('data_names','cabelCategories')
        ;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function deleted(Request $request)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_viewAny_deleted'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $search_types = [];
        array_push($search_types,array("value" => "building", "name" => "building"));
        array_push($search_types,array("value" => "room", "name" => "room"));

        $PerPagination = $request->input('p') ?? 10;
        $search_term =  $request->input('search_term');
        $search_type = $request->input('search_type');
        $search_compare = $request->input('search_compare');

        if($search_term != ""){
            switch ($search_type){
                case "name":
                    switch($search_compare){
                        case("="):
                            $categories = CabelCategory::onlyTrashed()->where('name','=',$search_term)->paginate($PerPagination);
                        break;
                        default:
                            $categories = CabelCategory::onlyTrashed()->where('name','like','%' . $search_term . '%')->paginate($PerPagination);
                        break;
                    }
                break;
            }
        }
        else{
            $categories = CabelCategory::onlyTrashed()->paginate($PerPagination);
        }

        return view('cabelCategories.deleted')
            ->with('search_types',$search_types)
            ->with('data',$categories)
            ->with('data_name','cabelCategory')
        ;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function create()
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_create'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        return view('cabelCategories.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_create'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $category = new CabelCategory();
        $category->name = $request->name;
        $category->save();

        Logger::LogCreated($category->id,get_class($category));

        return redirect()->route('cabelCategories.show',['cabelCategory' => $category]);
    }

    /**
     * Display the specified resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function show($cabelcategory)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_view'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $object = CabelCategory::where('id','=',$cabelcategory)->withTrashed()->first();

        return view('cabelCategories.show')
            ->with('data',$object)
            ->with('data_name','cabelCategory')
        ;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\CabelCategory  $cabelcategory
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function edit($cabelcategory)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_edit'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $object = CabelCategory::where('id','=',$cabelcategory)->withTrashed()->first();

        return view('cabelCategories.edit')
            ->with('data',$object)
            ->with('data_name','cabelCategory')
        ;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\CabelCategory  $cabelcategory
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, $cabelcategory)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_edit'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $object = CabelCategory::where('id','=',$cabelcategory)->withTrashed()->first();
        Logger::LogEdited($object->id,get_class($object),"Navn : ".$object->name." til ".$request->name);
        $object->name = $request->name;
        $object->save();

        return redirect()->route('cabelCategories.show',['cabelCategory' => $object]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\CabelCategory  $cabelcategory
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy($cabelcategory)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_delete'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $object = CabelCategory::where('id','=',$cabelcategory)->withTrashed()->first();
        Logger::LogDeleted($object->id,get_class($object));
        $object->delete();



        return redirect()->route('cabelCategories.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\CabelCategory  $cabelcategory
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete_force($cabelcategory)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_delete_force'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $object = CabelCategory::where('id','=',$cabelcategory)->withTrashed()->first();
        Logger::LogForceDeleted($object->id,get_class($object));
        $object->forceDelete();

        return redirect()->route('cabelCategories.deleted');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\CabelCategory  $cabelcategory
     * @return \Illuminate\Http\RedirectResponse
     */
    public function restore($cabelcategory)
    {
        Auth::user()->role->permissions->contains(Permission::firstWhere('name', '=', 'cabelCategories_restore'))
            ? Response::allow()
            : Response::deny('you are not the chosen one');

        $object = CabelCategory::where('id','=',$cabelcategory)->withTrashed()->first();
        $object->restore();

        Logger::LogRestored($object->id,get_class($object));

        return redirect()->route('cabelCategories.deleted');
    }
}