<?php

namespace App\Http\Controllers\Backend;

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

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

    public function deleteImage(Request $request)
    {
        $image = Medium::findOrFail($request->id);
        $image->post()->delete();
        $image->delete();
    }

    public function editImage(Request $request)
    {
        $image = Medium::findOrFail($request->id);

        return view('backend.partials.editImage', [
            'image' => $image
        ]);
    }

    public function updateImage(Request $request)
    {
        $image = Medium::findOrFail($request->id);
        $image->fill($request->only('url'))->save();
        $image->post->fill($request->post)->save();
    }

    public function upload($files, $model)
    {
        if($files) {
            foreach($files as $file) {
                $path = $file->hashName();
                $image = Image::make($file)->resize(1920, null, function($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });

                $thumbnail = clone($image);

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

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

                $thumbnail->resize(576, null, function($constraint) {
                    $constraint->aspectRatio();
                    $constraint->upsize();
                });
                $thumbnail_path = 'thumbnails/'.$path;
                Storage::put($thumbnail_path, $thumbnail->encode());

                $thumbnail_url = Storage::url($thumbnail_path);

                $medium = $model->post->media()->create([
                    'thumbnail' => $thumbnail_url,
                ]);
                $medium->post()->create([
                    'image' => $url,
                ]);
            }
        }
    }

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

            $thumbnail = clone($image);

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

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

            $thumbnail->resize(576, null, function($constraint) {
                $constraint->aspectRatio();
                $constraint->upsize();
            });
            $thumbnail_path = 'thumbnails/'.$path;
            Storage::put($thumbnail_path, $thumbnail->encode());

            $thumbnail_url = Storage::url($thumbnail_path);

            if($model->post->featured_image) {
                $model->post->featured_image->fill([
                    'thumbnail' => $thumbnail_url,
                ])->save();
                $model->post->featured_image->post->fill([
                    'image' => $url,
                ])->save();
            } else {
                $medium = $model->post->media()->create([
                    'thumbnail' => $thumbnail_url,
                ]);
                $medium->post()->create([
                    'image' => $url,
                    'featured' => 1,
                ]);
            }
        } elseif($request->has('image') && $model->post->featured_image) {
            $model->post->featured_image->delete();
        }
    }
}
