<?php

namespace App\Http\Controllers\Frontend\Forum;


use App\Exceptions\GeneralException;
use App\Http\Controllers\Controller;
use App\Models\Forums\ForumMedia;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ForumMediaController extends Controller
{

    public function store(Request $request)
    {

        $image = $request->file('uploaded_file');
     //   dd($image);
        $type = strtolower($image->getClientOriginalExtension());
        $originalname = $image->getClientOriginalName();
        $filename = $image->getFilename();
        $path = $image->getPath();
        $size = $image->getSize();
        // is this an allowed type?
        if (!in_array($type, ['jpg', 'jpeg', 'png'])) {
            throw new GeneralException('You must supply a jpg or png image.');
        }

        // get the md5 hash of the contents. This allows for different files with the same name in teh same directory...
        $md5name = md5(file_get_contents($path . DIRECTORY_SEPARATOR . $filename));

        // Intervention image
        $img = Image::make($image);

        // fix any weird mobile orientation issues
        $img->orientate();

//        // Make the image a 200x200 square
        $img->resize(($img->getHeight() > $img->getWidth()) ? 640 : 960, null , function ($constraint) {
            $constraint->aspectRatio();
        });
//        // crop to square
//        $img->crop(200, 200);

        // save with the UUID as a filename
        $img->save(Storage::disk('forum_media')->path('/') . $md5name);
        $media = ForumMedia::updateOrCreate(['name' => $originalname,
            'size' => $size,
            'type'=>$type,
            'location' => $md5name,
            'path' => Storage::disk('forum_media')->path('/') . $md5name,
            'user_id' => auth()->user()->id]);

        // $user->avatar_location = 'avatars/' . $user->uuid . '.jpg';
        return array(
            'location' => route('frontend.forum.displaymedia', $media->id),
        );

    }

    // shows the raw file. If it's an image, display it, if anything else, show an icon.
    public function display($id)
    {
//dd('hi');
        $media = ForumMedia::findOrFail($id);
       // dd($media);

        switch (strtolower($media->type)) {
            case 'jpg':
                $location = $media->location;
                $content_typestr = 'image/jpeg';
                break;
            case 'png':
                $location = $media->location;
                $content_typestr = 'image/png';
                break;
            case 'bmp':
                $location = $media->location;
                $content_typestr = 'image/bmp';
                break;

            default:
                $location = '/unknown_img.png';
                $content_typestr = 'image/png';
                break;
        }
        $file = Storage::disk('forum_media')->get($location);
        // resize image

        $response = new \Illuminate\Http\Response($file, '200');
        $response->header("Content-Type", $content_typestr);
        return $response;
    }

    public function download($id)
    {

        $media = \App\Review_instances_media::findOrFail($id);
        $content_typestr = "";
        $path = $media->path;
        switch (strtolower($media->type)) {
            case 'jpg':

                $content_typestr = 'image/jpeg';
                break;
            case 'png':
                $content_typestr = 'image/png';
                break;
            case 'bmp':
                $content_typestr = 'image/bmp';
                break;

            default:
                $content_typestr = 'application/octet-stream';
                break;
        }
        //  dd(Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix().$path);
        //dd($path);
        $file = Storage::get($path);

        $response = new \Illuminate\Http\Response($file, '200');
        $response->header("Content-Type", $content_typestr);
        // suggest to browser a download
        if ($content_typestr != 'image/png') {

            $response->header("Content-Disposition", 'download; filename=' . $media->name);
        }
        return $response;
    }

    // display a thumbnail from an id
    public function thumb($id)
    {

        $media = ForumMedia::findOrFail($id);

        switch (strtolower($media->type)) {
            case 'jpg':
            case 'png':
            case 'bmp':
                $path = $media->path;
                $file = Storage::get($path);
                break;

            default:
                $file = Storage::get('/public/unknown.png');
                break;
        }
        $image = Image::make($file);
        // get image height and width
        $origheight = $image->height();
        $origwidth = $image->width();
        // work out the ratio
        $ratio = ($origheight / $origwidth);
// work out size
        $size = array(($ratio < 0 ? (100 * $ratio) : 100), ($ratio < 0 ? 100 : (100 * $ratio)));
        // resize, dump out
        return $image->resize($size[0], $size[1])->response();

    }

    public function destroy(Request $request)
    {
        $input = $request::all();
        $status = Review_instances_media::destroy($input['id']);
        $response = array(
            'status' => $status,
        );
        return $response;

    }

}
