<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;

use Cms\Classes\Controller;
use BackendMenu;

use Illuminate\Http\Request;
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
use Illuminate\Support\Facades\Validator;
use Tps\Tps\Models\Projects;
class ProjectsController extends Controller
{
	protected $Projects;

    protected $helpers;

    public function __construct(Projects $Projects, Helpers $helpers)
    {
        parent::__construct();
        $this->Projects    = $Projects;
        $this->helpers          = $helpers;
    }

    public function index()
    {
        $data = $this->Projects->all()->toArray();
    
        $baseUrl = url('/storage/app/media');
    
        foreach ($data as &$project) {
            $project['image'] = $baseUrl . $project['image'];
        }
    
        return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
    }
    


    public function show($id){

        $data = $this->Projects::find($id);

        if ($data){
            return $this->helpers->apiArrayResponseBuilder(200, 'success', [$data]);
        } else {
            $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
        }

    }

    public function store(Request $request){

    	$arr = $request->all();

        while ( $data = current($arr)) {
            $this->Projects->{key($arr)} = $data;
            next($arr);
        }

        $validation = Validator::make($request->all(), $this->Projects->rules);
        
        if( $validation->passes() ){
            $this->Projects->save();
            return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->Projects->id]);
        }else{
            return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors() );
        }

    }

    public function update($id, Request $request){

        $status = $this->Projects->where('id',$id)->update($data);
    
        if( $status ){
            
            return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been updated successfully.');

        }else{

            return $this->helpers->apiArrayResponseBuilder(400, 'bad request', 'Error, data failed to update.');

        }
    }

    public function delete($id){

        $this->Projects->where('id',$id)->delete();

        return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
    }

    public function destroy($id){

        $this->Projects->where('id',$id)->delete();

        return $this->helpers->apiArrayResponseBuilder(200, 'success', 'Data has been deleted successfully.');
    }


    public static function getAfterFilters() {return [];}
    public static function getBeforeFilters() {return [];}
    public static function getMiddleware() {return [];}
    public function callAction($method, $parameters=false) {
        return call_user_func_array(array($this, $method), $parameters);
    }
    
}