local actions = {
	["Start Selection"] = {
		store = function()
			local storedEnts = lume.map(selectedEntities,function(ent) return ent.id end)
			return storedEnts
		end,
		restore = function(storedEnts)
			lume.clear(selectedEntities)
			for i,v in ipairs(storedEnts) do 
				selectedEntities[i]=v
			end
		end
	}
}

function editor.pushUndo(target,state)
	if target.undone ~= time then 
		target.undone 	= time
		target.maxUndos = target.maxUndos or editor.maxUndos
		target.undoPos 	= target.undoPos or 0 

		if #target.undos>target.maxUndos then 
			table.remove(target.undos,1)  
			-- collectgarbage()
		end

		if target.undoPos>0 then 
			target.undos=lume.slice(target.undos,1,math.max(1,#target.undos-target.undoPos))
		end
		target.undoPos = 0

		table.insert(target.undos,state)
	end
end

function editor.undo(target)
	target.undoPos 	= target.undoPos~=nil and target.undoPos or 0 
	local first = target.undoPos==0
	local undoPos 	= mc( 0, shiftDown and target.undoPos-1 or target.undoPos+1, #target.undos)
	if undoPos ~= target.undoPos  then 
		if target.undoPos==0 then 
			undoPos=undoPos+1
			target.pushUndoState("",true)
		end
		tempTip(shiftDown and "Redo" or "Undo")
		target.undoPos = mc( 0, undoPos, #target.undos)
		target.lastState = target.undos[mc(1,#target.undos-target.undoPos+1,#target.undos)]
		target.undoFunc(target,first)
		if undoPos == 0 then
			table.remove(target.undos,#target.undos)  
		end
	end
end

function editor.pushUndoState(action,data)
	editor.storeSelected()
	
	local _,i = lume.match(currentRooms,function(x) return x==currentRoom end)
	local t = {camera=deepcopy(currentRoom.camera),viewport=deepcopy(currentRoom.viewport)}

	local storedLevel 
	if type(data)=="boolean" and data==true then storedLevel = editor.preSave(true) end

	data = data or (actions[action] and actions[action].store and actions[action].store()) or nil

	editor:pushUndo  	({
		action = action,
		data   = data,
		level  = storedLevel,
		viewID = i,
		viewT  = t,
		set    = lume.clone(currentRooms),
	})
end

function editor.undoFunc(target)
	local savedCam 				= deepcopy(cam)
	local ls 							= target.lastState
		
	if actions[ls.action] and ls.data then 
		actions[ls.action].restore(ls.data)
	end

	--level 						= ls.level
	--scene.load 				()
	
	if ls.level then 
		merge(level,ls.level,5)

		currentRooms=ls.set

		pcall(function() currentRooms[ls.viewID].camera   = ls.viewT.camera end)
		pcall(function() currentRooms[ls.viewID].viewport = ls.viewT.viewport end)
	end
	
	--editor.setRoom 			(currentRooms[ls.viewID])
	--scene.setView 			(currentRooms[ls.viewID])
	cam.x,cam.y,cam.z,cam.zoom = savedCam.x,savedCam.y,cam.z,savedCam.zoom
end


function merge(t1,t2,depth,visited,prt)
	local visited = visited or {}
	local depth = depth or 5
	if depth>0 then 
		depth = depth-1
		for k,v in pairs(t1) do 
			if t1[k] and t2[k] then 
				if type(v) =="table"  then 
					local vised = inTable(visited,v)
					if  not vised then 
						if v.isEntity then 
							print("ent")
						end
						merge(t1[k],t2[k],depth,lume.concat(visited,{v}),prt)
					end
				else 
					t1[k]=t2[k]
				end
			end
		end
	end
end

