assert(editor == nil)
editor = {
	room_mode = false,
	palette_mode = false
}

function stepEditor()
	editor.palette_mode = editor.palette_mode or false
	animateTiles()
	if Keybind:checkPressed(Keybind.editor.room_mode) then
		if love.keyboard.isDown("lshift") then
			editor.room_mode = "delete"
		else
			editor.room_mode = not editor.room_mode
		end
		editor.room_points = {}
	end

	if Keybind:checkPressed(Keybind.editor.palette_mode) then
		if editor.palette_mode then
			editor.palette_mode = false
			palette_scroll_x = nil
			palette_scroll_y = nil
		else
			editor.palette_mode = true
			palette_scroll_x = 0
			palette_scroll_y = 0
		end
	end

	if Keybind:checkPressed(Keybind.editor.palette_mode) then
		if editor.palette_mode then
			editor.palette_mode = false
			palette_scroll_x = nil
			palette_scroll_y = nil
		else
			editor.palette_mode = true
			palette_scroll_x = 0
			palette_scroll_y = 0
		end
	end


		if Keybind:checkPressed(Keybind.editor.palette_mode) then
			if editor.palette_mode then
				editor.palette_mode = false
				palette_scroll_x = nil
				palette_scroll_y = nil
			else
				editor.palette_mode = true
				palette_scroll_x = 0
				palette_scroll_y = 0
			end
		end

	-- TODO:
  -- i changed this but i dont know what was to do here.
	-- - made specific action keybinds
	if Keybind:checkDown(Keybind.editor.left) then
		Camera.pos.x = Camera.pos.x - 3
	end
	if Keybind:checkDown(Keybind.editor.right) then
		Camera.pos.x = Camera.pos.x + 3
	end
	if Keybind:checkDown(Keybind.editor.up) then
		Camera.pos.y = Camera.pos.y - 3
	end
	if Keybind:checkDown(Keybind.editor.down) then
		Camera.pos.y = Camera.pos.y + 3
	end

	if editor.palette_mode then
		if Keybind:checkPressed(Keybind.editor.palette_change) then
			local next = false
			local export = nil
			for k, v in pairs(tileset) do
				if export == nil then
					export = v
				end
				if next then
					LevelData.tileset = v
					next = false
					break
				end

				if v == LevelData.tileset then
					next = true
				end
			end
			if next then
				LevelData.tileset = export
			end
			getLevelTileData()
			indexLevelTiles()
		end
	end

	if Keybind:checkPressed(Keybind.editor.save) then
		Prompt:new({
			name = "level name",
			input = "unnamed",
			func = function(name_prompt)
				if name_prompt.canceled then return end
				Prompt:new({
					name = "filename",
					input = "level.lua",
					func = function(file_prompt)
						if file_prompt.canceled then return end
						exportLevel(name_prompt.input, file_prompt.input)
					end,
				}):activate()
			end,
		}):activate()
	end

	if Keybind:checkPressed(Keybind.debug.editor) then
		editor_mode = not editor_mode
		createTileObjects()
	end
end

function scrollEditor(y)
	if editor.palette_mode then
		if love.keyboard.isDown("lshift") then
			palette_scroll_y = palette_scroll_y + y
		else
			palette_scroll_x = palette_scroll_x + y
		end
	else
		local oscale = game.scale
	 	game.scale = math.max(0.1,game.scale + y/16)
	end
end

function drawEditor()
	startGameworldDraw()
	drawGameworldBackground()
	drawGridDisplay()
	drawGameworldForeground()
	endGameworldDraw()
	doEditorEdit()

	drawEditorRooms()
	drawSelectingPaletteTile()

	if editor.palette_mode then
		doEditorPalette()
	end
end

function doEditorEdit()
	local mouse_x = love.mouse.getX()
	local mouse_y = love.mouse.getY()
	local horizontal = 1+math.floor(((mouse_x/game.scale) / tile_properties.width) + (Camera.pos.x / tile_properties.width))
	local vertical = 1+math.floor(((mouse_y/game.scale) / tile_properties.height) + (Camera.pos.y / tile_properties.height))
	local expand_h = 0
	local expand_v = 0
	local level_width = getLevelTileWidth()
	local level_height = getLevelTileHeight()

	if horizontal > level_width then
		 expand_h = horizontal-level_width
	elseif horizontal < 0 then
		 expand_h = horizontal
	end
	if vertical > level_height then
		 expand_v = vertical-level_height
	elseif vertical < 0 then
		 expand_v = vertical
	end
	love.graphics.setColor(100, 100, 100, 0.8)
	love.graphics.print("> " .. horizontal .. ", " .. vertical .. "; " .. math.floor(mouse_x / game.scale + Camera.pos.x) .. ", " .. math.floor(mouse_y / game.scale + Camera.pos.y))
	love.graphics.print("> " .. level_width .. "(" .. expand_h .. "), " .. level_height .. "(".. expand_v .. ")", 0, 10)

	if editor.room_mode then
		local rx = horizontal * tile_properties.width
		local ry = vertical * tile_properties.height
		local r = editor.room_points
		if Keybind:checkPressed(Keybind.generic.rclick) then
			editor.room_points = {}
		elseif Keybind:checkPressed(Keybind.generic.lclick) then
			if editor.room_mode == "delete" then
				for i, room in ipairs(LoadedObjects.Rooms) do
					if room:containsPoint(rx, ry) then
						table.remove(LoadedObjects.Rooms, i)
					end
				end
			else
				table.insert(r, { x = rx, y = ry })
			end
		end
		if #editor.room_points == 2 then
			table.insert(LoadedObjects.Rooms, Collision:new(r[1].x-tile_properties.width,r[1].y-tile_properties.height,r[2].x,r[2].y))
			editor.room_points = {}
		end
		if editor.room_mode == "delete" then
			love.graphics.print("Select room to delete", 0, 20)
		elseif #editor.room_points == 0 then
			love.graphics.print("Select top left of new room", 0, 20)
		else
			love.graphics.print("Select bottom right of new room", 0, 20)
		end
	elseif not editor.palette_mode then
		if 	LevelTiles[vertical] ~= nil
		and LevelTiles[vertical][horizontal] ~= nil
		and love.keyboard.isDown("lshift") ~= true
		and love.keyboard.isDown("lctrl") ~= true
		then
			if Keybind:checkDown(Keybind.generic.lclick)
			and selecting_tile ~= nil
			then
				setTile(vertical,horizontal,selecting_tile)
			elseif Keybind:checkDown(Keybind.generic.rclick) then
				setTile(vertical,horizontal,0)
			end
			reloadLevelTiles()

		elseif Keybind:checkPressed(Keybind.generic.lshift) then
			expandLevelCanvas(math.sign(expand_h),math.sign(expand_v))
			reloadLevelTiles()
		elseif Keybind:checkPressed(Keybind.generic.lctrl) then
			reduceLevelCanvas(math.sign(expand_h),math.sign(expand_v))
			reloadLevelTiles()
		end
	end
end

function drawSelectingPaletteTile()
	if selecting_tile ~= nil and selecting_tile ~= 0 then

		local mouse_x = love.mouse.getX()
		local mouse_y = love.mouse.getY()
		local horizontal = math.floor(((mouse_x/game.scale) / tile_properties.width) + (Camera.pos.x / tile_properties.width))
		local vertical = math.floor(((mouse_y/game.scale) / tile_properties.height) + (Camera.pos.y / tile_properties.height))
		local draw_x = tile_properties.width * horizontal - Camera.pos.x
		local draw_y = tile_properties.height * vertical - Camera.pos.y

		love.graphics.draw(
			LevelData.tileset,
			TileIndex[selecting_tile],
			draw_x,
			draw_y
		)
	end
end

function doEditorPalette()

	local width = LevelData.tileset:getPixelWidth()/tile_properties.width
	local height = LevelData.tileset:getPixelHeight()/tile_properties.height

	love.graphics.setColor(0,0,0,1)
	love.graphics.rectangle(
		"fill",
		(palette_scroll_x + 1) * (tile_properties.width+1),
		(palette_scroll_y + 1) * (tile_properties.height+1),
		1 + LevelData.tileset:getPixelWidth() * ((tile_properties.width+1) / tile_properties.width),
		1 + LevelData.tileset:getPixelHeight()* ((tile_properties.height+1) / tile_properties.height)
	)


	love.graphics.setColor(1,1,1,1)
	local position_x = 1
	local position_y = 1
	for i = 1, #TileIndex-width-1 do

		local tile_x = (palette_scroll_x + position_x) * (tile_properties.width+1)
		local tile_y = (palette_scroll_y + position_y) * (tile_properties.height+1)

		love.graphics.draw(
			LevelData.tileset,
			TileIndex[i],
			tile_x,
			tile_y,
			0,
			1,
			1
		)
		if Keybind:checkDown(Keybind.generic.lclick) then
			local mouse_x = love.mouse.getX()
			local mouse_y = love.mouse.getY()

			if 	mouse_x > (tile_x) * game.scale
			and mouse_x < (tile_x + tile_properties.width) * game.scale
			and mouse_y > (tile_y) * game.scale
			and mouse_y < (tile_y + tile_properties.height) * game.scale
			then
				selecting_tile = position_x + ((position_y-1) * width)

				love.graphics.print(selecting_tile .. " | " .. tile_x .. ", " .. tile_y, 0, 20)
			end
		end

		if Keybind:checkDown(Keybind.generic.rclick) then
			selecting_tile = nil
		end

		if selecting_tile ~= nil and selecting_tile ~= 0 and i == selecting_tile then
			love.graphics.setColor(1,0,1,1)
			love.graphics.rectangle(
				"line",
				tile_x,
				tile_y,
				tile_properties.width,
				tile_properties.height
			)
			love.graphics.setColor(1,1,1,1)
		end

		position_x = position_x + 1

		if position_x > width then
			position_x = position_x - width
			position_y = position_y + 1
		end
	end


	love.graphics.rectangle(
		"line",
		(palette_scroll_x + 1) * (tile_properties.width+1),
		(palette_scroll_y + 1) * (tile_properties.height+1),
		1 + LevelData.tileset:getPixelWidth() * ((tile_properties.width+1) / tile_properties.width),
		1 + LevelData.tileset:getPixelHeight()* ((tile_properties.height+1) / tile_properties.height)
	)
end


function drawEditorRooms()
	for _, room in pairs(LoadedObjects.Rooms) do
		love.graphics.setColor(0,0,100,1)
		love.graphics.rectangle("line",room.from.x-Camera.pos.x, room.from.y-Camera.pos.y, room.width, room.height)
	end
end
