map = { }
math.randomseed(os.time())
function map:init( )
	self._initShake = false
	self._shakeTimer = Game.worldTimer
	self._shakeInterval = 2.2

	map.goodX = nil
	map.goodY = nil

	self._mapTable = { }
	self._tileSize = 64
	self._mapSizeX = 6
	self._mapSizeY = 8
	self._mapOffset=self._tileSize
	self._stairX = 0
	self._stairY = 0
	self._hidden = 1073741825
	self._viewable = 1073741826

	map.offX = 64
	map.offY = 64
	print("SHOULD HAPPEN")

	self._startX = 1
	self._startY = 1

	self._endX = 6
	self._endY = 8

	self._chanceList = { }


	self._pathTable = {}
	self._nonPathTable = { }

	self._texTable = { }
	for i = 1, 10 do
		self._texTable[i] = 1073741825+i
	end

	self._level = 0

	self._chanceTable = { }
	self._chanceTable[1] = { a = 1, b = 10 }
	self._chanceTable[2] = { a = 11, b = 36}
	self._chanceTable[3] = { a = 0, b = 0 }
	self._chanceTable[4] = { a = 30, b = 70 }
	self._chanceTable[5] = { a = 71, b = 77 }
	self._chanceTable[6] = { a = 78, b = 86 }
	self._chanceTable[7] = { a = 87, b = 100 }

	
end

function map:getDepth( )
	return self._level
end

function map:update( )
	--print("Should be looping")

	map:_updateShake( )

	mGrid:setPos(self._dungeon, map.offX , map.offY )
	mGrid:setPos(self._fog, map.offX , map.offY )
end

function map:firstPass( )



end

function map:generate( )

	local playerLevelCalc = math.floor( player:getPlayerlevel( ) / 2)
	local rubyA = 8
	local rubyB = 45

	if self._level == 0 or self._level == 1 then
		rubyA = 1
		rubyB = 50
	end


	self._chanceTable[1] = { a = 1, b = 4 }
	self._chanceTable[2] = { a = rubyA, b = rubyB }
	self._chanceTable[3] = { a = 0, b = 0 }
	self._chanceTable[4] = { a = 30, b = 70 }
	self._chanceTable[5] = { a = 71, b = 77 }
	self._chanceTable[6] = { a = 78, b = 86 }
	self._chanceTable[7] = { a = 87, b = 100 }


	self._dungeonTileSet = {}
	self._dungeonTileSet[1] = "tileset_map"
	self._dungeonTileSet[2] = "tileset_map4"
	self._dungeonTileSet[3] = "tileset_map3"

	self._dungeon =  mGrid:new(self._mapSizeX, self._mapSizeY, self._tileSize, "Game/ggj/media/"..self._dungeonTileSet[math.random(1,#self._dungeonTileSet)]..".png", 1)

	self._fog = mGrid:new(self._mapSizeX, self._mapSizeY, self._tileSize, "Game/ggj/media/fog_grid.png", 1073741825)
	self._collisionGrid = mGrid:new(self._mapSizeX, self._mapSizeY, self._tileSize, "Game/ggj/media/collision_grid.png", 2)

	mGrid:setPos(self._dungeon, self._tileSize, self._tileSize)
	mGrid:setPos(self._fog, self._tileSize, self._tileSize)
	mGrid:setPos(self._collisionGrid, self._tileSize, self._tileSize)

	player:createHealthBar( )

	self._startX = math.random(1,6)
	self._startY = 1

	self._endX = math.random(1,6)
	self._endY = 8


	print("WERE HERE 119")
	for y = 2, self._mapSizeY-1 do
		local x = math.random(1, 6)
		local wall = 3
		self:setTile(x, y, wall)
		local x = math.random(1, 6)
		local wall = 3
		self:setTile(x, y, wall)	
	end


		print("WERE HERE 130")
	for x = 1, self._mapSizeX do 
		for y = 1, self._mapSizeY do
			local gridValue = mGrid:getTile(self._dungeon, x, y) 
			if gridValue == 1073741825 or gridValue == 1073741826 then
				mGrid:setTiles3(self._collisionGrid, x, y, 1)
			end
		end
	end	


	_tab = mGrid:getLocalTable(self._collisionGrid)
	Game.grid = mGrid:transformTableForJumper(_tab)
	Game:initPathfinding( Game.grid )

	path = pather:getPath(self._startX, self._startY, self._endX, self._endY)

	print("WERE HERE 147")
	if path ~= nil then
		length = path:getLength( )
		path:fill()
		--for x = 1, _length do
		for node, count in path:nodes() do
			local temp = {
				x = node:getX(), 
				y = node:getY(), 
			}

			table.insert(self._pathTable, temp)
		end
	end

	print("WERE HERE 162")	
	for i,v in ipairs(self._pathTable) do
		mGrid:setTiles3(self._collisionGrid, v.x, v.y, 3)
	end

	print("WERE HERE 167")
	-- create table that contains all walkable squares that are not in the path
	for x = 1, self._mapSizeX do
		for y = 1, self._mapSizeY do
			local gridValue = mGrid:getTile(self._dungeon, x, y)
			if gridValue ~= 1073741827 then
				local bool = self:findInPath(x, y) 
				if bool == false then
					--print("TILE VALUE: "..mGrid:getTile(self._dungeon,x, y).."")
					local temp = {
						_x = x,
						_y = y,
					}

					table.insert(self._nonPathTable, temp)
				end
			end
		end
	end

	print("WERE HERE 186")
	for i,v in ipairs(self._nonPathTable) do
		--print("LAA "..i.."")
		mGrid:setTiles3(self._collisionGrid, v._x, v._y, 4)
	end

	print("WERE HERE 193")
	self:addHazzard( )


	print("WERE HERE 197")
	self:addStairs( )
	print("WERE HERE 199")
	self:populateWithItems( )

	print("WERE HERE 202")
	mGrid:destroy(self._collisionGrid)
	
	
	print("WERE HERE 206")
	self:revealAtStartPiece( )

	self._level = self._level + 1

	self._pathTable = { }
end

function map:revealAtStartPiece( )
	--[[if self._pathTable ~= nil then
		if self._pathTable[#self._pathTable] ~= nil then
			if self._pathTable[#self._pathTable].x  ~= nil then
				local x = self._pathTable[math.random(1, #self._pathTable)].x
				local y = self._pathTable[math.random(1, #self._pathTable)].y
				self:revealAtLoc(x, y)
			end
		else
			local x = math.random(1,6)
			local y = math.random(1,8)
			self:revealAtLoc(x, y)
		end
	else
		local x = math.random(1,6)
		local y = math.random(1,8)
		self:revealAtLoc(x, y)
	end--]]

		local x = math.random(1,6)
		local y = math.random(1,8)

		for _x = x - 2, x + 2 do
			for _y = y - 2, y + 2 do
				if mGrid:getTile(self._fog, _x, _y) ~= nil then
					local rnd = math.random(1, 5)
					if rnd >= 2 and rnd <= 4 then
						self:revealAtLoc2(_x, _y)
						local bool, id = items:isAt(_x, _y)
						if bool ~= false then
							items:revealItem(id)
						end
					end
				end
			end
		end 
end

function map:addHazzard( ) -- walls, lava
	for i,v in ipairs(self._nonPathTable) do
		local chance = math.random(1, 100)
		local tp = math.random(1,2)
		if chance <= 14 then -- lava
			tp = 4
		elseif chance > 14 and chance <= 40 then
			tp = 3
		end

		self:setTile(v._x, v._y, tp)
	end
end

function map:nrEmptyTile( )
	self._EmptyTable = { }
	local count = 0
	for x = 1, self._mapSizeX do
		for y = 1, self._mapSizeY do
			local result = mGrid:getTile(self._dungeon, x, y)
			if result == 1073741825 or result == 1073741826 then
				count = count + 1
				local temp = {
					_x = x,
					_y = y,

				}
				table.insert(self._EmptyTable, temp)
			end
		end
	end

	return count
end

function map:populateWithItems( ) -- and enemies

	-- now let's see if we can spawn a legendary :D
	local rndChance = math.random(1,5)

	--print("========================POINTER HERE")
	if rndChance <= 3 then
		local x, y = self:findEmptySpot( )
		if x ~= nil and map.y ~= nil then
			items:new(x, y, 8)
		end
	end

	--print("TEST: "..self:nrEmptyTile( ).."")
	local maxNrItems = math.random(5,self:nrEmptyTile( ))
	local itemCount = 0




	
	for i = 1, maxNrItems do
		local x, y = self:findEmptySpot( )
		if x ~= nil and y ~= nil then
			local rnd = math.random(1, 100)
			for i,v in ipairs(self._chanceTable) do
				if rnd > v.a and rnd < v.b then
					if itemCount < maxNrItems then
						print("CHANCE TABLE: "..#self._chanceTable.." I IS: "..i.." X IS: "..x.." and y: "..y.."")
						print(""..i.."BEFORE========================POINTER HERE")
						itemCount = itemCount + 1
						items:new(x, y, i)
						print(""..i.." AFTER========================POINTER HERE")
					end
				end
			end			
		end
	end

	-- now let's see if we can spawn a legendary :D
	local rndChance = math.random(1,5)

	if rndChance < 3 then

	end
end

function map:findInPath(_x, _y)
	local bool = false
	for i,v in ipairs(self._pathTable) do
		if v.x == _x and v.y == _y then
			bool = true
		end
	end

	return bool
end

function map:areStairsThere(_x, _y)
	local bool = false
	local tileType = mGrid:getTile(self._dungeon, _x, _y)
	if tileType == 1073741829 then
		bool = true
	end

	return bool
end

function map:addStairs( )
	local x = math.random(1, self._mapSizeX)
	local y = math.random(1, self._mapSizeY)

	if mGrid:getTile(self._dungeon, x, y) ~= 3 and mGrid:getTile(self._dungeon, x, y) ~= 4 then
		self:setTile(x, y, 5)
		self._stairX = x
		self._stairY = y
		items:new(x, y, 3)
	else
		self:addStairs( )
	end
	--[[for i = 1, math.random(2, 5) do
		local x = math.random(1, self._mapSizeX)
		local y = math.random(1, self._mapSizeY)
		items:new(x, y, 1)
	end--]]

end

function map:setTile(_x, _y, _value)
	mGrid:setTiles3(self._dungeon, _x, _y, _value )
end

function map:revealAtLoc(_x, _y)
	mGrid:setTiles3(self._fog, _x, _y, 2)
	local result = mGrid:getTile(self._dungeon, _x, _y)
	if result == 1073741828 then
		print("DAMAGE DA PLAYA'!")
		local attk_eff = effect:new("ATTACK EFF", -1, 0, player:returnDmgToPlayerEffect( ), 4, nil, false, false)
		effect:setSpeed(attk_eff, 0.15)
		sound:stopAllFromCategory(SOUND_WALKING)
		sound:play(sound.playerHit)
		player:alterHP(-20)
	end
end

function map:revealAtLoc2(_x, _y)
	mGrid:setTiles3(self._fog, _x, _y, 2)
	local result = mGrid:getTile(self._dungeon, _x, _y)

end

function map:setFogAtLoc(_x, _y)
	mGrid:setTiles3(self._fog, _x, _y, 1)
end

function map:getOffset( )
	return self._mapOffset
end

function map:isLocationRevealed(_x, _y)
	local bool = false
	local value = mGrid:getTile(self._fog, _x, _y)

	if value == self._hidden then
		bool = false
	else
		bool = true
	end

	return bool
end

function map:getMapSize( )
	return 	self._mapSizeX, self._mapSizeY
end


function map:getRandomEmptyLoc( )

	local x = math.random(1, self._mapSizeX)
	local y = math.random(1, self._mapSizeY)

	local tileica = mGrid:getTile(self._dungeon, x, y)
	local bool = false
	local _x = nil
	local _y = nil
	if tileica == 1073741825 and items:isAt(x, y) == false then

		print("RETURNING")
		print("X IS: "..x.." and y is: "..y.."")
		map.goodX = x
		map.goodY = y
	else
		self:getRandomEmptyLoc( )
	end

end

function map:findEmptySpot( )

	local x = 1
	local y = 1
	repeat
		x = math.random(1, self._mapSizeX)
		y = math.random(1, self._mapSizeY)
	until (mGrid:getTile(self._dungeon, x, y) == 1073741825 or mGrid:getTile(self._dungeon, x, y) == 1073741826 ) and items:isAt(x, y) == false


	return x, y



end

function map:reset( )
	print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
	player:deleteHealthGrid( )

	mGrid:destroy(self._fog)
	
	mGrid:destroy(self._dungeon)

	
	for i = 1, 20 do
		items:dropAll( )
	end



	self:generate( )


end

function map:destroyAll( )
	player:deleteHealthGrid( )
	mGrid:destroy(self._fog)
	mGrid:destroy(self._dungeon)


	for i = 1, 40 do
		items:dropAll( )
	end

	effect:dropAll( )
	anim:dropAll( )

	self._level = 0
end

function map:revealAll( )
	for x = 1, self._mapSizeX do
		for y = 1, self._mapSizeY do
			self:revealAtLoc2(x, y)
			local rev_eff2 = effect:new("REVEF2F", x, y, player:returnRevealTexture( ), 7, nil, false, false)
		end
	end
end







-------------------------------
-- SCREEN SHAKE STUFF
-------------------------------

function map:setOffset(_x, _y)
	map.offX = _x
	map.offY = _y
end
function map:shake(_int, _int2, _dur)
	if self._initShake == false then
		self._oldOffX = map.offX
		self._oldOffY = map.offY
		self._shakeTimer = Game.worldTimer
		self._initShake = true
		self._int = _int
		self._duration = _dur
	end
end

function map:_updateShake( )
	if self._initShake == true then
		local dur
		if self._duration ~= nil then
			dur = self._duration
		else
			dur = 0.2
		end
		if Game.worldTimer <= self._shakeTimer + dur then
			map.offX = math.random(self._oldOffX - self._int, self._oldOffX + self._int)
			map.offY = math.random(self._oldOffY - self._int, self._oldOffY + self._int)

		else
			self._initShake = false
			map.offX = self._oldOffX
			map.offY = self._oldOffY
			--interface:setTargetAtLoc(2000, 200000)
		end
	end
end
function map:recordOffset( )
	self._recX = map.offX
	self._recY = map.offY
end

function map:returnOldOffset( )
	map.offX = self._recX
	map.offY = self._recY
	--self:setScreen(self._recX, self._recY)
end