<?php

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Validator;
use App\Picture;
use App\WorkOrderVisit;
use App\Http\Requests\PictureRequest;

class PictureController extends Controller
{
	
	public function index(Request $request)
    {

		$pictures = Picture::where('company_id', $this->_getCompany()->id);
		
		if (isset($request->work_order_visit_id) && $request->work_order_visit_id) {
			$pictures = $pictures->where('work_order_visit_id', $request->work_order_visit_id);
		}
		
		if (isset($request->property_id) && $request->property_id) {
			$pictures = $pictures->where('property_id', $request->property_id);
		}
		
		$pictures = $pictures->orderBy('created_at', 'asc')->get();

		return response()->json($pictures, 200);
	
    }

    public function store(PictureRequest $request)
    {
		
		$picture = $this->_save($request);

		if ($picture) {			
			return response()->json([
				'title' => 'Picture',
				'message' => 'Picture was added successfully.',
				'object' => $picture
			], 201);
		} else {
			return response()->json([
				'title' => 'Picture',
				'message' => 'It was not possible to add the Picture.'
			], 500);
		}
	
    }

	public function show($id)
    {

		$picture = Picture::find($id);

		if (!$picture || $picture->company_id != $this->_getCompany()->id) {
			return response()->json([
				'title' => 'Picture',
				'message' => 'Record not found'
			], 404);
		}

		return response()->json($picture, 200);
	
    }

    public function update(PictureRequest $request, $id)
    {
		
		$picture = $this->_save($request, $id);

		if ($picture) {
			return response()->json([
				'title' => 'Picture',
				'message' => 'Picture was edited successfully.',
				'object' => $picture
			], 200);
		} else {
			return response()->json([
				'title' => 'Picture',
				'message' => 'It was not possible to edit the Picture.'
			], 500);
		}
		
    }
	
    public function destroy($id)
    {

		$picture = Picture::find($id);

		if (!$picture) {
			return response()->json([
				'title' => 'Picture',
				'message' => 'Record not found',
			], 404);
		}

		$picture->delete();
		
		return response()->json([
				'title' => 'Picture',
				'message' => 'Picture was deleted successfully.'
			], 200);
	
    }
	
	private function _save(Request $request, $id = null) 
    {

		if ($id) {

			$picture = Picture::find($id);

			if (!$picture || $picture->company_id != $this->_getCompany()->id) {
				return response()->json([
					'title' => 'Picture',
					'message' => 'Record not found',
				], 404);
			}

		} else {

			$picture = new Picture();
			
			$picture->created_at = date('Y-m-d H:i:s');

		}
		
		$work_order_visit = WorkOrderVisit::find($request->work_order_visit_id);
		
		if ($work_order_visit) {
			$picture->property_id = $work_order_visit->work_order->property_id;
		}
		
		$picture->updated_at = date('Y-m-d H:i:s');
		$picture->company_id = $this->_getCompany()->id;
		$picture->work_order_visit_id = $request->work_order_visit_id;
		$picture->url = $request->url;
		$picture->when = $request->when;

        if ($picture->save()) {
			return $picture;
		}

        return null;

    }

}
