<?php

namespace App\Http\Controllers;

use App\Models\Photo;
use App\Traits\Multimedia;

class PhotoController extends Controller
{
	use Multimedia;

	protected $list_duplicates = 20;
	protected $list_deletes = 50;

	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		$this->middleware('guest');
	}

	public function delete(Photo $o)
	{
		$o->remove = TRUE;
		$o->save();

		return redirect()->action('PhotoController@info',[$o->id]);
	}

	public function deletes($id=NULL)
	{
		return view('catalog.deletereview',[
			'catalog'=>is_null($id) ? Photo::where('remove',1)->with(['software.model.make'])->paginate($this->list_deletes) : Photo::where('id',$id)->paginate(1),
			'return'=>url('p/deletes'),
			'type'=>'photo',
		]);
	}

	public function duplicates($id=NULL)
	{
		return view('catalog.duplicatereview',[
			'catalog'=>is_null($id) ? Photo::duplicates()->with(['software.model.make'])->paginate($this->list_duplicates) : Photo::where('id',$id)->paginate(1),
			'return'=>url('p/duplicates'),
			'type'=>'photo',
		]);
	}

	public function info(Photo $o)
	{
		return view('photo.view',['o'=>$o]);
	}

	public function thumbnail(Photo $o)
	{
		return response($o->thumbnail(TRUE))
			->header('Content-Type','image/jpeg');
	}

	public function undelete(Photo $o)
	{
		$o->remove = NULL;
		$o->save();

		return redirect()->action('PhotoController@info',[$o->id]);
	}

	public function view(Photo $o)
	{
		return response($o->image())
			->header('Content-Type','image/jpeg');
	}
}