<?php

    /**
     *  Main JSON Rest api for LGCCC Conferences
     */

    //Enable error reporting for dev only
        error_reporting(E_ERROR | E_PARSE);

    //Enable compression/output buffering
        if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();

    //Set required response headers
        header("Access-Control-Allow-Origin: *");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");         
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    }

    //Set JSON response format for header
        header("Content-Type: application/json; charset=UTF-8");
    
    //Include required files
    include_once( '../inc/db.php' );
    include_once( '../inc/galeria.php' );
    include_once( '../inc/helpers.php' );

	if($_REQUEST){
		$func = ($_REQUEST['f']!=''?$_REQUEST['f']:'list');
		$id = $_REQUEST['id'];
	}

    switch ( $func ){
        case("detail"):
            gallery_detail($id);
        break;
        case("list"):
        default:
            gallery_list();
        break;
    }

	function gallery_detail($id){
        $images=[];
        $image_ids = [];
        $database = new Database();
        $db = $database->getConnection();

        $galeria = new Galeria($db);

        $stmt = $galeria->gallery_images($id);
        
        $num = $stmt->rowCount();

        if($num>0){
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                extract($row);

                if($imagenes){
                    $ilist = unserialize($imagenes);
                    foreach($ilist as $i){
                        array_push($image_ids,$i);
                    }
                }
                if($sin_recortar){
                    $slist = unserialize($sin_recortar);
                    foreach($slist as $i){
                        array_push($image_ids,$i);
                    }
                }
                
            }
        }

        //Get all images from image_ids array
        foreach($image_ids as $image){
            $stmt = $galeria->image($image);
            $num = $stmt->rowCount();
            if($num>0){
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                    extract($row);
                    array_push($images,unserialize($imagedata));
                }
            }
        }

        $data = array(
            'count' => count($images),
            'result' => array (
                'images' => $images
            )
        );

        send_response( $data );
    }
	
    function gallery_list(){
        $database = new Database();
        $db = $database->getConnection();

        $galeria = new Galeria($db);

        $stmt = $galeria->gallery_list();

        $num = $stmt->rowCount();
        
        if($num>0){
            $galleries_arr=array();
        
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                extract($row);

                $response_item=array(
                    "title" => $post_title,
                    "id" => $ID,
                    "date" => $post_date,
                    "thumbnail" => $thumbnail
                );

                array_push($galleries_arr, $response_item);
            }
        }

        $data = array(
            'count' => count($galleries_arr),
            'result' => array (
                'galleries' => $galleries_arr
            )
        );

        send_response( $data );

    }

    function send_response( $data ){
        // set response code - 200 OK
	header('Content-Type: application/json; charset=utf-8');
	
        http_response_code(200);
       
        echo json_encode(
            $data, JSON_UNESCAPED_UNICODE
        );
    }