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






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 = 0, 3 do
	table.insert(slotpos, Vector3(-162 +(75/2), -y*75 + 114 ,0))
end

local slotpos_2x4 = {}
for y = 0, 3 do
	table.insert(slotpos_2x4, Vector3(-162, -y*75 + 114 ,0))
	table.insert(slotpos_2x4, Vector3(-162 +75, -y*75 + 114 ,0))
end

local slotpos_2x6 = {}
for y = 0, 5 do
	table.insert(slotpos_2x6, Vector3(-162, -y*75 + 170 ,0))
	table.insert(slotpos_2x6, Vector3(-162 +75, -y*75 + 170 ,0))
end

local slotpos_2x8 = {}
for y = 0, 6 do
	table.insert(slotpos_2x8, Vector3(-162, -y*75 + 240 ,0))
	table.insert(slotpos_2x8, Vector3(-162 +75, -y*75 + 240 ,0))
end

local function tosobackpack_updata3(inst)--2x8
    inst.components.container:SetNumSlots(#slotpos_2x8)
    inst.components.container.onopenfn = onopen
	inst.components.container.onclosefn = onclose
    inst.components.container.widgetslotpos = slotpos_2x8
    inst.components.container.widgetanimbank = "ui_krampusbag_2x8"
    inst.components.container.widgetanimbuild = "ui_krampusbag_2x8"
    --inst.components.container.widgetpos = Vector3(645,-85,0)
    inst.components.container.widgetpos = Vector3(-5,-75,0)
	inst.components.container.side_widget = true    
    inst.components.container.type = "pack"
end

local function tosobackpack_updata2(inst)--2x6
    inst.components.container:SetNumSlots(#slotpos_2x6)
    inst.components.container.onopenfn = onopen
	inst.components.container.onclosefn = onclose
    inst.components.container.widgetslotpos = slotpos_2x6
    inst.components.container.widgetanimbank = "ui_piggyback_2x6"
    inst.components.container.widgetanimbuild = "ui_piggyback_2x6"
    inst.components.container.widgetpos = Vector3(-5,-50,0)
	inst.components.container.side_widget = true  
    inst.components.container.type = "pack" 
	--inst.components.container.widgetbuttoninfo = widgetbuttoninfo
end

local function tosobackpack_updata1(inst)--2x4
    inst.components.container:SetNumSlots(#slotpos_2x4)
    inst.components.container.onopenfn = onopen
	inst.components.container.onclosefn = onclose
    inst.components.container.widgetslotpos = slotpos_2x4
    inst.components.container.widgetanimbank = "ui_backpack_2x4"
    inst.components.container.widgetanimbuild = "ui_backpack_2x4"
    inst.components.container.widgetpos = Vector3(-5,-50,0)
    inst.components.container.side_widget = true
    inst.components.container.type = "pack"
	--inst.components.container.widgetbuttoninfo = widgetbuttoninfo
end

local function tosobackpack_updata(inst)--4*5
    inst.components.container:SetNumSlots(#slotpos)
    inst.components.container.onopenfn = onopen
	inst.components.container.onclosefn = onclose
    inst.components.container.widgetslotpos = slotpos
    inst.components.container.widgetanimbank = "ui_thatchpack_1x4"
    inst.components.container.widgetanimbuild = "ui_thatchpack_1x4"
    inst.components.container.widgetpos = Vector3(-5,-70,0)
    inst.components.container.side_widget = true
    inst.components.container.type = "pack"
	--inst.components.container.widgetbuttoninfo = widgetbuttoninfo

end

local widgetbuttoninfo = {
    text = "升级",
    position = Vector3(0, -175, 0),
    fn = function(inst)
	if not inst.components.container:Has("livinglog", 3) then
		GetPlayer().components.talker:Say("需要活木升级")
	elseif inst.components.container:Has("livinglog", 3) and inst.backpack_level and inst.backpack_level == 0 then
		inst.backpack_level = 1
		inst.components.container:ConsumeByName("livinglog", 3)
        inst.components.container:Close(GetPlayer())
        tosobackpack_updata1(inst)
	elseif inst.components.container:Has("livinglog", 3) and inst.backpack_level and inst.backpack_level == 1 then
		inst.backpack_level = 2
		inst.components.container:ConsumeByName("livinglog", 3)
        inst.components.container:Close(GetPlayer())
        tosobackpack_updata2(inst)
	elseif inst.components.container:Has("livinglog", 3) and inst.backpack_level and inst.backpack_level == 2 then
		inst.backpack_level = 3
		inst.components.container:ConsumeByName("livinglog", 3)
        inst.components.container:Close(GetPlayer())
        tosobackpack_updata3(inst)
	end
	end
}

local function onsave(inst, data)
	data.backpack_level = inst.backpack_level
end

local function OnPreLoad(inst, data)
    if data then
		inst.backpack_level = data.backpack_level
    if data.backpack_level == 3 then
		tosobackpack_updata3(inst)
	elseif data.backpack_level == 2 then
		tosobackpack_updata2(inst)
	elseif data.backpack_level == 1 then
		tosobackpack_updata1(inst)
    end
	else
		tosobackpack_updata(inst)
	end
end

local function onload(inst, data)
    if data then
		inst.backpack_level = data.backpack_level
		if data.backpack_level == 3 then
			tosobackpack_updata3(inst)
		elseif data.backpack_level == 2 then
			tosobackpack_updata2(inst)
		elseif data.backpack_level == 1 then
			tosobackpack_updata1(inst)
		else
			tosobackpack_updata(inst)
		end
	else
		tosobackpack_updata(inst)
	end
end

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

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

    --inst.MiniMapEntity:SetIcon("backpack.png")
    
    --inst.foleysound = "dontstarve/movement/foley/backpack"
    


	
	
    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("container")
    inst.components.container:SetNumSlots(#slotpos)
    inst.components.container.onopenfn = onopen
	inst.components.container.onclosefn = onclose
    inst.components.container.widgetslotpos = slotpos
    inst.components.container.widgetanimbank = "ui_thatchpack_1x4"
    inst.components.container.widgetanimbuild = "ui_thatchpack_1x4"
    inst.components.container.widgetpos = Vector3(-5,-70,0)
    inst.components.container.side_widget = true
    inst.components.container.type = "pack"
	inst.components.container.widgetbuttoninfo = widgetbuttoninfo


    --inst.components.container:Open()
    --inst.components.container:Close()


    inst.OnSave = onsave
    inst.OnLoad = onload
    inst.OnPreLoad = OnPreLoad


  



    return inst
end

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