<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Slide;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Image;

class SlidesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('backend.slides.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Slide $slide)
    {
        $slides = Slide::orderBy('title')->pluck('title', 'id')->toArray();

        return view('backend.slides.form', [
            'slide' => $slide,
            'slides' => $slides
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'slug' => 'nullable|unique:slides,slug',
        ]);

        $slide = Slide::create($request->except('_method', '_token', 'image', 'image_2', 'image_3'));

        $this->upload($request, $slide);
        $this->upload($request, $slide, 'image_2');
        $this->upload($request, $slide, 'image_3');

        return redirect()->route('backend.slides.edit', $slide->id)->with('status', 'El slide ha sido creado');
    }

    public function upload($request, $slide, $field = 'image')
    {
        if($file = $request->file($field)) {
            $path = $file->hashName();
            $image = Image::make($file)->resize(1920, null, function($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });

            Storage::put($path, $image->encode());

            $url = Storage::url($path);

            $slide->fill([
                $field => $url,
            ])->save();
        }
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Slide  $slide
     * @return \Illuminate\Http\Response
     */
    public function edit(Slide $slide)
    {
        return view('backend.slides.form', [
            'slide' => $slide,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Slide  $slide
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Slide $slide)
    {
        $slide->fill($request->except('_method', '_token', 'image', 'image_2', 'image_3'))->save();

        if($file = $request->file('image')) {
            $this->upload($request, $slide);
        } elseif($request->has('image')) {
            $slide->fill([
                'image' => null
            ])->save();
        }
        if($file = $request->file('image_2')) {
            $this->upload($request, $slide, 'image_2');
        } elseif($request->has('image_2')) {
            $slide->fill([
                'image_2' => null
            ])->save();
        }
        if($file = $request->file('image_3')) {
            $this->upload($request, $slide, 'image_3');
        } elseif($request->has('image_3')) {
            $slide->fill([
                'image_3' => null
            ])->save();
        }

        return back()->with('status', 'El slide ha sido editado');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Slide  $slide
     * @return \Illuminate\Http\Response
     */
    public function destroy(Slide $slide)
    {
        $slide->delete();

        return redirect()->route('backend.slides.index')->with('status', 'El slide ha sido eliminado');
    }

    public function order(Request $request)
    {
        foreach($request->post as $key => $id) {
            Slide::where('id', $id)->update([
                'order' => $key
            ]);
        }
    }
}
