function downloadObjectAsJson(exportObj, exportName){
  var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj,null,2));
  var downloadAnchorNode = document.createElement('a');
  downloadAnchorNode.setAttribute("href",     dataStr);
  downloadAnchorNode.setAttribute("download", exportName + ".json");
  document.body.appendChild(downloadAnchorNode); // required for firefox
  downloadAnchorNode.click();
  downloadAnchorNode.remove();
}

function getAllFragments() {
	out = {}
	for(var storename in ITowns){
		
		if(typeof ITowns[storename] === 'object' && "fragments" in ITowns[storename]){
			out[storename] = {}
			store = ITowns[storename].fragments
			for(var townid in store) {
				out[storename][townid] = []
				for(var i = 0; i < store[townid].models.length;i++){
					out[storename][townid].push(store[townid].models[i].attributes)
				}
			}
		}
	}
	return out
}

function makeObject() {
	out = {}
	for(var townid in ITowns.towns) {
	    out[townid] = {}
		out[townid].name = ITowns.towns[townid].name
		out[townid].points = ITowns.towns[townid].points
		out[townid].god = ITowns.towns[townid].god()
		out[townid].unitsTotal = ITowns.towns[townid].units()
		out[townid].supportTotal = ITowns.towns[townid].unitsSupport()
		out[townid].outerTotal = ITowns.towns[townid].unitsOuter()
	    out[townid].resources = ITowns.towns[townid].resources()
	    out[townid].buildings = ITowns.towns[townid].getBuildings().getBuildings()
	    out[townid].researches = ITowns.towns[townid].researches().attributes
		out[townid].espstorage = ITowns.towns[townid].getEspionageStorage()
		
		orderz = ITowns.towns[townid].buildingOrders()
		out[townid].buildingOrders = []
		for(var i = 0; i<orderz.length;i++) {
			order = orderz.models[i].attributes
			out[townid].buildingOrders.push({
				buildingTime: order.building_time,
				building: order.building_type,
				beginTime: order.created_at,
				tearingDown: order.tear_down,
				endTime: order.to_be_completed_at,
				refund: order.cancel_refund,
				cost: {wood: order.wood, stone: order.stone, iron: order.iron}
			})
		}
		recruitments = ITowns.all_remaining_unit_orders.fragments[townid]
		out[townid].recruitingOrders = []
		for(var i = 0; i<recruitments.models.length; i++) {
			order = recruitments.models[i].attributes
			out[townid].recruitingOrders.push({
				count: order.count,
				beginTime: order.created_at,
				kind: order.kind,
				done: order.parts_done,
				refund: order.refund_for_single_unit,
				endTime: order.to_be_completed_at,
				unit: order.unit_type,
				cost: {wood: order.wood, stone: order.stone, iron: order.iron, favor: order.favor}
			})
		}
	
		castedPowers = ITowns.all_casted_powers.fragments[townid]
		out[townid].castedPowers = []
		for(var i = 0; i<castedPowers.models.length; i++) {
			order = castedPowers.models[i].attributes
			out[townid].castedPowers.push({
				configuration: order.configuration,
				endTime: order.to_be_completed_at,
				extended: order.extended,
				level: order.level,
				originPlayer: order.origin_player_id,
				power: order.power_id
			})
		}
		
		researches = MM.getTownAgnosticCollectionsByName("ResearchOrder")[0].fragments[townid]
		out[townid].researchOrders = []
		for(var i = 0; i<researches.models.length; i++) {
			order = researches.models[i].attributes
			out[townid].researchOrders.push({
				research: order.research_type,
				beginTime: order.created_at,
				endTime: order.to_be_completed_at,
				refund: order.cancel_refund
			})

		}
		/*
		militia = MM.getModels().Militia
		if(townid in militia){
			out[townid].militia = {
				start : militia[townid].attributes.started_at,
				end: militia[townid].attributes.finished_at
			}
		} else {
			out[townid].militia = null
		}*/
		out[townid].militia = null
		
	}
	celebrations = MM.getModels().Celebration
	outCele = []
	for (var i in celebrations) {
		outCele.push({
			town: celebrations[i].attributes.townid,
			type: celebrations[i].attributes.celebration_type,
			end: celebrations[i].attributes.finished_at,
		})
	} 
	mvts = MM.getModels().MovementsUnits
		outMvts = []
		for (var i in mvts) {
			mvt = mvts[i].attributes
			outMvts.push({
				arrival: mvt.arrival_at,
				cancelableUntil: mvt.cancelable_until,
				invisibleUntil: mvt.cap_of_invisibility_effective_until,
				destIsAttackSpot: mvt.destination_is_attack_spot,
				origIsAttackSpot: mvt.origin_is_attack_spot,
				homeId: mvt.home_town_id	,
				player: mvt.player_id,
				start: mvt.started_at,
				targetId: mvt.target_town_id,
				type: mvt.type,
			})
		trdz = MM.getModels().Trade
		outTrd = []
		for (var i in trdz) {
			trd = trdz[i].attributes
			outTrd.push({
				start: trd.started_at,
				arrival: trd.arrival_at,
				dest: trd.destination_town_id,
				destType: trd.destination_town_type,
				gold: trd.gold,
				stone: trd.stone,
				wood: trd.wood,
				iron: trd.iron,
				orig: trd.origin_town_id,
				origType: trd.origin_town_type,
				exchange: trd.in_exchange
			})
		}
	}
	fav = MM.getModels().PlayerGods[Object.keys(MM.getModels().PlayerGods)[0]].attributes
	outFav = {
		aphrodite: fav.aphrodite_favor,
		ares: fav.ares_favor,
		artemis: fav.artemis_favor,
		athena: fav.athena_favor,
		fury: fav.fury,
		hades: fav.hades_favor,
		hera: fav.hera_favor,
		poseidon: fav.poseidon_favor,
		zeus: fav.zeus_favor
	}
	utz = MM.getModels().Units
	outUts = []
	for (var i in utz) {
		outUts.push({
			orig: utz[i].getOriginTownId(),
			curr: utz[i].getCurrentTownId(),
			units: utz[i].getUnits(),
			sameIsland: utz[i].isSameIsland()
		})
	}
	return {towns: out,celebrations: outCele, movements: outMvts, trades: outTrd, favors: outFav, units: outUts}
}
downloadObjectAsJson(makeObject(),"greposimu")


