--[[
BY
 _____   _____   __   _  
|  _  \ /  _  \ |  \ | | 
| | | | | | | | |   \| | 
| | | | | | | | | |\   | 
| |_| | | |_| | | | \  | 
|_____/ \_____/ |_|  \_| 

]]

local brain = require "brains/shadow_knightbrain"

local prefabs =
{
}

--local maxbosswellpetmaxlife = 75
local maxbosswellpetmaxlife = 20

local assets =
{
	Asset("ANIM", "anim/waxwell_shadow_mod.zip"),
	Asset("SOUND", "sound/maxwell.fsb"),
}

local weapons = {
"swap_spear",
"swap_spike",
"swap_nightmaresword_shadow",
"swap_axe",
"swap_pickaxe",
"swap_shovel",
}

local hats = {
"hat_bee",
"hat_beefalo",
"hat_bush",
"hat_earmuffs",
"hat_feather",
"hat_flower",
"hat_football",
"hat_fruit",
"hat_miner_off",
"hat_ruins",
"hat_slurper",
"hat_slurtle",
"hat_spider",
"hat_straw",
"hat_top",
"hat_walrus",
"hat_winter",
}

local function diepet(inst)
SpawnPrefab("shadow_despawn").Transform:SetPosition(inst.Transform:GetWorldPosition())
inst:Remove()
end

local function FindTarget(inst, radius)
    return FindEntity(
        inst,
        SpringCombatMod(radius),
        function(guy)
            return inst.components.combat:CanTarget(guy)
                and not (inst.components.follower ~= nil and inst.components.follower.leader == guy)
        end,
        { "_combat", "character" },
        { "monster", "INLIMBO" }
    )
end

local function BossShadowPetRetarget(inst)
    return FindTarget(inst, 5)
end

local function OnAttacked(inst, data)
    inst.components.combat:SetTarget(data.attacker)
    inst.components.combat:ShareTarget(data.attacker, 30, function(dude)
        return dude:HasTag("donmod_maxwellboss_pet")
            and not dude.components.health:IsDead()
            and dude.components.follower ~= nil
            and dude.components.follower.leader == inst.components.follower.leader
    end, 10)
end


local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeGhostPhysics(inst, 1, 0.5)
	
	inst:AddTag("monster")
	inst:AddTag("hostile")
	inst:AddTag("donmod_maxwellboss_pet")

	inst.Transform:SetFourFaced(inst)

	inst.AnimState:SetBank("wilson")
	inst.AnimState:SetBuild("waxwell_shadow_mod")
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:SetMultColour(0, 0, 0, .5)
	
	inst:DoTaskInTime(90, diepet)	--die
	
	local tool = weapons[math.random(6)]
	local hat = hats[math.random(17)]

	if tool ~= nil then
		inst.AnimState:OverrideSymbol("swap_object", tool, tool)
		inst.AnimState:Hide("ARM_normal")
	else
		inst.AnimState:Hide("ARM_carry")
	end

	if hat ~= nil then
		inst.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat")
		inst.AnimState:Hide("HAIR_NOHAT")
		inst.AnimState:Hide("HAIR")
	else
		inst.AnimState:Hide("HAT")
		inst.AnimState:Hide("HAIR_HAT")
	end

	inst:AddTag("NOBLOCK")
	inst:AddTag("healthinfo")

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end
	
	inst:AddComponent("shadowsubmissive")

	inst:AddComponent("locomotor")
	inst.components.locomotor.runspeed = TUNING.SHADOWWAXWELL_SPEED
	inst.components.locomotor.pathcaps = { ignorecreep = true }
	inst.components.locomotor:SetSlowMultiplier(.6)

	inst:AddComponent("health")
	inst.components.health:SetMaxHealth(maxbosswellpetmaxlife)

	
	inst:ListenForEvent("attacked", OnAttacked)
				
	inst:AddComponent("follower")
	
	inst:AddComponent("combat")
	inst.components.combat.hiteffectsymbol = "torso"
	inst.components.combat:SetDefaultDamage(TUNING.SHADOWWAXWELL_DAMAGE)
	inst.components.combat:SetRetargetFunction(2, BossShadowPetRetarget)

	inst:SetBrain(brain)
	inst:SetStateGraph("SGshadowwaxwell")
	
	return inst
end

return Prefab("donmod_maxwellboss_pet", fn, assets, prefabs)
