<?php

namespace DMZ\Http\Controllers\Web;

use Illuminate\Http\Request;
use DMZ\Models\Contents;
use DMZ\Models\Categories;
use DMZ\Models\Files;
use DMZ\Http\Requests;
use DMZ\Http\Controllers\Controller;
use Validator;
use Response;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;

class FilesController extends Controller
{

        public function edit($id)
            {
                //  return $id;
                $data['content'] = Contents::getContentItem($id)->first();

                // return $data;
                return view('web.files', $data);
            }
        public function delete($id)
            {
                  return $id;
                 

                return route('/');
            }
        public function store(Request $request)
            {
                // return $request->all();
                $data = new Files;
                $data->content_id = $request->id;
                if ($request->file('files')=='') {
                    return redirect()->route('welcome')
                        ->withSuccess('No se ha registrado ningún archivo');
                }
                $file = $request->file('files');
                $fileext = $file->getClientOriginalExtension();
                $creation = str_slug(date('Y-m-d H-i-s'));
                switch ($fileext) {
                    case 'jpg':
                    case 'jpeg':
                    case 'png':
                    case 'bmp':
                        $nombre = $creation.'-'.strtolower(str_slug($request->name)).'.'.$fileext;
                        $data->type = 'image';
                        $data->extension = $fileext;
                        \Storage::disk('imagesBig')->put($nombre,  \File::get($file));

                        $nombreOptimized = 'image-'.$creation.'-'.strtolower(str_slug($request->name)).'.'.$fileext;
                        $resizeOptimized= Image::make($request->file('files'));
                        $resizeOptimized->resize(900, 900, function ($constraint) {
                            $constraint->aspectRatio();
                        })->encode($fileext);
                        $path=\Storage::disk('imagesOptimized')->put( $nombreOptimized, (string) $resizeOptimized );

                        $nombreThumbnail = 'thumbnail-'.$creation.'-'.strtolower(str_slug($request->name)).'.'.$fileext;
                        $resizeThumbnail= Image::make($request->file('files'));
                        $resizeThumbnail->resize(100, 100, function ($constraint) {
                            $constraint->aspectRatio();
                        })->encode($fileext);
                        $path=\Storage::disk('imagesSmall')->put( $nombreThumbnail, (string) $resizeThumbnail );
                        break;
                    case 'pdf':
                    case 'xls':
                    case 'xlsx':
                    case 'doc':
                    case 'docx':
                    case 'ppt':
                    case 'pptx':
                        $data->type = 'file';
                        $nombre = $creation.'-'.strtolower(str_slug($request->name)).'.'.$fileext;
                        $data->extension = $fileext;

                        \Storage::disk('filesWeb')->put($nombre,  \File::get($file));
                        break;

                    default: 

                        $content = Contents::getContentItemId($request->id)->first();

                        return redirect($content->category_slug.'/blog/'.$content->slug)
                           ->withSuccess('Los datos se actualizaron correctamente');
                            
                        break;
                }



                $data->file = $nombre;
                $data->name = $request->name;
                $data->description = $request->description;
                $data->save();
                $content = Contents::getContentItemId($request->id)->first();

               return redirect($content->category_slug.'/blog/'.$content->slug)
                   ->withSuccess('Los datos se actualizaron correctamente');

            }

}
