<?php

namespace App\Http\Controllers;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;

class ItemPicturesController extends Controller
{
    protected $visibility = 'public';
    protected $driver = 'images';
    protected $prefix = 'items';
    protected $defaultPath = 'items-default-image.jpg';

    public function show($pathAfter)
    {
        $path = Storage::disk($this->driver)->url($pathAfter);
        if (!File::exists($path)) {
            if ($pathAfter == 'avatar'){
                $path = Storage::disk($this->driver)->url('users/default-avatar.jpg');
            }else {
                $path = Storage::disk($this->driver)->url('items/items-default-image.jpg');
            }
        }

        $file = File::get($path);
        $type = File::mimeType($path);

        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);

        return $response;
    }

    public function store(UploadedFile $file): string
    {
        return Storage::disk($this->driver)->put($this->prefix, $file);
    }

    /**
     * @param array $files from Input::file()
     * @return array
     */
    public function storeInputFiles(array $files): array
    {
        $pathToFiles = [];
        foreach ($files as $fileBag) {
            foreach ($fileBag as $file) {
                $pathToFiles[] = $this->store($file);
            }
        }
        return $pathToFiles;
    }

}
