/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2010 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.gallery3.rest {
	import mx.rpc.AsyncToken;

	public class Gallery3 {
		public static var ROOT_URI:String = "item/1";
		
		private static var _urlBase:String;
		private static var _apiKey:String;

		public static function get urlBase(): String {
			return Gallery3._urlBase;
		}		
		public static function set urlBase(value:String): void {
			Gallery3._urlBase = value;
		}	

		public static function get apiKey(): String {
			return Gallery3._apiKey;
		}		
		public static function set apiKey(value:String): void {
			Gallery3._apiKey = value;
		}	
			
  		protected var _url:String;
  		protected var _token:String;
  		protected var _data:Object;
 		protected var _file:String;
 		protected var _dataComplete: Function;
 		
		protected var _original_entity:Object;
  		
		public function Gallery3() {
	    	this._data = {entity: new Object()};
	    	this._token = null;
	    	this._url = null;
	    	this._file = null;
		}

		/**
	   	 * Connect to a remote Gallery3 instance
	     *
	     * @param   string Gallery 3 API url, eg http://example.com/gallery3/index.php/rest
	     * @param   string username
	     * @param   string password
	     * @return  string authentication token
	     */
	  	public static function login(url:String, user:String, pass:String):Object {
//	    	var response:Object = RestHelper::request(
//	      		"post", url, null, {user: user, password: pass});
//	    	return response;
			return "";
	  	}
	
	  	/**
	   	 * Construct a new Gallery3 instance associated with a remote resource
	   	 * @param   string remote url
	   	 * @param   string authentication token
	   	 * @return  object Gallery3
	   	 */
	  	public static function factory(url:String=null, token:String=null):Gallery3 {
	    	var obj:Gallery3 = new Gallery3();
	    	obj._token = token;
	    	obj._url = url;
	    	return obj;
	  	}

		/**
		 * Set a value on the remote resource.  You must call save for it to take effect.
		 *
		 * @param string   key
		 * @param string   value
		 * @return object  Gallery3
		 * @chainable
		 */
		public function set_value(key:String, value:String):Gallery3 {
			this._data.entity[key] = value;
			return this;
		}
		
		/**
		 * Attach a file to the remote resource.
		 *
		 * @param string   path to a local file (eg: /tmp/foo.jpg)
		 * @return object  Gallery3
		 */
		public function file(file:String):Gallery3 {
			this._file = file;
			return this;
		}

		/**
		 * Save any local changes made to this resource.  If this is an existing resource, we'll return
		 * the resource itself.  If we're creating a new resource, return the newly created resource.
		 *
		 * @return object  Gallery3
		 */
		public function create(url:String, token:String):Gallery3 {
//			var response:Object = RestHelper::request(
//			  "post", url, token, this._data.entity, this._file);
//			this._url = response.url;
//			this._token = token;
//			return this.load();
			return this;
		}
		
		/**
		 * Save any local changes made to this resource.  If this is an existing resource, we'll return
		 * the resource itself.  If we're creating a new resource, return the newly created resource.
		 *
		 * @return object  Gallery3
		 */
		public function save():Gallery3 {
//			var response:Object = Gallery3_Helper::request(
//			  "put", this._url, this._token,
//			  array_diff(this._original_entity, (array)$this._data.entity));
//			return this.load();
			return this;
		}
		
		/**
		 * Delete the remote resource.
		 *
		 * @return object  Gallery3
		 */
		public function deleteItem():Gallery3 {
//			RestHelper::request("delete", this._url, this._token);
//			this._data = {entity: new Object()};
//			this._url = null;
			return this;
		}

        public function onDataComplete(onDataComplete:Function):Gallery3 {
        	this._dataComplete = onDataComplete;
        	return this;
        }

		/**
		 * Reload the resource from a given url.  This is useful after the remote resource has been
		 * modified.
		 *
		 * @return object   Gallery3
		 */
		public function load(params:Object=null):Gallery3 {
			var clazz:Gallery3 = this;
			var result:AsyncToken = Rest.factory()
				.method("get")
				.url(this._url)
				.token(this._token)
				.resultHandler(function(statusCode:int, data:Object):void {
					_data = data;
					_original_entity = clazz._data.entity;
					_dataComplete(data);
				})
				.load(params);
			return this;
		}
	}
}