dojo.provide("tests.webmap.data");
dojo.require("doh.runner");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("webmap.data");

var xyData = {identifier:"f0",
	aliases: {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"},
	items: [
		{"f0": "key0", "f1": "1", "f2": "4", "f3": "0.Value 3rd" },
		{"f0": "key1", "f1": "2", "f2": "3", "f3": "1.Value 3rd" },
		{"f0": "key2", "f1": "3", "f2": "2", "f3": "2.Value 3rd" },
		{"f0": "key3", "f1": "4", "f2": "1", "f3": "3.Value 3rd" },
		{"f0": "key4", "f1": "2", "f2": "3", "f3": "4.Value 3rd" },
		{"f0": "key5", "f1": "3", "f2": "2", "f3": "5.Value 3rd" }
	]
};
var jsonString = dojo.toJson(xyData);
var themeData = {identifier:"f0",
	aliases: {"key field":"f0", "x coord":"f1", "y coord":"f2", "Theme name":"f3"},
	items: [
		{"f0": "key0", "f1": "1", "f2": "4", "f3": "cd 1" },
		{"f0": "key1", "f1": "2", "f2": "3", "f3": "cd 2" },
		{"f0": "key2", "f1": "3", "f2": "2", "f3": "cd 1" },
		{"f0": "key3", "f1": "4", "f2": "1", "f3": "cd 3" },
		{"f0": "key4", "f1": "2", "f2": "3", "f3": "cd 3" },
		{"f0": "key5", "f1": "3", "f2": "2", "f3": "cd 4" }
	]
};

var dataTestFailure = function(e){
	//why?
	var m = "";
	if (!dojo.isIE) {
		for (var i = 0; i < e.message.length; i++)
			m +=  e.message[i];
	}else{
		m = e.message;
	}
	console.error(m);
}

doh.register("tests.webmap.data", 
	[
		{
			name: "construct_with_string",
			clientData: null,
			setUp: function(){
				this.clientData = new webmap.data.ClientDataStore({
					data:jsonString,
					xField: "f1",
					yField: "f2",
					identifyListFields: ["f0","f3"],
					identifyDetailFields: ["f0","f1","f2","f3"]
				});
			},
			runTest: function() {
				tests.assertEqual(xyData.identifier, this.clientData._id);
				tests.assertEqual(xyData.identifier, this.clientData._ds.identifier);
				tests.assertEqual(xyData.aliases, this.clientData._ds.aliases);
				tests.assertEqual(xyData.identifier, this.clientData._id);
				tests.assertEqual(this.clientData._ds._jsonData, xyData); //weird, but this fails if the params are reversed
				tests.assertEqual(jsonString, dojo.toJson(this.clientData._ds._jsonData));
			},
			tearDown: function(){
				this.clientData.destroy();
			}
		},
		{
			name: "construct_with_object",
			clientData: null,
			setUp: function(){
				this.clientData = new webmap.data.ClientDataStore({
					data: eval("(" + jsonString + ")"),
					xField: "f1",
					yField: "f2",
					identifyListFields: ["f0","f3"],
					identifyDetailFields: ["f0","f1","f2","f3"]
				});
			},
			runTest: function() {
				tests.assertEqual(xyData.identifier, this.clientData._id);
				tests.assertEqual(xyData.identifier, this.clientData._ds.identifier);
				tests.assertEqual(xyData.aliases, this.clientData._ds.aliases);
				tests.assertEqual(xyData.identifier, this.clientData._id);
				tests.assertEqual(this.clientData._ds._jsonData, xyData); //weird, but this fails if the params are reversed
				tests.assertEqual(jsonString, dojo.toJson(this.clientData._ds._jsonData));
			},
			tearDown: function(){
				this.clientData.destroy();
			}
		},
		{
			name: "getGrouped_not_theme",
			clientData: null,
			groupByXy: webmap.data.groupByXy,
			args: null,
			callback: function(){},
			setUp: function(){
				this.clientData = new webmap.data.ClientDataStore({
					data:jsonString,
					xField: "f1",
					yField: "f2",
					identifyListFields: ["f0","f3"],
					identifyDetailFields: ["f0","f1","f2","f3"]
				});
				var _this = this;
				webmap.data.groupByXy = function(args){_this.args = args;};
			},
			runTest: function() {
				this.clientData.getGrouped(this, "callback");
				tests.assertEqual(this.clientData._ds, this.args.datastore);
				tests.assertEqual(this.clientData._xFld, this.args.xFld);
				tests.assertEqual(this.clientData._yFld, this.args.yFld);
				tests.assertEqual(this, this.args.scope);
				tests.assertEqual("callback", this.args.success);
			},
			tearDown: function(){
				webmap.data.groupByXy = this.groupByXy;
				this.clientData.destroy();
			}
		},
		{
			name: "getGrouped_is_theme",
			clientData: null,
			setUp: function(){
				this.clientData = new webmap.data.ClientDataStore({
					data:jsonString,
					xField: "f1",
					yField: "f2",
					identifyListFields: ["f0","f3"],
					identifyDetailFields: ["f0","f1","f2","f3"]
				});
			},
			runTest: function() {
				console.warn("Wrtie me!"); try{notWritten++;}catch(ignore){}
			},
			tearDown: function(){
				this.clientData.destroy();
			}
		},
		{
			name: "getGrouped_alreday_grouped",
			clientData: null,
			callback: function(arg){tests.assertEqual(this.clientData.grouped, arg);},
			setUp: function(){
				this.clientData = new webmap.data.ClientDataStore({
					data:jsonString,
					xField: "f1",
					yField: "f2",
					identifyListFields: ["f0","f3"],
					identifyDetailFields: ["f0","f1","f2","f3"]
				});
				this.clientData.grouped = "already grouped";
			},
			runTest: function() {
				this.clientData.getGrouped(this, "callback");
			},
			tearDown: function(){
				this.clientData.destroy();
			}
		},
		{
			name: "groupByXy",
			store: null,
			args: null,
			delayedAssertions: function(grouped) {
				var expected = {"declaredClass": "gov.nyc.doitt.gis.service.webmap.domain.XyClientData", "preamble": null, "data": {"1@4": {"x": "1", "y": "4", "c": 1}, "2@3": {"x": "2", "y": "3", "c": 2}, "3@2": {"x": "3", "y": "2", "c": 2}, "4@1": {"x": "4", "y": "1", "c": 1}}, "envelope": {"minX": "1", "minY": "1", "maxX": "4", "maxY": "4", "declaredClass": "gov.nyc.doitt.gis.service.map.domain.MapEnvelope", "preamble": null}};
				if (dojo.isIE)
					expected = {"preamble": null, "declaredClass": "gov.nyc.doitt.gis.service.webmap.domain.XyClientData", "data": {"1@4": {"x": "1", "y": "4", "c": 1}, "2@3": {"x": "2", "y": "3", "c": 2}, "3@2": {"x": "3", "y": "2", "c": 2}, "4@1": {"x": "4", "y": "1", "c": 1}}, "envelope": {"preamble": null, "declaredClass": "gov.nyc.doitt.gis.service.map.domain.MapEnvelope", "minX": "1", "minY": "1", "maxX": "4", "maxY": "4"}};
				tests.assertEqual(expected, grouped);
				console.warn("woo hoo!");
			},
			failure: dataTestFailure,
			setUp: function(){
				this.store = new dojo.data.ItemFileReadStore({data:xyData});
				this.args = new Object();
				this.args.datastore = this.store;
				this.args.xFld = "f1";
				this.args.yFld = "f2";
				this.args.scope = this;
				this.args.success = "delayedAssertions";
				this.args.failure = "failure";
			},
			runTest: function() {
				webmap.data.groupByXy(this.args);
			}
		},
		{
			name: "groupByTheme",
			store: null,
			args: null,
			delayedAssertions: function(grouped) {
					var expected = {"themeName": "f3", "declaredClass": "gov.nyc.doitt.gis.service.webmap.domain.ThematicClientData", "preamble": null, "data": {"cd 1": 2, "cd 2": 1, "cd 3": 2, "cd 4": 1}, "envelope": {"minX": "1", "minY": "1", "maxX": "4", "maxY": "4", "declaredClass": "gov.nyc.doitt.gis.service.map.domain.MapEnvelope", "preamble": null}};
				if (dojo.isIE)
					expected = '{"preamble": null, "declaredClass": "gov.nyc.doitt.gis.service.webmap.domain.ThematicClientData", "themeName": "f3", "data": {"cd 1": 2, "cd 2": 1, "cd 3": 2, "cd 4": 1}, "envelope": {"preamble": null, "declaredClass": "gov.nyc.doitt.gis.service.map.domain.MapEnvelope", "minX": "1", "minY": "1", "maxX": "4", "maxY": "4"}}';
				tests.assertEqual(expected, grouped);
				console.warn("woo hoo!");
			},
			failure: dataTestFailure,
			setUp: function(){
				this.store = new dojo.data.ItemFileReadStore({data:themeData});
				this.args = new Object();
				this.args.datastore = this.store;
				this.args.xFld = "f1";
				this.args.yFld = "f2";
				this.args.themeFld = "f3";
				this.args.scope = this;
				this.args.success = "delayedAssertions";
				this.args.failure = "failure";
			},
			runTest: function() {
				webmap.data.groupByTheme(this.args);
			}
		},
		{
			name: "getList",
			clientData: null,
			delayedAssertions: function(list) {
				tests.assertEqual({"items": [{"f0": ["key1"], "f3": ["1.Value 3rd"]}, {"f0": ["key4"], "f3": ["4.Value 3rd"]}], "identifier": "f0", "aliases": {"key field": "f0", "x coord": "f1", "y coord": "f2", "Third field": "f3"}},
					list);
				console.warn("woo hoo!");
			},
			setUp: function(){
				this.clientData = new webmap.data.ClientDataStore({
					data:jsonString,
					xField: "f1",
					yField: "f2",
					identifyListFields: ["f0","f3"],
					identifyDetailFields: ["f0","f1","f2","f3"]
				});
			},
			runTest: function() {
				this.clientData.getList("2", "3", this, "delayedAssertions");
			},
			tearDown: function(){
				this.clientData.destroy();
			}
		},
		{
			name: "webmap.data.getFieldName",
			runTest: function(){
				tests.assertEqual("f0", webmap.data.getFieldName("key field", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("f1", webmap.data.getFieldName("x coord", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("f2", webmap.data.getFieldName("y coord", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("f3", webmap.data.getFieldName("Third field", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("a field", webmap.data.getFieldName("a field", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("a field", webmap.data.getFieldName("a field"));
			}
		},
		{
			name: "webmap.data.getFieldAlias",
			runTest: function(){
				tests.assertEqual("key field", webmap.data.getFieldAlias("f0", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("x coord", webmap.data.getFieldAlias("f1", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("y coord", webmap.data.getFieldAlias("f2", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("Third field", webmap.data.getFieldAlias("f3", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("fld", webmap.data.getFieldAlias("fld", {"key field":"f0", "x coord":"f1", "y coord":"f2", "Third field":"f3"}));
				tests.assertEqual("fld", webmap.data.getFieldAlias("fld"));
			}
		}
	]
);