require "prefabutil"

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

local prefabs = 
{
}

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

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

local function onbuilt(inst)
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:PushAnimation("idle", true)
end

local slotpos = {}
for y = 0, 1 do
	table.insert(slotpos, Vector3(-162 +(75/2), -y*75 + 114 ,0))
end

local function itemtest(inst, item, slot)
    if item.prefab == "sheath" or "purplegem" then
       return true
    end
    return false
end

--function Container:GetItemByName(item, amount)

local widgetbuttoninfo = {
    text = "修复",
    position = Vector3(-125, -40, 0),

    fn = function(inst)
		local player = GetPlayer()
        local item = inst.components.container.slots[1]
		local keqianghua = false
		local weapon_upgrade_chance = 1
	if not item then
		GetPlayer().components.talker:Say("没有物品")
	--elseif not inst.components.container:Has("purplegem", 1) then
		--GetPlayer().components.talker:Say("未放入材料紫宝石")
	--elseif item and not item.weapon_upgrade then
		--GetPlayer().components.talker:Say(STRINGS.YUYI_jianzhong3)
	--elseif item and item.weapon_upgrade and not inst.components.container:Has("zijing", 1) then
		--GetPlayer().components.talker:Say(STRINGS.YUYI_jianzhong4)
	--elseif item and item.weapon_upgrade and not inst.components.container:Has("zijing", item.weapon_upgrade) then
		--inst.components.container:DropItem(item)
		--inst.components.container:Close()
		--GetPlayer().components.talker:Say(STRINGS.YUYI_jianzhong5)
	elseif item.prefab == "jianqiao" then
	 if item.components.armor.condition then
	    item.components.armor.condition = 4200
	 end
		inst.components.container:DropItem(item)
		inst.components.container:Close()
		GetPlayer().components.talker:Say("修复成功")
	elseif item.prefab == "jianqiao2" then
	 if item.components.armor.condition then
	    item.components.armor.condition = 4200
	 end
		inst.components.container:DropItem(item)
		inst.components.container:Close()
		GetPlayer().components.talker:Say("修复成功")
	elseif item.prefab == "futou" then
	 if item.components.finiteuses then
	    item.components.finiteuses.current = 3600
	 end
		inst.components.container:DropItem(item)
		inst.components.container:Close()
		GetPlayer().components.talker:Say("修复成功")
	elseif item.prefab == "sword_4" then
	       if not inst.components.container:Has("thulecite", 1) then
		       GetPlayer().components.talker:Say("请放入第二格远古铥矿进行锻造！")
		   elseif item and not inst.components.container:Has("thulecite", 8) then
		       GetPlayer().components.talker:Say("如果你是想升级断剑，那么需要8个远古铥矿哦")
		   elseif item and inst.components.container:Has("thulecite", 8) then
		       inst.components.container:ConsumeByName("sword_4", 1)
		       inst.components.container:ConsumeByName("thulecite", 8)
			   --inst.components.container:GiveItem("sword_77", nil, nil, nil, nil)
			   --inst.components.container:DropItem("sword_77")

			   inst.components.container:Close()
			   player.components.inventory:Equip(SpawnPrefab("sword_77"))
			   GetPlayer().components.talker:Say("恭喜！得到史诗装备-魔剑")
		   end

	end



end}

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	trans:SetScale(2.0, 2.0, 2.0)
	MakeObstaclePhysics(inst, 1)

	local light = inst.entity:AddLight()
	inst.Light:SetRadius(3.5)
	inst.Light:SetFalloff(0.85)
	inst.Light:SetIntensity(.85)
--	inst.Light:SetColour(255/255, 0/255, 255/255)
	inst.Light:Enable(true)

	anim:SetBank("jianzhong")
	anim:SetBuild("jianzhong")
	anim:PlayAnimation("idle", true)

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "jianzhong.tex" )
	
	inst:AddTag("structure")
	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:AddComponent("inspectable")

    inst:AddComponent("container")
    inst.components.container:SetNumSlots(#slotpos)
    inst.components.container.widgetslotpos = slotpos
    inst.components.container.widgetpos = Vector3(0,150,0)
    inst.components.container.side_align_tip = 160
    inst.components.container.itemtestfn = itemtest
    inst.components.container.widgetbuttoninfo = widgetbuttoninfo
    inst.components.container.canbeopened = true
    inst.components.container.onopenfn = function(inst) GetPlayer().SoundEmitter:PlaySound("dontstarve/common/craftable/icebox_open") end
    inst.components.container.onclosefn = function(inst) GetPlayer().SoundEmitter:PlaySound("dontstarve/common/craftable/icebox_open") end
	--inst.SoundEmitter:PlaySound("dontstarve/common/craftable/icebox_open")
	MakeSnowCovered(inst, .01) 
	inst:ListenForEvent("onbuilt", onbuilt)
	inst:ListenForEvent( "daytime", function() inst.Light:Enable(false) end , GetWorld() )
	inst:ListenForEvent( "dusktime", function() inst.Light:Enable(true) end , GetWorld() )
--------------------------------------

return inst
end

return Prefab( "common/jianzhong", fn, assets, prefabs ),
       MakePlacer("common/jianzhong_placer", "jianzhong", "jianzhong", "idle",nil ,nil ,nil ,2) 