--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆
--◆◆[[Author: JinYering]]
--◆◆[[E-Mail: jin_yering@naver.com]]
--◆◆[[WhaRang The Millennium Fox]]
--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆--◆◆

require "prefabutil"

local assets =
{
	Asset("ANIM", "anim/fox_firepit_hot.zip"),
	Asset("ATLAS", "images/inventoryimages/firepit_ghost.xml")
}

local prefabs =
{
    "firepit_ghost_fire",
}    

local function onhammered(inst, worker)
	inst.components.lootdropper:DropLoot()
	local ash = SpawnPrefab("ash")
	ash.Transform:SetPosition(inst.Transform:GetWorldPosition())
	SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
	inst.SoundEmitter:PlaySound("dontstarve/common/destroy_stone")
	inst:Remove()
end

local function onhit(inst, worker)
	inst.AnimState:PlayAnimation("hit")
	inst.AnimState:PushAnimation("idle")
end

local function onignite(inst)
    if not inst.components.cooker then
        inst:AddComponent("cooker")
    end
end

local function onextinguish(inst)
    if inst.components.cooker then
        inst:RemoveComponent("cooker")
    end
    if inst.components.fueled then
        inst.components.fueled:InitializeFuelLevel(0)
    end
end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
	-------------------◆[MiniMap]
	 local minimap = inst.entity:AddMiniMapEntity()
	 minimap:SetIcon( "firepit_h.tex" )
	-------------------◆[AnimState]
	 anim:SetBank("fox_firepit_hot")
	 anim:SetBuild("fox_firepit_hot")
	 anim:PlayAnimation("idle",false)
	 -----------------------
	 inst:AddTag("campfire")
	 inst:AddTag("firepit")
	 inst:AddTag("hotfire")
	 inst:AddTag("structure")
	-------------------◆[Physics]
	 MakeObstaclePhysics(inst, .35)
    -------------------◆[Burnable]
	 inst:AddComponent("burnable")
	 inst.components.burnable:AddBurnFX("firepit_ghost_fire", Vector3(0, 0, 0))
	 if IsDLCEnabled(REIGN_OF_GIANTS) then
	 inst.components.burnable:MakeNotWildfireStarter()
	 inst:AddComponent("waterproofer")
     inst.components.waterproofer.effectiveness = 0
	 elseif not IsDLCEnabled(REIGN_OF_GIANTS) then
	 end
	 inst:ListenForEvent("onextinguish", onextinguish)
	 inst:ListenForEvent("onignite", onignite)
	-------------------◆[LootDropper]
	 inst:AddComponent("lootdropper")
	-------------------◆[Workable]
	 inst:AddComponent("workable")
	 inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
	 inst.components.workable:SetWorkLeft(6)
	 inst.components.workable:SetOnFinishCallback(onhammered)
	 inst.components.workable:SetOnWorkCallback(onhit)    
	-------------------◆[Fueled]
	 inst:AddComponent("fueled")
	 inst.components.fueled.maxfuel = TUNING.FIREPIT_FUEL_MAX*1.5
	 inst.components.fueled.accepting = true
	 inst.components.fueled:SetSections(4)
	 inst.components.fueled.bonusmult = TUNING.FIREPIT_BONUS_MULT
	 inst.components.fueled.ontakefuelfn = function() inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel") end
	 inst.components.fueled:SetUpdateFn( function()
        if GetSeasonManager() and GetSeasonManager():IsRaining() then
            inst.components.fueled.rate = 1 + 1*GetSeasonManager():GetPrecipitationRate()
        else
            inst.components.fueled.rate = 1
        end
        if inst.components.burnable and inst.components.fueled then
            inst.components.burnable:SetFXLevel(inst.components.fueled:GetCurrentSection(), inst.components.fueled:GetSectionPercent())
        end
	 end)  
	 inst.components.fueled:SetSectionCallback( function(section)
        if section == 0 then
            inst.components.burnable:Extinguish() 
        else
            if not inst.components.burnable:IsBurning() then
                inst.components.burnable:Ignite()
            end
            inst.components.burnable:SetFXLevel(section, inst.components.fueled:GetSectionPercent())
        end
	 end)
	 inst.components.fueled:InitializeFuelLevel(TUNING.FIREPIT_FUEL_START)
	-------------------◆[Inspectable]
	 inst:AddComponent("inspectable")
	 inst.components.inspectable.getstatus = function(inst)
        local sec = inst.components.fueled:GetCurrentSection()
        if sec == 0 then 
            return "OUT"
        elseif sec <= 4 then
            local t = {"EMBERS","LOW","NORMAL","HIGH"}
            return t[sec]
        end
	 end
	-------------------◆[OnBuilt]
	 inst:ListenForEvent("onbuilt", function()
        anim:PlayAnimation("place")
        anim:PushAnimation("idle",false)
        inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel") end)
	-------------------◆
    return inst
end

return Prefab( "common/objects/firepit_ghost", fn, assets, prefabs),
		MakePlacer( "common/firepit_ghost_placer", "fox_firepit_hot", "fox_firepit_hot", "preview")