dojo.provide("tests.webmap.WindowManager");

dojo.require("doh.runner");
dojo.require("webmap.WindowManager");
dojo.require("tests.Util");

doh.register("tests.webmap.WindowManager", 
	[
		{
			name: "constructor_destroy",
			manager: null,
			byId: dijit.byId,
			ids: [],
			setUp: function(){
				this.manager = new webmap.WindowManager();
				this.manager._windowIds = ["a","b","c"];
				var _this = this;
				dijit.byId = function(id){
					return {
						destroy: function(){_this.ids.push(id);}
					};
				};
			},
			runTest: function(){
				tests.assertEqual(5, this.manager._subscriptions.length);
				// TODO: Sarah - verify which subscriptions
				tests.assertEqual(new Array(), this.manager._windowIds);
				tests.assertEqual(webmap.widget.scrollTableViewTypes, this.manager._viewTypes);
				this.manager.destroy();
				tests.assertEqual(null, this.manager._subscriptions);
				tests.assertEqual(null, this.manager._windowIds);
				tests.assertEqual(null, this.manager._viewTypes);				
				tests.assertEqual(["a","b","c","webmapLegend"], this.ids);				
			},
			tearDown: function(){
				dijit.byId = this.byId;
			}
		},
		{
			name: "_themeChanged",
			manager: null,
			setUp: function(){
				this.manager = new webmap.WindowManager();
			},
			runTest: function(){
				this.manager._clientDataChanged = false;
				dojo.publish("webmap.widget.ThemeSelector.changeTheme",[]);
				tests.assertTrue(this.manager._legendChanged);
			},
			tearDown: function(){
				this.manager.destroy();
			}
		},
		{
			name: "_showCallout",
			manager: null,
			listArgs: null,
			detailArgs: null,
			listCount: 0,
			detailCount: 0,
			isDormant: webmap.isDormant,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				webmap.isDormant = function(){return false;};
				var _this = this;
				this.manager._showListCallout = function(args){_this.listCount++;_this.listArgs = args;};
				this.manager._showDetailCallout = function(args){_this.detailCount++;_this.detailArgs = args;};
			},
			runTest: function(){
				dojo.publish("webmap.MapManager.identified",[{data:{items:[1,2]}}]);
				tests.assertEqual({data:{items:[1,2]}}, this.listArgs);
				tests.assertEqual(1, this.listCount);
				dojo.publish("webmap.MapManager.identified",[{featureId:"1@2", data:{items:[]}}]);
				tests.assertEqual({featureId:"1@2", data:{items:[]}}, this.listArgs);
				tests.assertEqual(2, this.listCount);
				dojo.publish("webmap.MapManager.identified",[{featureId:"fid", data:{items:[1]}}]);
				tests.assertEqual({featureId:"1@2", data:{items:[]}}, this.listArgs);
				tests.assertEqual(2, this.listCount);
				tests.assertEqual({featureId:"fid", data:{items:[1]}}, this.detailArgs);
				tests.assertEqual(1, this.detailCount);
				dojo.publish("webmap.MapManager.identified",[{featureId:"fid", data:{items:[]}}]);
				tests.assertEqual({featureId:"1@2", data:{items:[]}}, this.listArgs);
				tests.assertEqual(2, this.listCount);
				tests.assertEqual({featureId:"fid", data:{items:[1]}}, this.detailArgs);
				tests.assertEqual(1, this.detailCount);
				webmap.isDormant = function(){return true;};
				dojo.publish("webmap.MapManager.identified",[]);
				tests.assertEqual({featureId:"1@2", data:{items:[]}}, this.listArgs);
				tests.assertEqual(2, this.listCount);
				tests.assertEqual({featureId:"fid", data:{items:[1]}}, this.detailArgs);
				tests.assertEqual(1, this.detailCount);
			},
			tearDown: function(){
				webmap.isDormant = this.isDormant;
				this.manager.destroy();
			}
		},
		{
			name: "_showListCallout",
			manager: null,
			subscription: null,
			connection: null,
			shownCount: 0,
			byIdCount: 0,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				var _this = this;
				this.subscription = dojo.subscribe("webmap.WindowManager.calloutShown", function(){_this.shownCount++;});
				this.connection = dojo.connect(dijit, "byId", function(id){_this.byIdCount++;});
			},
			runTest: function(){
				this.manager._showListCallout({featureId:"1@2",data:{identifier:"f0",items:[]},point:{x:1,y:2}});
				tests.assertEqual("cb1@2", this.manager._lastListCalloutId);
				tests.assertEqual(["cb1@2"], this.manager._windowIds);
				tests.assertEqual(1, this.shownCount);
				tests.assertEqual(3, this.byIdCount);
				var cb = dijit.byId("cb1@2");
				tests.assertEqual(1, cb.x);
				tests.assertEqual(2, cb.y);
				tests.assertEqual(40, cb.offset);
				tests.assertEqual(webmap.config.calloutListTitle, cb.title);
				tests.assertEqual(webmap.widget.scrollTableViewTypes.list, cb.viewType);
				tests.assertEqual("block", dojo.style(cb.domNode, "display"));
				cb.hide();
				this.manager._showListCallout({featureId:"1@2",data:{identifier:"f0",items:[]},point:{x:1,y:2}});
				tests.assertEqual(["cb1@2"], this.manager._windowIds);
				tests.assertEqual(2, this.shownCount);
				tests.assertEqual(6, this.byIdCount);
				var c = 0;
				dijit.registry.byClass(cb.declaredClass).forEach(function(){c++;});
				tests.assertEqual(1, c);
			},
			tearDown: function(){
				dojo.unsubscribe(this.subscription);
				dojo.disconnect(this.connection);
				this.manager.destroy();
			}
		},
		{
			name: "_showDetailCallout",
			manager: null,
			subscription: null,
			connection: null,
			shownCount: 0,
			byIdCount: 0,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				this.manager._showListCallout({featureId:"1@2",data:{identifier:"f0",items:[]},point:{x:1,y:2}});
				var _this = this;
				this.subscription = dojo.subscribe("webmap.WindowManager.calloutShown", function(){_this.shownCount++;});
				this.connection = dojo.connect(dijit, "byId", function(id){_this.byIdCount++;});
			},
			runTest: function(){
				this.manager._showDetailCallout({featureId:"fid",data:{identifier:"f0",items:[{f0:"fid"}]},point:{x:1,y:2}});
				tests.assertEqual(["cb1@2", "cbfid"], this.manager._windowIds);
				tests.assertEqual(2, this.shownCount);
				tests.assertEqual(5, this.byIdCount);
				var cb = dijit.byId("cbfid");
				tests.assertEqual(1, cb.x);
				tests.assertEqual(2, cb.y);
				tests.assertEqual(40, cb.offset);
				tests.assertEqual(webmap.config.calloutDetailTitle + " fid", cb.title);
				tests.assertEqual(webmap.widget.scrollTableViewTypes.detail, cb.viewType);
				tests.assertEqual("block", dojo.style(cb.domNode, "display"));
				cb.hide();
				this.manager._showDetailCallout({featureId:"fid",data:{identifier:"f0",items:[{f0:"fid"}]},point:{x:1,y:2}});
				tests.assertEqual(["cb1@2", "cbfid"], this.manager._windowIds);
				tests.assertEqual(4, this.shownCount);
				tests.assertEqual(10, this.byIdCount);
				var c = 0;
				dijit.registry.byClass(cb.declaredClass).forEach(function(){c++;});
				tests.assertEqual(2, c);
			},
			tearDown: function(){
				dojo.unsubscribe(this.subscription);
				dojo.disconnect(this.connection);
				this.manager.destroy();
			}
		},
		{
			name: "_destroyWindows_legendChanged_true",
			manager: null,
			byId: dijit.byId,
			disconnect: dojo.disconnect,
			windowIds: [],
			connections: [],
			mapData: null,
			count: 0,
			isDormant: webmap.isDormant,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				webmap.isDormant = function(){return false;};
				this.manager._legendChanged = true;
				this.manager._windowIds = ["a","b","c"];
				this.manager._connections = [1,2,3];
				var _this = this;
				this.manager._showLegend = function(arg){_this.count++;_this.mapData = arg;};
				dijit.byId = function(id){
					if (id == "webmapLegend"){
						_this.windowIds.push(id);
						return false;
					} 
					return {
						destroy: function(){_this.windowIds.push(id);}
					};
				};
				dojo.disconnect = function(c){_this.connections.push(c);};
			},
			runTest: function(){
				dojo.publish("webmap.MapManager.mapDataChanged", ["mapData"]);
				tests.assertEqual("mapData", this.mapData);
				tests.assertEqual(1, this.count);
				tests.assertFalse(this.manager._legendChanged);
				tests.assertEqual(["a","b","c"], this.windowIds);
				tests.assertEqual([1,2,3], this.connections);
				tests.assertEqual([], this.manager._windowIds);
				tests.assertEqual([], this.manager._connections);
			},
			tearDown: function(){
				webmap.isDormant = this.isDormant;
				dijit.byId = this.byId;
				dojo.disconnect = this.disconnect;
				this.manager.destroy();
			}
		},
		{
			name: "_destroyWindows_legendChanged_false_webmapLegend_false",
			manager: null,
			byId: dijit.byId,
			disconnect: dojo.disconnect,
			windowIds: [],
			connections: [],
			mapData: null,
			count: 0,
			isDormant: webmap.isDormant,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				webmap.isDormant = function(){return false;};
				this.manager._legendChanged = false;
				this.manager._windowIds = ["a","b","c"];
				this.manager._connections = [1,2,3];
				var _this = this;
				this.manager._showLegend = function(arg){_this.count++;_this.mapData = arg;};
				dijit.byId = function(id){
					if (id == "webmapLegend"){
						_this.windowIds.push(id);
						return false;
					} 
					return {
						destroy: function(){_this.windowIds.push(id);}
					};
				};
				dojo.disconnect = function(c){_this.connections.push(c);};
			},
			runTest: function(){
				dojo.publish("webmap.MapManager.mapDataChanged", ["mapData"]);
				tests.assertEqual("mapData", this.mapData);
				tests.assertEqual(1, this.count);
				tests.assertFalse(this.manager._legendChanged);
				tests.assertEqual(["webmapLegend", "a","b","c"], this.windowIds);
				tests.assertEqual([1,2,3], this.connections);
				tests.assertEqual([], this.manager._windowIds);
				tests.assertEqual([], this.manager._connections);
			},
			tearDown: function(){
				webmap.isDormant = this.isDormant;
				dijit.byId = this.byId;
				dojo.disconnect = this.disconnect;
				this.manager.destroy();
			}
		},
		{
			name: "_destroyWindows_legendChanged_false_webmapLegend_true",
			manager: null,
			byId: dijit.byId,
			disconnect: dojo.disconnect,
			windowIds: [],
			connections: [],
			mapData: null,
			count: 0,
			isDormant: webmap.isDormant,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				webmap.isDormant = function(){return false;};
				this.manager._legendChanged = false;
				this.manager._windowIds = ["a","b","c"];
				this.manager._connections = [1,2,3];
				var _this = this;
				this.manager._showLegend = function(arg){_this.count++;_this.mapData = arg;};
				dijit.byId = function(id){
					if (id == "webmapLegend"){
						_this.windowIds.push(id);
						return true;
					} 
					return {
						destroy: function(){_this.windowIds.push(id);}
					};
				};
				dojo.disconnect = function(c){_this.connections.push(c);};
			},
			runTest: function(){
				dojo.publish("webmap.MapManager.mapDataChanged", ["mapData"]);
				tests.assertEqual(null, this.mapData);
				tests.assertEqual(0, this.count);
				tests.assertFalse(this.manager._legendChanged);
				tests.assertEqual(["webmapLegend", "a","b","c"], this.windowIds);
				tests.assertEqual([1,2,3], this.connections);
				tests.assertEqual([], this.manager._windowIds);
				tests.assertEqual([], this.manager._connections);
			},
			tearDown: function(){
				webmap.isDormant = this.isDormant;
				dijit.byId = this.byId;
				dojo.disconnect = this.disconnect;
				this.manager.destroy();
			}
		},
		{
			name: "_showLegend_has_legend",
			manager: null,
			byId: dijit.byId,
			isDormant: webmap.isDormant,
			legend: null,
			widget: null,
			changeCount: 0,
			newCount: 0,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				webmap.isDormant = function(){return true;};
				dijit.byId = function(){return "legWidget";};
				var _this = this;
				this.manager._changeLegend = function(a0, a1){_this.changeCount++;_this.legend = a0;_this.widget = a1;};
				this.manager._newLegend = function(arg){_this.newCount++;_this.legend = arg;};
			},
			runTest: function(){
				this.manager._showLegend("mapData");
				tests.assertEqual(null, this.legend);
				tests.assertEqual(null, this.widget);
				tests.assertEqual(0, this.changeCount);
				tests.assertEqual(0, this.newCount);
				webmap.isDormant = function(){return false;};
				this.manager._showLegend({legend:"legend"});
				tests.assertEqual("legend", this.legend);
				tests.assertEqual("legWidget", this.widget);
				tests.assertEqual(1, this.changeCount);
				tests.assertEqual(0, this.newCount);
			},
			tearDown: function(){
				webmap.isDormant = this.isDormant;
				dijit.byId = this.byId;
				this.manager.destroy();
			}
		},
		{
			name: "_showLegend_no_legend",
			manager: null,
			byId: dijit.byId,
			isDormant: webmap.isDormant,
			legend: null,
			widget: null,
			changeCount: 0,
			newCount: 0,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				webmap.isDormant = function(){return true;};
				dijit.byId = function(){return false;};
				var _this = this;
				this.manager._changeLegend = function(a0, a1){_this.changeCount++;_this.legend = a0;_this.widget = a1;};
				this.manager._newLegend = function(arg){_this.newCount++;_this.legend = arg;};
			},
			runTest: function(){
				this.manager._showLegend("mapData");
				tests.assertEqual(null, this.legend);
				tests.assertEqual(null, this.widget);
				tests.assertEqual(0, this.changeCount);
				tests.assertEqual(0, this.newCount);
				webmap.isDormant = function(){return false;};
				this.manager._showLegend({legend:"legend"});
				tests.assertEqual("legend", this.legend);
				tests.assertEqual(null, this.widget);
				tests.assertEqual(0, this.changeCount);
				tests.assertEqual(1, this.newCount);
			},
			tearDown: function(){
				webmap.isDormant = this.isDormant;
				dijit.byId = this.byId;
				this.manager.destroy();
			}
		},
		{
			name: "_newLegend",
			manager: null,
			div: null,
			legend: null,
			appendCount: 0,
			win: null,
			positionCount: 0,
			mockLegend: {description:"description"},
			setUp: function(){
				this.manager = new webmap.WindowManager();
				var _this = this;
				this.manager._appendLegend = function(a0, a1){_this.appendCount++;_this.div = a0;_this.legend = a1;};		
				this.manager._positionLegend = function(a){_this.positionCount++;_this.win = a;};		
			},
			runTest: function(){
				this.manager._newLegend(this.mockLegend);
				var leg = dijit.byId("webmapLegend");
				tests.assertEqual(this.mockLegend.description, leg.title);
				tests.assertEqual(this.mockLegend, this.legend);
				tests.assertEqual(1, this.appendCount);
				tests.assertEqual(this.win, leg);
				tests.assertEqual(1, this.positionCount);
				tests.assertEqual("block", dojo.style(leg.domNode, "display"));
			},
			tearDown: function(){
				this.manager.destroy();
			}
		},
		{
			name: "_changeLegend",
			manager: null,
			legend: null,
			node: null,
			title: null,
			desc: null,
			appendCount: 0,
			titleCount: 0,
			mockLegend: {description:"description"},
			mockWidget: null,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				var _this = this;
				this.manager._appendLegend = function(a0, a1){_this.appendCount++;_this.node = a0;_this.legend = a1;};
				this.mockWidget = {contentNode:"node",setTitle: function(t0, t1){_this.titleCount++;_this.desc = t0;_this.title = t1;}};
			},
			runTest: function(){
				var _this = this;
				this.manager._changeLegend(this.mockLegend, this.mockWidget);
				tests.assertEqual(this.mockLegend, this.legend);
				tests.assertEqual(this.mockWidget.contentNode, this.node);
				tests.assertEqual(1, this.appendCount);
				tests.assertEqual(this.mockLegend.description, this.desc);
				tests.assertEqual("Legend", this.title);
				tests.assertEqual(1, this.titleCount);
			},
			tearDown: function(){
				this.manager.destroy();
			}
		},
		{
			name: "_appendLegend_ff",
			manager: null,
			div: null,
			legend: null,
			setUp: function(){
				if (dojo.isIE) return;
				this.manager = new webmap.WindowManager();
				this.div = document.createElement("div");
				document.body.appendChild(this.div);
				this.div.appendChild(document.createElement("div"));
				this.div.id = "div0";
				this.div.firstChild.id = "div1";
				this.legend = {
					markup: '<?xml version="1.0" encoding="utf-8"?>' +
						'<svg:svg version="1.1" xmlns:svg="http://www.w3.org/2000/svg">' +
						'<svg:polyline fill="cornsilk" stroke="black" stroke-width="1" points="">' +
						'</svg:polyline></svg:svg>'
				};
			},
			runTest: function(){
				if (dojo.isIE) return;
				this.manager._appendLegend(this.div, this.legend);
				tests.assertEqual("webmapLegendContent", this.div.className);
				tests.assertEqual(null, dojo.byId("div1"));
				tests.assertEqual('<svg svg="http://www.w3.org/2000/svg" version="1.1"><polyline points="" stroke-width="1" stroke="black" fill="cornsilk"></polyline></svg>',
					dojo.byId("div0").innerHTML);
			},
			tearDown: function(){
				if (dojo.isIE) return;
				this.manager.destroy();
				document.body.removeChild(this.div);
			}
		},
		{
			name: "_appendLegend_ie",
			manager: null,
			div: null,
			legend: null,
			setUp: function(){
				if (!dojo.isIE) return;
				this.manager = new webmap.WindowManager();
				this.div = document.createElement("div");
				document.body.appendChild(this.div);
				this.div.appendChild(document.createElement("div"));
				this.div.id = "div0";
				this.div.firstChild.id = "div1";
				this.legend = {
					markup: '<?xml version="1.0" encoding="utf-8"?>' +
						'<v:vml xmlns:v="urn:schemas-microsoft-com:vml">' +
						'<v:polyline fillcolor="#FFF8DC" strokecolor="black" strokeweight="1" points="">' +
						'</v:polyline></v:vml>'
				};
			},
			runTest: function(){
				if (!dojo.isIE) return;
				this.manager._appendLegend(this.div, this.legend);
				tests.assertEqual("webmapLegendContent", this.div.className);
				tests.assertEqual(null, dojo.byId("div1"));
				console.warn(dojo.byId("div0").innerHTML);
				tests.assertEqual('',
					dojo.byId("div0").innerHTML);
			},
			tearDown: function(){
				if (!dojo.isIE) return;
				this.manager.destroy();
				document.body.removeChild(this.div);
			}
		},
		{
			name: "_positionLegend",
			manager: null,
			mockWindow: {domNode:null},
			getViewport: dijit.getViewport,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				this.mockWindow.domNode = document.createElement("div");
				document.body.appendChild(this.mockWindow.domNode);
				dojo.style(this.mockWindow.domNode, "position", "absolute");
				dojo.style(this.mockWindow.domNode, "left", "100px");
				dojo.style(this.mockWindow.domNode, "top", "100px");
				dojo.style(this.mockWindow.domNode, "width", "100px");
				dojo.style(this.mockWindow.domNode, "height", "100px");
				dijit.getViewport = function(){return {l:10, t:20, w:200, h:100}};
			},
			runTest: function(){
				this.manager._positionLegend(this.mockWindow);
				tests.assertEqual("webmapLegendWindow", this.mockWindow.domNode.className);
				tests.assertEqual(106, dojo.style(this.mockWindow.domNode, "left"));
				tests.assertEqual(16, dojo.style(this.mockWindow.domNode, "top"));
			},
			tearDown: function(){
				this.manager.destroy();
				document.body.removeChild(this.mockWindow.domNode);
				dijit.getViewport = this.getViewport;
			}
		},
		{
			name: "_itemClick",
			manager: null,
			isDormant: webmap.isDormant,
			byId: dijit.byId,
			container: null,
			mockTable: {domNode:null},
			subscription: null,
			args: null,
			count: 0,
			setUp: function(){
				this.manager = new webmap.WindowManager();
				webmap.isDormant = function(){return false;};
				dijit.byId = function(id){return {x:1,y:2};};
				this.container = document.createElement("div");
				this.container.id = "container";
				document.body.appendChild(this.container);
				this.mockTable.domNode = document.createElement("div");
				this.mockTable.domNode.id = "table";
				this.container.appendChild(this.mockTable.domNode);
				var _this = this;
				this.subscription = dojo.subscribe("webmap.WindowManager.identifyItem",
					function(args){_this.count++;_this.args = args;});
			},
			runTest: function(){
				this.manager._itemClick({scrollTable:this.mockTable});
				tests.assertEqual({"scrollTable": {"domNode":this.mockTable.domNode}, "point": {"x": 1, "y": 2, "preamble": null, "declaredClass": "gov.nyc.doitt.gis.service.map.domain.ImagePoint"}, "layerId":webmap.config.clientDataLayerId},
					this.args);
				tests.assertEqual(1, this.count);
			},
			tearDown: function(){
				document.body.removeChild(this.container);
				webmap.isDormant = this.isDormant;
				dijit.byId = this.byId;
				this.manager.destroy();
			}
		}
	]
);