require "prefabutil"

local assets =
{
Asset("ANIM", "anim/gate.zip"),
Asset("IMAGE", "images/inventoryimages/gate.tex"),
Asset("ATLAS", "images/inventoryimages/gate.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"  then
       return true
    end
    return false
end

--function Container:GetItemByName(item, amount)

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

    fn = function(inst)
        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_gate3)
	--elseif item and item.weapon_upgrade and not inst.components.container:Has("zijing", 1) then
		--GetPlayer().components.talker:Say(STRINGS.YUYI_gate4)
	--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_gate5)
	elseif item and item.weapon_upgrade and item.weapon_upgrade >= 20 and inst.components.container:Has("zijing", item.weapon_upgrade) then
		inst.components.container:DropItem(item)
		inst.components.container:Close()
		GetPlayer().components.talker:Say(STRINGS.YUYI_gate6)
	elseif item and item.weapon_upgrade and item.weapon_upgrade < 20 and inst.components.container:Has("zijing", item.weapon_upgrade) then
		keqianghua =true
	end


	if keqianghua and inst.components.container:Has("zijing", item.weapon_upgrade) then
		local chance = math.random()
		local chances = math.random()
		GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/rock_break")

		if item and item.weapon_upgrade and item.weapon_upgrade < 6 then--12345
			weapon_upgrade_chance = 1
		elseif item and item.weapon_upgrade and GetPlayer().components.inventory and GetPlayer().components.inventory:Has("qianghuafu", 1) then
			weapon_upgrade_chance = 1
		elseif item and item.weapon_upgrade and item.weapon_upgrade >= 12 and item.weapon_upgrade < 20 then
			weapon_upgrade_chance = 1/item.weapon_upgrade
		elseif item and item.weapon_upgrade and item.weapon_upgrade >= 9 and item.weapon_upgrade < 12 then --678910
			weapon_upgrade_chance = 0.15
		elseif item and item.weapon_upgrade and item.weapon_upgrade >= 6 and item.weapon_upgrade < 9 then --678910
			weapon_upgrade_chance = 0.33
		end

	if chances <= weapon_upgrade_chance and item.components.weapon then
			if item.weapon_upgrade == 0 and item.weapon_defense then--df0
				item.weapon_defense = item.weapon_defense + 10
			elseif item.weapon_upgrade == 3 and item.weapon_bloodsuck then--bl3
				item.weapon_bloodsuck = item.weapon_bloodsuck + 1.5
			elseif item.weapon_upgrade == 5 and item.weapon_combo_multiplier then--df6
				if item.prefab == "yiyuhuan" then item.weapon_adddmg = item.weapon_adddmg + 2.5
				else
				item.weapon_combo_multiplier = item.weapon_combo_multiplier+0.01
				end
			elseif item.weapon_upgrade == 6 and item.weapon_defense then--df6
				item.weapon_defense = item.weapon_defense + 10
			elseif item.weapon_upgrade == 9 and item.weapon_bloodsuck then--bl9
				item.weapon_bloodsuck = item.weapon_bloodsuck + 1.5
			elseif item.weapon_upgrade == 12 and item.weapon_defense then--df6
				item.weapon_defense = item.weapon_defense + 10
			elseif item.weapon_upgrade == 15 and item.weapon_combo_multiplier then--df6
				if item.prefab == "yiyuhuan" then item.weapon_adddmg = item.weapon_adddmg + 2.5
				else
				item.weapon_combo_multiplier = item.weapon_combo_multiplier+0.01
				end
			elseif item.weapon_upgrade == 18 and item.weapon_defense then--df12
				item.weapon_defense = item.weapon_defense + 10
----------------------------------------------------------------------------
			elseif chance <= 0.165 and item.weapon_combo_multiplier then
				if item.prefab == "yiyuhuan" then item.weapon_adddmg = item.weapon_adddmg + 2.5
				else
				item.weapon_combo_multiplier = item.weapon_combo_multiplier+0.01
				end
			elseif chance <= 0.330 and item.weapon_hitcritical then
				item.weapon_hitcritical = item.weapon_hitcritical + 2.5
			elseif chance <= 0.495 and item.weapon_panic then
				item.weapon_panic = item.weapon_panic + 2.5
			elseif chance <= 0.660 and item.weapon_darkdness then
				item.weapon_darkdness = item.weapon_darkdness + 2.5
			elseif chance <= 0.825 and item.weapon_slowly then
				item.weapon_slowly = item.weapon_slowly + 2.5
			else
				if item.weapon_adddmg then item.weapon_adddmg = item.weapon_adddmg + 2.5 end
			end

		if item and item.weapon_upgrade and item.weapon_upgrade >= 6 then GetPlayer().components.inventory:ConsumeByName("qianghuafu", 1) end
		inst.components.container:ConsumeByName("zijing", item.weapon_upgrade)
		inst.components.container:DropItem(item)
		inst.components.container:Close()
		if item and item.weapon_upgrade then item.weapon_upgrade = item.weapon_upgrade + 1 end

	elseif chances <= weapon_upgrade_chance and (item.prefab == "yuyi_hat2" or item.prefab == "yuyi_hat3" or item.prefab == "yuyi_sack3") then
		if item.armor_health then item.armor_health = item.armor_health + 1 end
		if item.prefab == "yuyi_sack3" and item.armor_godbless == 0 then
			item.armor_godbless = 1
		elseif chance <= 0.20 and item.armor_absorb and item.components.armor.absorb_percent < 0.85 then
			item.armor_absorb = item.armor_absorb + 0.04
			item.components.armor:SetAbsorption(0.80+(1-TUNING.DIFFICULTY_YUYI)/10+item.armor_absorb)
		elseif chance <= 0.40 and item.armor_dodge and item.armor_dodge < 30 then
				item.armor_dodge = item.armor_dodge + 4
		elseif chance <= 0.60 and item.armor_drop and item.armor_drop < 64 then
				item.armor_drop = item.armor_drop + 4
		elseif chance <= 0.80 and item.weapon_hunger and item.weapon_hunger < 30 then
				item.weapon_hunger = item.weapon_hunger + 4
		elseif chance <= 1.00 and item.armor_thorns and item.armor_thorns < 21 then
				item.armor_thorns = item.armor_thorns + 3
--		elseif chance <= 0.85 and item.armor_godbless and item.armor_godbless < 10 then
--			item.armor_godbless = item.armor_godbless + 1
		end

		if item and item.weapon_upgrade and item.weapon_upgrade >= 6 then
		GetPlayer().components.inventory:ConsumeByName("qianghuafu", 1)
		end
		inst.components.container:ConsumeByName("zijing", item.weapon_upgrade)
		inst.components.container:DropItem(item)
		inst.components.container:Close()
		if item and item.weapon_upgrade then
			item.weapon_upgrade = item.weapon_upgrade + 1
		end
elseif chances <= weapon_upgrade_chance and (item.prefab == "yiyuhattop" or item.prefab == "yiyu_glass" or item.prefab == "yiyu_headdress" or item.prefab == "yiyusack" or item.prefab == "snailarmor" or item.prefab == "yiyu_wing") then
		if item.armor_health then item.armor_health = item.armor_health + 1 end
		if chance <= 0.20 and item.armor_absorb then
			if item.prefab == "yiyuhattop" and item.armor_absorb < 0.30 then
				item.armor_absorb = item.armor_absorb + 0.06
				item.components.armor:SetAbsorption(0.5+item.armor_absorb)
			elseif item.prefab == "yiyu_glass" and item.armor_absorb < 0.40 then
				item.armor_absorb = item.armor_absorb + 0.08
				item.components.armor:SetAbsorption(0.4+item.armor_absorb)
			elseif item.prefab == "yiyu_headdress" and item.armor_absorb < 0.40 then
				item.armor_absorb = item.armor_absorb + 0.08
				item.components.armor:SetAbsorption(0.4+item.armor_absorb)
			elseif item.prefab == "yiyusack" and item.armor_absorb < 0.20 then
				item.armor_absorb = item.armor_absorb + 0.04
				item.components.armor:SetAbsorption(0.65+item.armor_absorb)
			elseif item.prefab == "snailarmor" and item.armor_absorb < 0.10 then
				item.armor_absorb = item.armor_absorb + 0.02
				item.components.armor:SetAbsorption(0.85+(1-TUNING.DIFFICULTY_YUYI)/10+item.armor_absorb)
			elseif item.prefab == "yiyu_wing" and item.armor_absorb < 0.30 then
				item.armor_absorb = item.armor_absorb + 0.06
			else
				inst.components.container:ConsumeByName("zijing", 1)
				inst.components.container:DropItem(item)
				inst.components.container:Close()
			end
		elseif chance <= 0.375 and item.armor_fire_resistance then
			if item.prefab == "yiyu_glass" and item.armor_fire_resistance < 1 then
				item.armor_fire_resistance = item.armor_fire_resistance + 0.2
			elseif item.armor_fire_resistance < 0.5 then
				item.armor_fire_resistance = item.armor_fire_resistance + 0.1
			else
				item.armor_drop = item.armor_drop + 5
			end
		elseif chance <= 0.5 and item.armor_cold_resistance and item.armor_cold_resistance < 10 then
			item.armor_cold_resistance = item.armor_cold_resistance + 1
		elseif chance <= 0.625 and item.armor_winter_temp and item.armor_winter_temp < 120 and item.prefab ~= "yiyu_wing" then
			item.armor_winter_temp = item.armor_winter_temp + 30
		elseif chance <= 0.75 and item.armor_summer_temp and item.armor_summer_temp < 120 and item.prefab ~= "yiyu_wing" then
			item.armor_summer_temp = item.armor_summer_temp + 30
		elseif chance <= 0.85 and item.prefab == "yiyu_wing" and item.weapon_upgrade and item.armor_godbless and item.armor_godbless < 10 then
			item.armor_godbless = item.armor_godbless + 1
		elseif chance <= 0.875 and item.armor_dodge and item.armor_dodge < 48 then
			if item.prefab == "yiyu_glass" then
				item.armor_dodge = item.armor_dodge + 6
			elseif item.prefab == "yiyu_headdress" then
				item.armor_dodge = item.armor_dodge + 6
			else
				item.armor_dodge = item.armor_dodge + 4
			end
		elseif item.armor_drop and item.armor_drop < 50 then
			if item.prefab == "snailarmor" then
				item.armor_drop = item.armor_drop + 10
			else
				item.armor_drop = item.armor_drop + 5
			end
		end
		if item and item.weapon_upgrade and item.weapon_upgrade >= 6 then
		GetPlayer().components.inventory:ConsumeByName("qianghuafu", 1)
		end
		inst.components.container:ConsumeByName("zijing", item.weapon_upgrade)
		inst.components.container:DropItem(item)
		inst.components.container:Close()
		if item and item.weapon_upgrade then
			item.weapon_upgrade = item.weapon_upgrade + 1
		end
	elseif chances > weapon_upgrade_chance then
		inst.components.container:ConsumeByName("zijing", 1)
		inst.components.container:DropItem(item)
		inst.components.container:Close()
	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("gate")
	anim:SetBuild("gate")
	anim:PlayAnimation("idle", true)

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "gate.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/wilson/equip_item_gold") end
    inst.components.container.onclosefn = function(inst) GetPlayer().SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold") end

	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/gate", fn, assets, prefabs ),
       MakePlacer("common/gate_placer", "gate", "gate", "idle",nil ,nil ,nil ,2) 