<?php

require_once 'CoreRESTfulBridge.php';
require_once 'ImapConnector.php';

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$import = false;


if($import) {


echo "testSave...\n";

$imap = new ImapConnector("imap.aerea.de", "soeren.sprenger@aerea.de", "plaintextpw", true, true, false);
if ($imap->connect()) {
	echo "connected.\n";
} else {
	echo "error connecting.\n";
	die();
}

echo "There are ".$imap->messageCount()." Messages on the Server.\n";

//echo "These are the imap folders:\n";
//var_dump($imap->getFolders());

                //$imap->cd("Gesendet");
                //echo "in Gesendet are: ".$imap->messageCount()." messages.\n";
                //$imap->disconnect();
$count = $imap->messageCount();

for ($i = 1; $i <= $count; $i++) {

	$msg = $imap->getEMailMessageById($i);
	$attr = array();

	$attr['timestamp']     = $msg->timestamp();
	$attr['subject']       = $msg->subject();
	$attr['from_personal'] = $msg->from_personal();
	$attr['to_personal']    = $msg->to_personal();
	$attr['body_raw']      = $msg->getBody();


	//if($attr['subject'] == "Happy New Year!") {
		echo "saving mail: \"" .$attr['subject']."\" from: \"".$attr['from_personal']."\"...\n";
	        //print_r($attr);

		$json = coreRESTfulBridge::save("email", $attr);
		if($json->status != "ok") {
			die("error");
		} else {
			echo "saved id: ".$json->id."\n";
		}
	//}


	if ($i == 300) {
//		break;
	}
	
}
}

//$user = array( "uname" => "test", "firstname" => "Test2", "lastname" => "Foobar" );

//$ret = coreRESTfulBridge::save("users", $user);

//$id = $ret->id;


echo "testFindAll...\n";

$attributes = array($argv[1] => $argv[2], $argv[3] => $argv[4]);

//print_r(coreRESTfulBridge::findByPK("users", $id));

//die();

	$time_start = microtime_float();
	$ret = coreRESTfulBridge::findAll("wiki",20 , 0);
	$time_end = microtime_float();
	$time = $time_end - $time_start;
	foreach($ret->result as $found) {
		echo "Found: ".$found->id." (".$found->name.")\n";
		foreach($found->attributes as $key => $value) {
			if ($key == "text") {
				continue;
			}
			echo "$key:\t\t $value\n";
		}
			

	}
	//print_r($ret);
	echo "found ".$ret->count." sets in $time seconds.\n";

$time_end = microtime_float();
$time = $time_end - $time_start;

//echo "found ".$ret->count." sets in $time seconds.\n";




