local assets =
{
	Asset("ANIM", "anim/jiandai.zip"),
    Asset("ATLAS", "images/inventoryimages/jiandai.xml"),
    Asset("IMAGE", "images/inventoryimages/jiandai.tex"),
    Asset("ANIM", "anim/ui.zip"),
}

local function nofuel(inst)
    if inst:HasTag("fridge") then
	   inst:RemoveTag("fridge")
	end
end

local function onupdatefueledraining(inst)
    local owner = inst.components.inventoryitem.owner
    local fuelpercent = inst.components.fueled:GetPercent()
		if not inst.components.fueled:IsEmpty() then
	           inst:AddTag("fridge")
        end
end


local function OnLoad(inst, data)
    if inst.components.fueled:IsEmpty() then
        nofuel(inst)
	elseif not inst.components.fueled:IsEmpty() and not inst:HasTag("fridge")then
	inst:AddTag("fridge")
    end
end

local function onopen(inst)
	inst.AnimState:PlayAnimation("open", false)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")
end

local function onclose(inst)
	inst.AnimState:PlayAnimation("closed", false)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")
end

local slotpos = {}

for y = 4, 0, -1 do
		for x = 0, 4 do
			table.insert(slotpos, Vector3(80*x-80*2+80, 80*y-80*2-80,0))
	end
end



local function fn()
	local inst = CreateEntity()
    
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
    --inst.entity:AddMiniMapEntity()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("jiandai")
    inst.AnimState:SetBuild("jiandai")
    inst.AnimState:PlayAnimation("idle")

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "jiandai.tex" )
    
    --inst.foleysound = "dontstarve/movement/foley/backpack"
    
    --交易组件
	inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(function(inst, item) return item and item.prefab=="ice" end)
    inst.components.trader.onaccept=function(inst,giver,item)
    if inst.components.fueled.currentfuel <= inst.components.fueled.maxfuel then
	   inst.components.fueled.currentfuel = inst.components.fueled.currentfuel + 100
	   	inst.components.fueled:StartConsuming()
    else
	inst.components.fueled.currentfuel = inst.components.fueled.maxfuel
		inst.components.fueled:StartConsuming()
    end



		end
    inst.components.trader.onrefuse=function(inst,giver,item) if giver.components.talker then giver.components.talker:Say("用冰添加燃料") end end
	
	
    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.keepondeath = true
	inst.components.inventoryitem.imagename = "jiandai"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/jiandai.xml"
	inst.components.inventoryitem.cangoincontainer = true

    inst:AddComponent("fueled")
	--inst.components.fueled.fueltype = "CHARCOAL"
    inst.components.fueled:InitializeFuelLevel(300)
    inst.components.fueled:SetDepletedFn(nofuel)
    --inst.components.fueled:SetFirstPeriod(TUNING.TURNON_FUELED_CONSUMPTION, TUNING.TURNON_FULL_FUELED_CONSUMPTION)
	inst.components.fueled:StartConsuming()

	inst:AddComponent("container")
    inst.components.container:SetNumSlots(#slotpos)
    inst.components.container.widgetslotpos = slotpos
	inst.components.container.widgetpos = Vector3(0,100,0)
	inst.components.container.side_align_tip = 160
    inst.components.container.widgetanimbank = "ui"
    inst.components.container.widgetanimbuild = "ui"


    inst.components.container.onopenfn = onopen
	inst.components.container.onclosefn = onclose
    inst.components.container:Open()
    inst.components.container:Close()



    inst.OnLoad = OnLoad

  

    --inst:AddTag("fridge")
    --inst:AddTag("structure")


    return inst
end

return Prefab("common/inventory/jiandai", fn, assets)