<?php

namespace App\Http\Controllers\Admin;

use App\Module;
use Exception;
use App\Log;
use App\Gallery;
use App\GalleryPhoto;
use Illuminate\View\View;
use App\Category;
use App\GalleryDescription;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use App\Http\Controllers\Controller;
use App\Http\Requests\GalleryRequest;
use Illuminate\Http\RedirectResponse;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class GalleryController extends Controller
{

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

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

    /**
     * return data of the pages.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $query = Gallery::whereNotIn('id',['2','1'])->latest();

        if ($request->date_from) {
            $query->whereDate('galleries.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('galleries.created_at', '<=', $request->date_to);
        }

        if ($request->name) {
            $query->where('ad.name', 'LIKE', '%'.$request->name.'%');
        }

        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $galleries= $query->paginate(100);

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param GalleryRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(GalleryRequest $request) :RedirectResponse
    {
        $gallery = Gallery::create($request->all());
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'بانرات الرئيسية','action'=>'اضافة']);

       
        

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param Gallery $gallery
     * @return View
     */
    public function edit(Gallery $gallery) :View
    {
        $action= route('galleries.update', $gallery->id);

       
        return view( $this->viewDirectory.'form', compact('gallery', 'action') );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param GalleryRequest $request
     * @param Gallery $gallery
     * @return RedirectResponse|Redirector
     */
    public function update(GalleryRequest $request, Gallery $gallery) :RedirectResponse
    {

        // Update image and category id if request has it
        $gallery->update($request->all());
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'بانرات الرئيسية','action'=>'تعديل']);

       

        // Delete old description
        // GalleryDescription::where('gallery_id', $gallery->id)->delete();

        // // Insert new description
        // $this->saveData( $request, $gallery->id );

        return redirect($this->redirectSuccessPath)->with('message', __('dashboard.saveDone'));
    }

    /**
     * Handle Save form data
     *
     * @param GalleryRequest $request
     * @param int $gallery_id
     * @return void
     */

    private function saveData(GalleryRequest $request, int $gallery_id ):void
    {
        $requestData= $request->all();
        foreach (languages() as $lang) {
            $data=[
                'gallery_id'=> $gallery_id,
                'language_id'=> $lang->id,
                'name'=> $requestData['name_'.$lang->local],
                
              
            ];
            GalleryDescription::create($data);
        }

    

    }


    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $gallery = Gallery::withTrashed()->find($id);
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'بانرات الرئيسية','action'=>'حذف']);
        if ($gallery) {
            if ($gallery->deleted_at) {
                $gallery->forceDelete();
            } else {
                $gallery->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several pages by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'بانرات الرئيسية','action'=>'حذف']);
        if ($request->force) {
            Gallery::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Gallery::whereIn('id', $ids)->delete();
        }

        return response()->json(['message'=> __('dashboard.deletedDone')]);
    }

    /**
     * Restore the specified category from storage
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $gallery = Gallery::withTrashed()->find($id);
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'بانرات الرئيسية','action'=>'استرجاع']);

        if ($gallery){
            $gallery->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);
    }

    /**
     * Restore several pages by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $gallery = Gallery::whereIn('id', $request->ids)->onlyTrashed();
        Log::create(['user_id'=>\Auth::user()->id,'type'=>'بانرات الرئيسية','action'=>'استرجاع']);
        if ($gallery){
            $gallery->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
    }
}
