<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use League\Glide\ServerFactory;
use App\Domain\Feeds\Models\Feed;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Contracts\Filesystem\Filesystem;
use League\Glide\Responses\LaravelResponseFactory;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class StaticController extends Controller
{

    public function show(Filesystem $filesystem, Request $request, $path)
    {
        $server = ServerFactory::create([
            'response' => new LaravelResponseFactory($request),
            'source' => $filesystem->getDriver(),
            'cache' => $filesystem->getDriver(),
            'cache_path_prefix' => '.glide-cache',
            'base_url' => 'img',
        ]);

        $server->setPresets([
            'small' => [
                'w' => 200,
                'h' => 200,
                'fit' => 'crop',
            ],
            'medium' => [
                'w' => 500,
                'h' => 500,
                'fit' => 'crop',
            ],

            'banner' => [
                'w' => 500,
                'fit' => 'crop',
            ],
            'hero' => [
                'w' => 1600,
                'h' => 600,
                'fit' => 'crop',
            ],
        ]);

        return $server->getImageResponse($path, $request->all());
    }

    public function avatarRemove(Filesystem $filesystem)
    {
        $user = auth()->user();

        $server = ServerFactory::create([
            'source' => $filesystem->getDriver(),
            'cache' => $filesystem->getDriver(),
            'cache_path_prefix' => '.glide-cache',
            'base_url' => 'img',
        ]);

        Storage::disk('public')->delete($user->photo_path);
        $server->deleteCache($user->photo_path);
        $user->photo_path = null;
        $user->save();

        return Redirect::back()->with('success', 'Аватар успешно удален!');
    }

    public function avatar(Request $request)
    {
        $user = auth()->user();

        request()->validate([
            'avatar' => 'required|image|mimes:jpeg,jpg,png|max:1024',
        ]);

        $path = $request->file('avatar')->store('avatars', 'public');
        $user->photo_path = $path;
        $user->save();

        return Redirect::back()->with('success', 'Аватар успешно обновлен!');
    }

    public function banner(Request $request)
    {
        $user = auth()->user();

        request()->validate([
            'banner' => 'required|image|mimes:jpeg,jpg,png|max:2048',
        ]);

        $path = $request->file('banner')->store('banners', 'public');
        $user->banner_path = $path;
        $user->save();

        return Redirect::back()->with('success', 'Баннер успешно обновлен!');
    }

    public function bannerRemove(Filesystem $filesystem)
    {
        $user = auth()->user();

        $server = ServerFactory::create([
            'source' => $filesystem->getDriver(),
            'cache' => $filesystem->getDriver(),
            'cache_path_prefix' => '.glide-cache',
            'base_url' => 'img',
        ]);

        Storage::disk('public')->delete($user->banner_path);
        $server->deleteCache($user->banner_path);
        $user->banner_path = null;
        $user->save();

        return Redirect::back()->with('success', 'Баннер успешно удален!');
    }

    public function removePreview($id)
    {
        $media = Media::findOrFail($id);
        $feed = $media->model;
        if (is_a($feed, Feed::class)) {
            if ($feed->user()->is(auth()->user())) {
                $media->delete();
            }
        }
        return Redirect::back()->with('success', 'Превью успешно удалено!');
    }


}
