<?php

// $properties = $_GET['properties'];
// $limit = $_GET['limit'];
// $offset = $_GET['offset'];
// $group = $_GET['group'];
// $order = $_GET['join'];
// $filter = $_GET['filter'];
// $URI = $_GET['URI'];

$concept = isset( $_GET['concept'] ) ? $_GET['concept'] : false;
$id = isset( $_GET['id'] ) ? $_GET['id'] : false;
$filter = isset( $_GET['filter'] ) ? $_GET['filter'] : false;
$criteria =  isset( $_GET['criteria'] ) ? $_GET['criteria'] : false;

$criteria = $filter ? $criteria ?

	    array_merge( $criteria, array( 'filter' => $filter ) ):
 
	    array( 'filter' => $filter ):

	    $criteria;

$properties = ( $criteria && isset( $criteria['properties'] ) )? $criteria['properties']: false;

$service = ( $criteria && isset( $criteria['service'] ) )? $criteria['service']: false;

require_once 'api/controller.php';

///Conversor Para utf8 ante de codificar para json pois o json so funciona com utf8
function srtToUtf8($data)
{
    return mb_convert_encoding( $data , 'UTF-8' , 'UTF-8 , ISO-8859-1' );
}

function toUtf8($data)
{
    if(is_array($data))
    {   
        $return = array();
        foreach ($data as $i => $v)
           $return[srtToUtf8($i)] = (is_array($v)) ? toUtf8($v) : srtToUtf8($v);
        
        return $return;
    }
    else
       return srtToUtf8($data);
}
////////////////////////////////////////////////////////////////////////////////////////


echo json_encode( toUtf8(Controller::call( $id ? 'read' : 'find',
				    Controller::URI( $concept, $id, $service ),
				    $properties,
				    $criteria )) );

Controller::closeAll();