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

    protected $helpers;

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

    public function index()
    {
        $data = $this->Pages->with(['banner'])->get();
        
        $jsonFilePath = __DIR__.'/../../../../../storage/app/media/homePage/index.json';

        $youtubeLink = null;

        if (file_exists($jsonFilePath)) {
            $jsonContent = file_get_contents($jsonFilePath);
            $jsonData = json_decode($jsonContent, true);
            $youtubeLink = isset($jsonData['link']) ? $jsonData['link'] : null;
        }

        $baseUrl = url('/storage/app/media');
        
        $additionalData = [
            "banner_video_link" => $baseUrl."/homePage/index.mp4",
            "banner_video_image" => $baseUrl."/homePage/index.jpg",
            "youtube_link" => $youtubeLink
        ];

        foreach ($data as &$record) {
            if ($record->id == 1) {
                foreach ($additionalData as $key => $value) {
                    $record->$key = $value;
                }
            }
        }
        
        return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
    }

    public function show($id){

        $data = $this->Pages::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->Pages->{key($arr)} = $data;
            next($arr);
        }

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

    }

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

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

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

    public function destroy($id){

        $this->Pages->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);
    }
    
}