items = { }

function items:init( )
	math.randomseed( os.time() )
	math.randomseed( math.random(os.time()-20, os.time()+20 ) )
	math.randomseed( math.random(os.time()-20, os.time()+20 ) )
	self._itemTable = { }

	self._potionAnimTex = anim:newDeck("Game/ggj/media/potion.png", 64, g_ActionPhaseLayer)
	self._coinAnimTex = anim:newDeck("Game/ggj/media/coin_entity.png", 64, g_ActionPhaseLayer)
	self._stairsAniMTex = anim:newDeck("Game/ggj/media/stairs.png", 64, g_ActionPhaseLayer)
	self._wizardAnimTex = anim:newDeck("Game/ggj/media/entity_1.png", 64, g_ActionPhaseLayer)
	self._batAnimTex = anim:newDeck("Game/ggj/media/bat_1.png", 64, g_ActionPhaseLayer)
	self._MageAnimTex = anim:newDeck("Game/ggj/media/wizard_1.png", 64, g_ActionPhaseLayer)
	self._OrcAnimTex = anim:newDeck("Game/ggj/media/orc.png", 64, g_ActionPhaseLayer)
	self._MapAnimTex = anim:newDeck("Game/ggj/media/reveal_map.png", 64, g_ActionPhaseLayer)

	self._itemType = { }
	self._itemType[1] = { name = "Potion", _type = 1, tex = self._potionAnimTex} -- ++ health
	self._itemType[2] = { name = "Chest", _type = 2, tex = self._coinAnimTex} -- ++ coin
	self._itemType[3] = { name = "Stairs", _type = 3, tex = self._stairsAniMTex}
	self._itemType[4] = { name = "Wizard", _type = 4, tex = self._wizardAnimTex, dmg = 20}
	self._itemType[5] = { name = "Bat", _type = 5, tex = self._batAnimTex, dmg = 20}
	self._itemType[6] = { name = "Mage", _type = 6, tex = self._MageAnimTex, dmg = 40}
	self._itemType[7] = { name = "Orc", _type = 7, tex = self._OrcAnimTex, dmg = 60 }
	self._itemType[8] = { name = "MagicMapOfMagic", _type = 8, tex = self._MapAnimTex }


	anim:createState("ITEM_HIDDEN", 9, 9, 200)
	anim:createState("ITEM_REVEALED", 1, 4, 0.25)
	anim:createState("ITEM_EFFECT", 5, 8, 0.10)
end

function items:returnChanceTable( )
	return self._chanceTable
end

function items:returNTable( )
	return self._itemTable
end

function items:returnTypeDMGValue(_tp)
	return self._itemType[_tp].dmg
end

function items:new(_x, _y, _type)
	math.randomseed( math.random(os.time()-20, os.time()+20 ) )
	math.randomseed( math.random(os.time()-20, os.time()+20 ) )
	local temp = {
		id = #self._itemTable + 1,
		tp = _type,
		x = _x,
		y = _y,
		isRevealed = false,
		markForDelete = false,
	}

	if self:isAt(_x, _y) == false then

		temp.gfx = anim:newAnim(self._itemType[temp.tp].tex, 9, _x*64, _y*64, 1)
		anim:setState(temp.gfx, "ITEM_HIDDEN")

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

function items:isAt(_x, _y)
	local bool = false
	local bID = nil
	for i,v in ipairs(self._itemTable) do
		if v.x == _x and v.y == _y then
			bool = true
			bID = i
		end
	end

	return bool, bID
end

function items:revealAll( )
	for i,v in ipairs(self._itemTable) do
		self:revealItem(i)
	end
end

function items:revealItem(_id)
	local v = self._itemTable[_id]
	anim:setState(v.gfx, "ITEM_REVEALED")
	v.isRevealed = true
end

function items:update( )
	for i,v in ipairs(self._itemTable) do
		if v.isRevealed == true then
			anim:updateAnim(v.gfx)
		end

		self:listenForDeletion(i)

	end
end

function items:rollCombatResult()
	local victoryBool = true
	local diceRoll = math.random(1, 20) -- 1 epic failure, 20 FLAWLESS VICTORY! 
	print("DICE ROLL IS: "..diceRoll.." | LEVEL  "..player:getPlayerlevel( ).."")

	if diceRoll < 4 + player:getPlayerlevel( ) then
		victoryBool = false
	end

	return victoryBool
end

function items:isItemRevealed(_id)
	local v = self._itemTable[_id]
	local bool = v.isRevealed
	return bool, _id --_id is optional, just in case I fuck soemthing up :)
end

function items:applyEffect(_id)
	local v = self._itemTable[_id]
	if v.markForDelete == false then
		if v.tp == 1 then -- potion
			player:restoreHP( )
			sound:play(sound.heal)
			v.markForDelete = true
		elseif v.tp == 2 then
			player:addGold(60)
			sound:play(sound.coins)
			v.markForDelete = true
		elseif v.tp == 3 then
			map:reset( )
			sound:play(sound.stairs)
			v.markForDelete = true
		elseif v.tp == 4 or v.tp == 5 or v.tp == 6 or v.tp == 7 then
			local result = self:rollCombatResult()
			if result == true then
				map:shake(8, 11, 0.50)
				player:addGold(40)
				player:addXP( )
				v.markForDelete = true
				--player:performAttackAnim(v.x*64, v.y*64)
				sound:play(sound.sword)
				local attk_eff = effect:new("ATTACK EFF", v.x, v.y, player:returnAttacKAnim( ), 6, nil, false, false)
				effect:setSpeed(attk_eff, 0.15)
			else
				player:substractMovementCost( )
				local attk_eff = effect:new("ATTACK EFF", -1, 0, player:returnDmgToPlayerEffect( ), 4, nil, false, false)
				sound:play(sound.playerHit)
				effect:setSpeed(attk_eff, 0.15)
				player:alterHP(-self._itemType[v.tp].dmg)
				map:shake(8, 14, 0.55)
			end
		elseif v.tp == 8 then
			Game:revealAll( )
			v.markForDelete = true
		end
	end

	if v.markForDelete == true then
		anim:setState(v.gfx, "ITEM_EFFECT", "ITEM_HIDDEN")
	else
		anim:setState(v.gfx, "ITEM_EFFECT", "ITEM_REVEALED")
	end
	
	
end

function items:drop(_id)
	local v = self._itemTable[_id]
	isRevealed = false
	anim:delete(v.gfx)
	table.remove(self._itemTable, _id)
end

function items:listenForDeletion(_id)
	local v = self._itemTable[_id]

	if v.markForDelete == true then
		if anim:getState(v.gfx) == "ITEM_HIDDEN" then
			self:drop(_id)
		end
	end
end

function items:dropAll( )
	
	for i,v in ipairs(self._itemTable) do
		v.markForDelete = true
		v.isRevealed = false
		self:drop(i)
	end
	for i,v in ipairs(self._itemTable) do
		v.markForDelete = true
		v.isRevealed = false
		self:drop(i)
	end
	for i,v in ipairs(self._itemTable) do
		v.markForDelete = true
		v.isRevealed = false
		self:drop(i)
	end
	
end