local Ingredient = GLOBAL.Ingredient
local RECIPETABS = GLOBAL.RECIPETABS
local STRINGS = GLOBAL.STRINGS
local Recipe = GLOBAL.Recipe
TECH = GLOBAL.TECH
SpawnPrefab = GLOBAL.SpawnPrefab
Vector3	= GLOBAL.Vector3
TheSim = GLOBAL.TheSim

PrefabFiles = 
{
	"krampuser", 
        "krampuseregg",
	"rockypet", 
}

STRINGS.NAMES.KRAMPUSER = "Krampuser"
STRINGS.RECIPE_DESC.KRAMPUSER = "Lead a krampuser!"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.KRAMPUSER = "Now I'm able to lead krampusers!"

STRINGS.NAMES.KRAMPUSEREGG = "Krampuser egg"
STRINGS.RECIPE_DESC.KRAMPUSEREGG = "Hatch a krampuser!"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.KRAMPUSEREGG = "Now I'm going to get a krampuser baby!"

STRINGS.NAMES.KRAMPUSEREGG_CRACKED = "Cracked krampuser egg"
STRINGS.RECIPE_DESC.KRAMPUSEREGG_CRACKED = "Hatch a krampuser!"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.KRAMPUSEREGG_CRACKED = "Now I'm going to get a krampuser baby!"

STRINGS.NAMES.ROCKYPET = "Rockypet"
STRINGS.RECIPE_DESC.ROCKYPET = "Lead a rockypet!"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.ROCKYPET = "Now I'm able to lead rockypets!"

-------------------------------------------------------------------------------

TUNING.KRAMPUSER_HEALTH = 2500
TUNING.KRAMPUSER_DAMAGE = 50
TUNING.KRAMPUSER_ATTACK_PERIOD = 1.2
TUNING.KRAMPUSER_SPEED = 8

TUNING.KRAMPUSER_HATCH_CRACK_TIME = 15
TUNING.KRAMPUSER_HATCH_TIME = 480 * 5
TUNING.KRAMPUSER_HATCH_FAIL_TIME = 20

TUNING.KRAMPUSER_TARGET_DIST = 12
TUNING.KRAMPUSER_LOSETARGET_DIST = 20

-------------------------------------------------------------------------------
local function newOnAttacked(inst, data)
if not (data.attacker:HasTag("player") or data.attacker:HasTag("krampuser") or data.attacker:HasTag("rockypet")) then
    inst.components.combat:SetTarget(data.attacker)
end
end

local function Followkrampuser(inst)
krampuser = krampuser or TheSim:FindFirstEntityWithTag("krampuser")
    if krampuser then
        if  inst.components.follower.leader ~= krampuser then
             inst.components.follower:SetLeader(krampuser)
        end 

    end
end

local function krampusPrefabPostInit(inst) 
    local minimap = inst.entity:AddMiniMapEntity()
    minimap:SetIcon("krampus_sack.png")
    inst:AddTag("krampus")
    inst:ListenForEvent("attacked", newOnAttacked)
    inst:AddComponent("follower")
    inst.Followtask = inst:DoPeriodicTask(0, function()  Followkrampuser(inst) end)
end


AddPrefabPostInit("krampus", krampusPrefabPostInit)

local function firekrampuserbag(inst)
    local owner = inst.components.inventoryitem and inst.components.inventoryitem.owner or nil
    if owner and owner:HasTag("krampuser") and owner.components.health and owner.components.health:IsHurt() then
		local foodstacksize = inst.components.stackable.stacksize
                local poopdrop = SpawnPrefab("poop")
                local pt = Vector3(inst.Transform:GetWorldPosition()) + Vector3(0,4.5,0)                
                poopdrop.Transform:SetPosition(pt:Get()) 
         if owner.components.health:GetPercent() >=0.75 or foodstacksize ==1 then
            inst = inst.components.stackable:Get()
            inst:Remove()
         owner.components.health:DoDelta(0.25*TUNING.KRAMPUSER_HEALTH)
         elseif owner.components.health:GetPercent() < 0.75 and owner.components.health:GetPercent() >= 0.5 or foodstacksize ==2 then
            inst = inst.components.stackable:Get(2)
            inst:Remove()
         owner.components.health:DoDelta(0.5*TUNING.KRAMPUSER_HEALTH)
         elseif owner.components.health:GetPercent() < 0.5 and owner.components.health:GetPercent() >= 0.25 or foodstacksize ==3 then
            inst = inst.components.stackable:Get(3)
            inst:Remove()
         owner.components.health:DoDelta(0.75*TUNING.KRAMPUSER_HEALTH)
         elseif owner.components.health:GetPercent() < 0.25 or foodstacksize ==4 then
            inst = inst.components.stackable:Get(4)
            inst:Remove()
         owner.components.health:DoDelta(1*TUNING.KRAMPUSER_HEALTH)
         end

    end
end

local function redgemfuelPrefabPostInit(inst) 
    inst.redgemtask = inst:DoPeriodicTask(1, function() firekrampuserbag(inst) end)
end
AddPrefabPostInit("redgem", redgemfuelPrefabPostInit)