player = { }

-- Player class holds data on "Attributes" and on Input methods
function player:init( )


	self._playerActionTable = { }

	self._hp = 100
	self._gold = 150
	self._hpDecreaseRate = 5
	self._msx = 0
	self._msy = 0
	self._movementCost = 10 -- gold
	self._tileSize = 64
	self._timeTick = 0
	self._xpIncrements = 2

	self._playerMaxXP = 100
	self._playerCurrentXP = 1
	self._playerLevel = 0

	self.__msx = math.floor(self._msx/self._tileSize)
	self.__msy = math.floor(self._msy/self._tileSize)

	self._revealTex = anim:newDeck("Game/ggj/media/reveal_effect.png", 64, g_Ui_Layer)
	self._revealTex2 = anim:newDeck("Game/ggj/media/reveal_effect2.png", 64, g_Ui_Layer)

	--self._revealAnim = anim:newAnim(self._revealTex, 1, -500,0, 1)



	self._xp_fill_tex = wimage:newTexture("ggj/media/xp_bar_beneath.png", g_RangeLayer, "self._x2p_bar_tex")
	self._xp_fill = image:newImage(self._xp_fill_tex, 116, 590)

	self._xp_bar_tex = wimage:newTexture("ggj/media/xp_bar.png", g_RangeLayer, "self._xp_bar_tex")
	self._xp_bar = image:newImage(self._xp_bar_tex, 64, 577)
	--anim:addToPool(self._xpBar)

	self._attack_anim_tex = anim:newDeck("Game/ggj/media/player_attack_anim.png", 64, g_Ui_Layer)

	self._playerDmgEff_tex = anim:newDeck("Game/ggj/media/damage_effect_fullscr.png", 640 , g_Ui_Layer)
	--self._attack_anim = anim:newAnim(self._attack_anim_tex, 6, 128, 128, 1)
	--anim:addToPool(self._attack_anim)

	anim:createState("ATTACK_ANIM", 1, 4, 0.15)
	anim:createState("ATTACK_IDLE", 5, 6, 200)

	anim:createState("EFFECT_REVEAL", 1, 4, 0.05)
	anim:createState("EFFECT_HIDDEN", 4, 4, 200)
	--anim:addToPool(self._revealAnim)
	

	
	--self._healAnimTex = anim:newDeck("Game/ggj/media/healing_effect.png", 512, g_ActionPhase_UI_Layer)
	--self._healAnim = anim:newAnim(self._healAnimTex, 4, 0, 128, 1)
	--anim:createState("EFFECT_HEAL_FULLSCREEN", 1, 4, 0.25)
	--anim:addToPool(self._healAnim)
end

function player:getPlayerlevel( )
	return self._playerLevel
end

function player:returnDmgToPlayerEffect( )
	return self._playerDmgEff_tex
end

function player:addXP( )
	self._playerCurrentXP = self._playerCurrentXP + self._xpIncrements
end

function player:returnAttacKAnim( )
	return self._attack_anim_tex
end

function player:createHealthBar( )
	--self._hp = 100
	self._healthBarGrid =  mGrid:new(1, 10, 32, "Game/ggj/media/health_bar.png", 1)
	mGrid:setPos(self._healthBarGrid, 20 ,144)

	self._rubyGrid = mGrid:new(1, 15, 32, "Game/ggj/media/ruby.png", 1)
	mGrid:setPos(self._rubyGrid, 458 ,80)
end
--[[
	Just in case I want to add new members to a party
]]

function player:drawXP( )
	local scaleFactor =  (self._playerCurrentXP / self._playerMaxXP ) * 100  --1 - ( (100 - ( (100 * self._playerMaxXP) / self._playerCurrentXP  ) ) / 100 )

	if scaleFactor > 9.7 then
		scaleFactor = 9.7
		self._playerLevel = self._playerLevel + 1
		self._playerCurrentXP = 1
	end
	image:setScale(self._xp_fill, scaleFactor, 1)
end

function player:setHealth(_value)
	for i = 2, 10 do
		if i <= math.floor(self._hp/10) then
			mGrid:setTiles3(self._healthBarGrid, 1, i, 1)
		else
			mGrid:setTiles3(self._healthBarGrid, 1, i, 2)
		end
	end
end

function player:setGold(_value)
	for i = 1, 15 do
		if i <= math.floor(self._gold/10) then
			mGrid:setTiles3(self._rubyGrid, 1, i, 1)
		else
			mGrid:setTiles3(self._rubyGrid, 1, i, 2)
		end
	end
end

function player:addGold(_value)
	self._gold = self._gold + _value
	self:setGold(self._gold)
end

function player:alterHP(_value)
	self._hp = self._hp + _value
	self:setHealth(self._hp)
end

function player:handleInput( )
	

	self.__msx = math.floor(Game.mouseX/self._tileSize)
	self.__msy = math.floor(Game.mouseY/self._tileSize)
	
	self:actionPerformed(self.__msx, self.__msy)

	self:updateTimeTick( )
end

function player:performAttackAnim(_x, _y)
	--anim:setState(self._attack_anim, "ATTACK_ANIM", "ATTACK_IDLE")
	anim:setPosition(self._attack_anim, _x, _y)

end

function player:update( )
	-- on user tap
	--anim:updateAnim(self._revealAnim, -200, -200)
	--anim:updateAnim(self._attack_anim, -200, -200)
	if self._gold > 150 then
		self._gold = 150
	end

	if self._gold < 50 then
		interface:setSupplyWarning(true)
	elseif self._gold > 50 then
		interface:setSupplyWarning(false)
	end

	
	if self._hp > 100 then
		self._hp = 100
	end

	if self._playerCurrentXP > 800 then
		self._playerLevel = self._playerLevel + 1
		self._playerCurrentXP = 1
	end

	if self._playerCurrentXP < 1 then
		self._playerCurrentXP = 1
	end

	self:drawXP( )

	if self._gold < 0 then
		--map:reset( )
		--self._gold = 0
		self._gold = 0
		--currentState = 16
		interface:setCauseOfDeathIMG(2)
		interface:setDefeatState(true)
		
	end


	if self._hp < 0 then
		--map:reset( )
		--self._hp = 0
		self._hp = 0
		--currentState = 16
		interface:setCauseOfDeathIMG(1)
		interface:setDefeatState(true)
	end
end

function player:actionPerformed(_x, _y)
	local bool = map:isLocationRevealed(_x, _y)

	--if bool == false then
		map:revealAtLoc(_x, _y)
		local mapSizeX, mapSizeY = map:getMapSize( )
		if self.__msx >= 1 and self.__msx <= mapSizeX then
			if self.__msy >= 1 and self.__msy <= mapSizeY then
				self:playRevealAnim(_x*64, _y*64)
			end
		end
		local itemBool, itemID = items:isAt(_x, _y)
		if itemBool == true  then
			if items:isItemRevealed(itemID) == true then
				items:applyEffect(itemID)
				self._gold = self._gold - self._movementCost
			else
				items:revealItem(itemID)
			end
		end

		if interface:getTweenStatus( ) == false then
			if bool == false then
				self._gold = self._gold - self._movementCost
				sound:play(sound.tap)
				map:shake(5, 8, 0.25)
				local rev_eff = effect:new("REVEFF", _x, _y, self._revealTex, 9, nil, false, false)
			end
		end
	--end

end

function player:substractMovementCost( )
	self._gold = self._gold - self._movementCost
	self:setGold(self._gold)
end

function player:returnRevealTexture( )
	return self._revealTex2
end

function player:playRevealAnim(_x, _y)
	--anim:updateAnim(self._revealAnim, _x, _y, true)
	--anim:setPosition(self._revealAnim, _x, _y)
	--anim:setState(self._revealAnim, "EFFECT_REVEAL", "EFFECT_HIDDEN")


end

function player:updateTimeTick( )
	local mapSizeX, mapSizeY = map:getMapSize( )
	if self.__msx >= 1 and self.__msx <= mapSizeX then
		if self.__msy >= 1 and self.__msy <= mapSizeY then
			--self._timeTick = self._timeTick + 1
			--self._hp = self._hp - self._hpDecreaseRate

			if self._healthBarGrid ~= nil then
				player:setHealth(self._hp)
				player:setGold(self._gold)
			end
		end
	end

	print("TimeTick is at: "..self._timeTick.." HP: "..self._hp.."")
end

function player:restoreHP( )
	self._hp = 105
end

function player:cleanupStuff( )
	
end

function player:deleteHealthGrid( )
	mGrid:destroy(self._healthBarGrid)
	mGrid:destroy(self._rubyGrid)
	image:removeProp(self._xp_bar, g_RangeLayer  )
	--self._hp = 100
	--self._gold = 150


end

function player:resetStats( )
	self._playerLevel = 0
	self._playerCurrentXP = 0
	image:removeProp(self._xp_fill, g_RangeLayer  )	
end

function player:dropXPbar( )
	if self._xp_fill ~= nil then
		image:removeProp(self._xp_fill, g_RangeLayer  )	
	end		
end