require "prefabutil"

local assets =
{
    Asset("ANIM", "anim/ui_chest_5x5.zip"),
    Asset("ANIM", "anim/ui_chest_4x4.zip"),
    Asset("ANIM", "anim/ui_chest_3x3.zip"),
    Asset("ANIM", "anim/yiyu_chest.zip"),
}

local function onopen(inst)
    if not inst:HasTag("burnt") then
        inst.AnimState:PlayAnimation("open")
        inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_open")
    end
end

local function onclose(inst)
    if not inst:HasTag("burnt") then
        inst.AnimState:PlayAnimation("closed", true)
        inst.SoundEmitter:PlaySound("dontstarve/wilson/chest_close")
    end
end

local function onhammered(inst, worker)
    inst.components.lootdropper:DropLoot()
    if inst.components.container then inst.components.container:DropEverything() end
    SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst.SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
    inst:Remove()
end

local function onhit(inst, worker)
    if not inst:HasTag("burnt") then
        inst.AnimState:PlayAnimation("hit")
        inst.AnimState:PushAnimation("closed", false)
        if inst.components.container then
            inst.components.container:DropEverything()
            inst.components.container:Close()
        end
    end
end

local function onbuilt(inst)
    inst.AnimState:PlayAnimation("place")
    inst.AnimState:PushAnimation("closed", false)
end


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

local slotpos_3x4 = {}
for y = 2.5, -.5, -1 do
    for x = 0, 2 do
        table.insert(slotpos_3x4, Vector3(75*x-75*2+75, 75*y-75*2+75,0))
    end
end

local slotpos_4x4 = {}
for y = 2.5, -.5, -1 do
    for x = 0, 3 do
        table.insert(slotpos_4x4, Vector3(75*x-93*2+75, 75*y-75*2+75,0))
    end
end

local slotpos_4x5 = {}
for y = 3, 0, -1 do
	for x = 0, 4 do
		table.insert(slotpos_4x5, Vector3(75*x-75*2, 75*y-75*2+70,0))
	end
end

local slotpos_5x5 = {}
for y = 4, 0, -1 do
	for x = 0, 4 do
		table.insert(slotpos_5x5, Vector3(75*x-75*2, 75*y-75*2,0))
	end
end

local function yiyuchest_updata4(inst)--5*5
        inst.components.container:SetNumSlots(#slotpos_5x5)
        inst.components.container.onopenfn = onopen
        inst.components.container.onclosefn = onclose
		inst.components.container.widgetslotpos = slotpos_5x5
		inst.components.container.widgetanimbank = "ui_chest_5x5"
		inst.components.container.widgetanimbuild = "ui_chest_5x5"
        inst.components.container.widgetpos = Vector3(0, 200, 0)
        inst.components.container.side_align_tip = 160
		inst.components.container.widgetbuttoninfo = nil
end

local function yiyuchest_updata3(inst)--4*5
        inst.components.container:SetNumSlots(#slotpos_4x5)
        inst.components.container.onopenfn = onopen
        inst.components.container.onclosefn = onclose
		inst.components.container.widgetslotpos = slotpos_4x5
		inst.components.container.widgetanimbank = "ui_chest_5x5"
		inst.components.container.widgetanimbuild = "ui_chest_5x5"
        inst.components.container.widgetpos = Vector3(0, 200, 0)
        inst.components.container.side_align_tip = 160
--		inst.components.container.widgetbuttoninfo = widgetbuttoninfo
end

local function yiyuchest_updata2(inst)--4*4
        inst.components.container:SetNumSlots(#slotpos_4x4)
        inst.components.container.onopenfn = onopen
        inst.components.container.onclosefn = onclose
		inst.components.container.widgetslotpos = slotpos_4x4
		inst.components.container.widgetanimbank = "ui_chest_4x4"
		inst.components.container.widgetanimbuild = "ui_chest_4x4"
        inst.components.container.widgetpos = Vector3(0, 200, 0)
        inst.components.container.side_align_tip = 160
--		inst.components.container.widgetbuttoninfo = widgetbuttoninfo
end

local function yiyuchest_updata1(inst)--3*4
        inst.components.container:SetNumSlots(#slotpos_3x4)
        inst.components.container.onopenfn = onopen
        inst.components.container.onclosefn = onclose
		inst.components.container.widgetslotpos = slotpos_3x4
		inst.components.container.widgetanimbank = "ui_chester_shadow_3x4"
		inst.components.container.widgetanimbuild = "ui_chester_shadow_3x4"
        inst.components.container.widgetpos = Vector3(0, 200, 0)
        inst.components.container.side_align_tip = 160
--		inst.components.container.widgetbuttoninfo = widgetbuttoninfo
end

local function yiyuchest_updata(inst)--3*3
        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_chest_3x3"
		inst.components.container.widgetanimbuild = "ui_chest_3x3"
        inst.components.container.widgetpos = Vector3(0, 200, 0)
        inst.components.container.side_align_tip = 160
end

local widgetbuttoninfo = {
    text = TUNING.CHEST,
    position = Vector3(0, -175, 0),
    fn = function(inst)
	if not inst.components.container:Has("goldnugget", 10) then
		GetPlayer().components.talker:Say(TUNING.CHEST1)
	elseif inst.components.container:Has("goldnugget", 10) and inst.chest_level and inst.chest_level == 0 then
		inst.chest_level = 1
		inst.components.container:ConsumeByName("goldnugget", 10)
        inst.components.container:Close(GetPlayer())
        yiyuchest_updata1(inst)
	elseif inst.components.container:Has("goldnugget", 10) and inst.chest_level and inst.chest_level == 1 then
		inst.chest_level = 2
		inst.components.container:ConsumeByName("goldnugget", 10)
        inst.components.container:Close(GetPlayer())
        yiyuchest_updata2(inst)
	elseif inst.components.container:Has("goldnugget", 10) and inst.chest_level and inst.chest_level == 2 then
		inst.chest_level = 3
		inst.components.container:ConsumeByName("goldnugget", 10)
        inst.components.container:Close(GetPlayer())
        yiyuchest_updata3(inst)
	elseif inst.components.container:Has("goldnugget", 10) and inst.chest_level and inst.chest_level == 3 then
		inst.chest_level = 4
		inst.components.container:ConsumeByName("goldnugget", 10)
        inst.components.container:Close(GetPlayer())
        yiyuchest_updata4(inst)
	end
	end
}

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

local function OnPreLoad(inst, data)
    if data then
		inst.chest_level = data.chest_level
    if data.chest_level == 4 then
		yiyuchest_updata4(inst)
	elseif data.chest_level == 3 then
		yiyuchest_updata3(inst)
	elseif data.chest_level == 2 then
		yiyuchest_updata2(inst)
	elseif data.chest_level == 1 then
		yiyuchest_updata1(inst)
    end
	else
		yiyuchest_updata(inst)
	end
end

local function onload(inst, data)
    if data then
		inst.chest_level = data.chest_level
		if data.chest_level == 4 then
			yiyuchest_updata4(inst)
		elseif data.chest_level == 3 then
			yiyuchest_updata3(inst)
		elseif data.chest_level == 2 then
			yiyuchest_updata2(inst)
		elseif data.chest_level == 1 then
			yiyuchest_updata1(inst)
		else
			yiyuchest_updata(inst)
		end
	else
		yiyuchest_updata(inst)
	end
end

local function chest(Sim)
        local inst = CreateEntity()
		local trans = inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        local minimap = inst.entity:AddMiniMapEntity()
		minimap:SetIcon("yiyu_chest.tex")
		inst.entity:AddTransform():SetScale(1.2, 1.2, 1.2)
		inst.chest_level = 0

        inst:AddTag("structure")
        inst:AddTag("chest")
        inst.AnimState:SetBank("yiyu_chest")
        inst.AnimState:SetBuild("yiyu_chest")
        inst.AnimState:PlayAnimation("closed")
        inst:AddComponent("inspectable")

        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_chest_3x3"
		inst.components.container.widgetanimbuild = "ui_chest_3x3"
        inst.components.container.widgetpos = Vector3(0, 200, 0)
        inst.components.container.side_align_tip = 160
		inst.components.container.widgetbuttoninfo = widgetbuttoninfo

        inst:AddComponent("lootdropper")
        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(4)
        inst.components.workable:SetOnFinishCallback(onhammered)
        inst.components.workable:SetOnWorkCallback(onhit)

        inst:ListenForEvent("onbuilt", onbuilt)
        MakeSnowCovered(inst, .01)
        inst.OnSave = onsave
        inst.OnLoad = onload
		inst.OnPreLoad = OnPreLoad
        return inst
end

return Prefab("common/yiyu_chest", chest, assets, prefabs),
	   MakePlacer("common/yiyu_chest_placer", "yiyu_chest", "yiyu_chest", "closed")