<?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\Cartoons;
class CartoonsController extends Controller
{
	protected $Cartoons;

    protected $helpers;

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

    public function index()
    {
        $data = $this->Cartoons->all()->toArray();
        
        $baseUrl = url('/storage/app/media');
        
        foreach ($data as &$project) {
            $project['list_image'] = $baseUrl . $project['list_image'];
            $project['banner_image'] = $baseUrl . $project['banner_image'];
            $project['logo'] = $baseUrl . $project['logo'];
            $project['main_characters_image'] = $baseUrl . $project['main_characters_image'];
            $project['shots_image'] = $baseUrl . $project['shots_image'];
            $project['posters_image'] = $baseUrl . $project['posters_image'];

            if(!is_null($project['list_characters']))
                foreach ($project['list_characters'] as &$character) 
                    $character['characters_image'] = $baseUrl . $character['characters_image'];

            if(!is_null($project['list_shots']))
                foreach ($project['list_shots'] as &$shot)
                    $shot['shot_image'] = $baseUrl . $shot['shot_image'];

            if(!is_null($project['list_shots']))
                foreach ($project['list_posters'] as &$poster) 
                    $poster['image'] = $baseUrl . $poster['image'];
        }
        
        return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
    }


    public function show($id){

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

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

        }

    }

    public function store(Request $request){

    	$arr = $request->all();

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

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

    }

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

        $status = $this->Cartoons->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->Cartoons->where('id',$id)->delete();

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

    public function destroy($id){

        $this->Cartoons->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) {
        if(isset($parameters['cartoon'])) $parameters = [$parameters['cartoon']];
        return call_user_func_array(array($this, $method), $parameters);
    }


    
    
    
}