<?php

namespace App\Http\Controllers;

use App\Container;
use App\File;
use App\Item;
use App\Event;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ItemController extends Controller
{

    public function showAllItems()
    {
        return response()->json(Item::all());
    }

    public function showByEvent($event)
    {
        $eid = Event::where('slug','=',$event)->first()->eid;
        $q = Item::byEvent($eid);
        return response()->json($q->get());
    }

    public function searchByEvent($event, $query)
    {
        $eid = Event::where('slug','=',$event)->first()->eid;
        $query_tokens = explode(" ",base64_decode ( $query , true));
        $q = Item::byEvent($eid);
        foreach ($query_tokens as $token)
            if(!empty($token))
                $q = $q->where('items.description','like','%'.$token.'%');
        return response()->json($q->get());
    }

    public function showOneItem($event, $id)
    {
        $eid = Event::where('slug','=',$event)->first()->eid;
        return response()->json(Item::byEvent($eid)->where('uid', '=', $id)->first());
    }

    public function create($event, Request $request)
    {
        $eid = Event::where('slug','=',$event)->first()->eid;
        $newitem = $request->except(['dataImage']);
        $newitem['eid'] = "".$eid;
        $item = Item::create($newitem);

        if ($request->get('dataImage')) {
            $pos = strpos($request->get('dataImage'), ",");
            //$head = substr($request->get('dataImage'), 0,  $pos);
            $image = base64_decode(substr($request->get('dataImage'), $pos + 1), true);
            if ($image) {
                $hash = md5(time());
                if (!file_exists('staticimages'))
                    mkdir('staticimages', 0755, true);
                file_put_contents('staticimages/' . $hash, $image);
                $file = File::create(array('hash' => $hash, 'iid' => $item['iid']));
                $item['file'] = $hash;
            }
        }
        return response()->json($item, 201);
    }

    public function update($event, $id, Request $request)
    {
        $eid = Event::where('slug', $event)->first()->eid;
        $item = Item::where('eid', $eid)->where('uid', $id)->first();
        $item->update($request->except(['file', 'box', 'dataImage']));

        if($request->get('returned')===true){
            $item->update(['returned_at' => DB::raw( 'current_timestamp' )]);
        }

        if ($request->get('dataImage')) {
            $pos = strpos($request->get('dataImage'), ",");
            //$head = substr($request->get('dataImage'), 0,  $pos);
            $image = base64_decode(substr($request->get('dataImage'), $pos + 1), true);
            if ($image) {
                $hash = md5(time());
                if (!file_exists('staticimages'))
                    mkdir('staticimages', 0755, true);
                file_put_contents('staticimages/' . $hash, $image);
                $file = File::create(array('hash' => $hash, 'iid' => $item['iid']));
                $item['file'] = $hash;
            }
        }
        return response()->json(Item::find($item['uid']), 200);
    }

    public function delete($event, $id)
    {
        $eid = Event::where('slug','=',$event)->first()->eid;
        Item::where('eid', $eid)->where('uid', $id)->first()->delete();
        return response()->json(array("status"=>'Deleted Successfully'), 200);
    }
}