var _=require('underscore'),
URLUtils=require('./URLUtils'),
s3db=require('./s3db');

function init(s3,uri,endpoint_prefix)
{
	if(!s3 || !uri) throw new Error('init@ObjectDB@s3db: s3 and uri parameters cannot be empty');
	this.s3=s3;
	var info=URLUtils.parse_s3_uri(uri);
	if(!info) throw new Error('init@ObjectDB@s3db: Invalid S3 URI: '+uri);
	if(!endpoint_prefix) s3.setBucket(info['bucket']);
	else s3.setBucket(info['bucket']+'.'+endpoint_prefix);
	this.path=URLUtils.add_ending_slash(info['path']);
}
module.exports=init;

init.prototype.get=function(key,ok_func,error_func){
	try{
		if(!key || !_.isString(key)) throw new Error('Invalid or empty Key');
		this.s3.get(this.path+key,'buffer',function(error,response){
			try{
				if(error) throw error;
				if(!response.buffer || !Buffer.isBuffer(response.buffer)) throw new Error('Empty or invalid Buffer Object Returned');
				var data=response.buffer.toString('utf-8');
				data=JSON.parse(data);
				if(ok_func && _.isFunction(ok_func)) ok_func(data,response['headers']);
			}catch(e){
				if(error_func && _.isFunction(error_func)) error_func(e);
			}
		});	
	}catch(e){
		if(error_func && _.isFunction(error_func)) error_func(e);
	}
};

init.prototype.put=function(key,obj,ok_func,error_func){
	try{
		if(!key || !_.isString(key) || !obj) throw new Error('Invalid or empty Key Or empty data');
		obj=new Buffer(JSON.stringify(obj),'utf-8');
		this.s3.putBuffer(this.path+key,obj,'private',{
			'Content-Type':'application/json'
		},function(error,response){
			try{
				if(error) throw error;
				if(ok_func && _.isFunction(ok_func)) ok_func(response);
			}catch(e){
				if(error_func && _.isFunction(error_func)) error_func(e);
			}
		});	
	}catch(e){
		if(error_func && _.isFunction(error_func)) error_func(e);
	}
};

init.prototype.del=function(key,ok_func,error_func){
	try{
		if(!key || !_.isString(key)) throw new Error('Invalid or empty Key');
		this.s3.del(this.path+key,function(error,response){
			try{
				if(error) throw error;
				if(ok_func && _.isFunction(ok_func)) ok_func(response);
			}catch(e){
				if(error_func && _.isFunction(error_func)) error_func(e);
			}
		});	
	}catch(e){
		if(error_func && _.isFunction(error_func)) error_func(e);
	}
};


@@ -76,16 +76,32 @@
 ./s3db')
+,%0AQ=require('q')
 ;%0A%0Afunct
@@ -2216,28 +2216,784 @@
 unc)) error_func(e);%0A%09%7D%0A%7D;%0A%0A
+init.prototype.is_set=function(key,yes_func,no_func,error_func)%7B%0A%09var deferred = Q.defer();%0A%09try%7B%0A%09%09if(!key %7C%7C !_.isString(key)) throw new Error('Empty & invalid Key');%0A%09%09%09%09%09%09%0A%09%09this.s3.head(this.path+key,_.bind(function(error,response)%7B%0A%09%09%09if(error) %7B%0A%09%09%09%09if(e%5B'code'%5D && e%5B'code'%5D=='404') %7B%0A%09%09%09%09%09if(no_func && _.isFunction(no_func)) no_func(e.key,e);%0A%09%09%09%09%09deferred.resolve(false);%0A%09%09%09%09%09return;%09%0A%09%09%09%09%7D%0A%09%09%09%09error.key=key;%0A%09%09%09%09if(error_func && _.isFunction(error_func)) error_func(error);%0A%09%09%09%09deferred.reject(error);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09if(yes_func && _.isFunction(yes_func)) yes_func();%0A%09%09%09deferred.resolve(true);%09%0A%09%09%7D,this));%09%09%0A%09%7Dcatch(e)%7B%0A%09%09if(error_func && _.isFunction(error_func)) error_func(e);%0A%09%09deferred.reject(e);%0A%09%7D%0A%09return deferred.promise;%0A%7D;%0A%0A

add is_set method with promise support