"use strict";

/* --== IMAGE LOAD ROUTINES ==-- */

var LOAD_IMAGES_REQUESTED = 0;
var LOAD_IMAGES_COMPLETED = 0;
var LOAD_PROBLEM_IMAGES = {};
function SDS_Load_imgComplete(){
	return (LOAD_IMAGES_REQUESTED==LOAD_IMAGES_COMPLETED);
}

function SDS_Load_Image(path,cb,anon){
	cb = cb ? cb : noop;
	var img = new Image();
	if (anon) img.setAttribute('crossOrigin', 'anonymous'); // hack for the map editor to, when run from a server, export canvases back to png
	img.isLoaded = false;
	LOAD_IMAGES_REQUESTED++;
	img.onload = function(){_SDS_Load_checkImg(img); cb();}
	img.src = path;

	return img;
}

function _SDS_Load_checkImg(img){
	if (img.width && img.height){
		img.isLoaded=true;
		delete LOAD_PROBLEM_IMAGES[img.src];
		LOAD_IMAGES_COMPLETED++;
	}
	else{
		LOAD_PROBLEM_IMAGES[img.src] = (LOAD_PROBLEM_IMAGES[img.src]) ? LOAD_PROBLEM_IMAGES[img.src]+1 : 1;
		setTimeout(function(){_SDS_Load_checkImg(img);}, 250);
	}
}

/* --== MAP LOAD ROUTINES ==-- */
var SDS_MAP_PALETTES = {};
var _SDS_LOAD_PALETTES_COMPLETE = false;
function _SDS_Load_Map_Palettes(){
	
	for (var terrainName in DATA_SDS_MAP_PALETTES){
		var t=DATA_SDS_MAP_PALETTES[terrainName];
		var imageName = (t.sheetImg) ? t.sheetImg : terrainName;
		var sheetImg = SDS_Load_Image("art/sheets/terrain/"+imageName+".png");
		var terrainSheet = new TileSheet(4,4, t.width, t.height, sheetImg);

		var palette = new MapTilePalette(terrainSheet, DEFINE_MAP_TERRAIN_TYPES.GROUND, null, t.minFeatureWidth, t.minFeatureHeight);

		var numTiles = (1<<t.width)<<t.height;
		for (var i=0; i<numTiles; i++){
			palette.map_tiles[i].terrain_type = t.terrain[i];
			if (t.detail[i]!=null){
				palette.map_tiles[i].terrain_detail = t.detailMaps[t.detail[i]];
			}
		}

		SDS_MAP_PALETTES[terrainName] = palette;
	}

	_SDS_LOAD_PALETTES_COMPLETE = true;
}

function _SDS_Parse_MapLayer(layerData){
	var layerPalName = layerData.palette;
	var indexBase = Math.max(SDS_MAP_PALETTES[layerPalName].tile_sheet.sheet_width,SDS_MAP_PALETTES[layerPalName].tile_sheet.sheet_height);
	indexBase = 1<<indexBase;
	var layout = [];
	for (var row=0; row<layerData.layout.length; row++){
		var layoutRow = [];
		for (var col=0; col<layerData.layout[row].length; col++){
			layoutRow.push(parseInt(layerData.layout[row][col],indexBase));
		}
		layout.push(layoutRow);
	}

	return new MapLayer(layout[0].length, layout.length, SDS_MAP_PALETTES[layerPalName], layout)
}

var SDS_MAP_ROOMS = {};
var _SDS_LOAD_ROOMS_COMPLETE = false;
function _SDS_Load_Map_Rooms(){

	for (var roomName in DATA_SDS_MAP_ROOMS){
		var r=DATA_SDS_MAP_ROOMS[roomName];
		var terrainName = r.palette;//"sea1";

		var indexBase = Math.max(SDS_MAP_PALETTES[terrainName].tile_sheet.sheet_width,SDS_MAP_PALETTES[terrainName].tile_sheet.sheet_height);
		indexBase = 1<<indexBase;

		var layout = [];
		var layoutRow;
		for (var row=0; row<r.layout.length; row++){
			layoutRow = [];
			for (var col=0; col<r.layout[row].length; col++){
				layoutRow.push(parseInt(r.layout[row][col],indexBase));
			}
			layout.push(layoutRow);
		}

		var backgrounds = [];
		for (var i = 0; i<r.backgrounds.length; i++){
			var bg = r.backgrounds[i];
			backgrounds.push(_SDS_Parse_MapLayer(bg));
		}

		var foregrounds = [];
		for (var i = 0; i<r.foregrounds.length; i++){
			var fg = r.foregrounds[i];
			foregrounds.push(_SDS_Parse_MapLayer(fg));
		}

		var water = null;
		if (r.waterLevel!=null){
			water = new MapWaterLayer(r.waterLevel.surfaceY, parseInt(r.waterLevel.surfaceTile,indexBase), parseInt(r.waterLevel.fillTile,indexBase));
		}
		
		SDS_MAP_ROOMS[roomName] = new MapRoom(roomName,layout[0].length, layout.length, r.margin, SDS_MAP_PALETTES[terrainName], layout, backgrounds, foregrounds, water, r.objects, r.decorations, r.bgm);
	}

	_SDS_LOAD_ROOMS_COMPLETE = true;
	
}

function _SDS_Load_Maps(){
	_SDS_Load_Map_Palettes();
	_SDS_Load_Map_Rooms();
}

/* --== ART LOAD ROUTINES ==-- */

var SDS_STATIC_ART = {};
function  _SDS_Load_Statics(){
	for (var art in DATA_SDS_STATIC_SRC){
		SDS_STATIC_ART[art] = SDS_Load_Image(DATA_SDS_STATIC_ART_ROOT+DATA_SDS_STATIC_SRC[art]);
	}
}

/* --== LOAD META HANDLERS ==-- */

/* loads all of the things; calls callback when done */
function SDS_Load(callback){
	_SDS_Load_Maps();
	_SDS_Load_Statics();

	_SDS_Finish_Load(callback);
}

/* calls callback when all images requested so far have loaded */
function _SDS_Finish_Images(callback){
	if (SDS_Load_imgComplete()){
		callback();
	}
	else{
		setTimeout(function(){_SDS_Finish_Images(callback);},250);
	}

}

function _SDS_Finish_Load(callback){
	if (	SDS_Load_imgComplete()
			&& _SDS_LOAD_PALETTES_COMPLETE
			&& _SDS_LOAD_ROOMS_COMPLETE
		){
		callback();
	}
	else{
		setTimeout(function(){_SDS_Finish_Load(callback);},250);
	}

}