

PrefabFiles = {
	"bajie",
}

Assets = {
    Asset( "IMAGE", "images/saveslot_portraits/bajie.tex" ),
    Asset( "ATLAS", "images/saveslot_portraits/bajie.xml" ),

    Asset( "IMAGE", "images/selectscreen_portraits/bajie.tex" ),
    Asset( "ATLAS", "images/selectscreen_portraits/bajie.xml" ),
	
    Asset( "IMAGE", "images/selectscreen_portraits/bajie_silho.tex" ),
    Asset( "ATLAS", "images/selectscreen_portraits/bajie_silho.xml" ),

    Asset( "IMAGE", "bigportraits/bajie.tex" ),
    Asset( "ATLAS", "bigportraits/bajie.xml" ),
	
	Asset( "IMAGE", "images/map_icons/bajie.tex" ),
	Asset( "ATLAS", "images/map_icons/bajie.xml" ),

}

local require = GLOBAL.require

-- The character select screen lines
GLOBAL.STRINGS.CHARACTER_TITLES.bajie = "BaJie"
GLOBAL.STRINGS.CHARACTER_NAMES.bajie = "The Mythical characters From Journey to the West named BaJie"
GLOBAL.STRINGS.CHARACTER_DESCRIPTIONS.bajie = "*Perk 1\n*Perk 2\n*Perk 3"
GLOBAL.STRINGS.CHARACTER_QUOTES.bajie = "\"Quote\""

-- Custom speech strings
GLOBAL.STRINGS.CHARACTERS.BAJIE = require "speech_bajie"

-- Let the game know character is male, female, or robot
table.insert(GLOBAL.CHARACTER_GENDERS.FEMALE, "bajie")


AddMinimapAtlas("images/map_icons/bajie.xml")
AddModCharacter("bajie")

function fairytale()
	local player = GetPlayer()
	if player.prefab == "bajie" then
		
	end
end

-- Watch Hunger
AddComponentPostInit("hunger", function(inst)
	local oldDoDec = inst.DoDec
	inst.DoDec = function(self, dt, ignore_damage) 
		if self:GetPercent() < 0.866 then
			self.inst:PushEvent("bajie_normal")
		elseif self.current >= self.max then
			self.inst:PushEvent("bajie_fairytale")
		end
		return oldDoDec(self, dt, ignore_damage)
	end
end)

function FindEntity(inst, radius, fn, musttags, canttags, mustoneoftags)
    if inst and inst:IsValid() then
		local x,y,z = inst.Transform:GetWorldPosition()
			
		--print ("FIND", inst, radius, musttags and #musttags or 0, canttags and #canttags or 0, mustoneoftags and #mustoneoftags or 0)
		local ents = GLOBAL.TheSim:FindEntities(x,y,z, radius, musttags, canttags, mustoneoftags) -- or we could include a flag to the search?
		for k, v in pairs(ents) do
			if v ~= inst and v.entity:IsValid() and v.entity:IsVisible() and (not fn or fn(v, inst)) then
				return v
			end
		end
	end
end

function pigmanPostInit(inst)
	local player = GLOBAL.GetPlayer()
	local fairytaleBajieActive = player.components.fairytalebajie and player.components.fairytalebajie.active
	
	local function bjNormalRetargetFn(inst)
		return FindEntity(inst, 20,
			function(guy)
				if not guy.LightWatcher or guy.LightWatcher:IsInLight() then
					local isFairytaleBajie = false 
					if fairytaleBajieActive then
						return guy:HasTag("monster") and guy.components.health and not guy.components.health:IsDead() and inst.components.combat:CanTarget(guy) and not (inst.components.follower.leader ~= nil and guy:HasTag("abigail"))
					else
						return (guy:HasTag("monster") or guy:HasTag("player")) and guy.components.health and not guy.components.health:IsDead() and inst.components.combat:CanTarget(guy) and not (inst.components.follower.leader ~= nil and guy:HasTag("abigail"))
						--return (guy:HasTag("player") or guy:HasTag("monster")) and guy.components.health and not guy.components.health:IsDead() and inst.components.combat:CanTarget(guy) and not (inst.components.follower.leader ~= nil and guy:HasTag("abigail"))
					end
				end
			end)
	end
	
	local function bjNormalKeepTargetFn(inst, target)
		local fairytaleActive = target.components.fairytalebajie and target.components.fairytalebajie.active
		--give up on dead guys, or guys in the dark, or werepigs
		return not fairytaleActive
			   and inst.components.combat:CanTarget(target)
			   and (not target.LightWatcher or target.LightWatcher:IsInLight())
			   and not (target.sg and target.sg:HasStateTag("transform") )
	end

	inst.components.combat:SetRetargetFunction(3, bjNormalRetargetFn)
	inst.components.combat:SetKeepTargetFunction(bjNormalKeepTargetFn)
end

function pigkingPostInit(inst)

	local player = GLOBAL.GetPlayer()
	local fairytaleBajieActive = player.components.fairytalebajie and player.components.fairytalebajie.active
	
	local oldOnGetItemFromPlayer = inst.components.trader.onaccept
	inst.components.trader.onaccept = function(inst, giver, item) 
		if player.prefab == "bajie" then
			if fairytaleBajieActive then
				oldOnGetItemFromPlayer(inst, giver, item)
			end
		else
			oldOnGetItemFromPlayer(inst, giver, item)
		end
		--oldOnGetItemFromPlayer(inst, giver, item)
	end
end

AddPrefabPostInit("pigman", pigmanPostInit)
AddPrefabPostInit("pigking", pigkingPostInit)
