local assets=
{ 
	Asset("ANIM", "anim/jianqiao.zip"),
	Asset("ANIM", "anim/swap_jianqiao.zip"),
	Asset("ANIM", "anim/jianqiao2.zip"),
	Asset("ANIM", "anim/swap_jianqiao2.zip"),
    Asset("ATLAS", "images/inventoryimages/jianqiao.xml"),
    Asset("IMAGE", "images/inventoryimages/jianqiao.tex"),
    Asset("ATLAS", "images/inventoryimages/jianqiao2.xml"),
    Asset("IMAGE", "images/inventoryimages/jianqiao2.tex"),
	
	Asset("ANIM", "anim/ui_thatchpack_1x4.zip"),
}

local prefabs = 
{
    "sheath",
    "sheath2",
}

local function OnDurability(inst, data)
	inst:AddTag("broken")
    inst.components.armor.absorb_percent = 0.2
	inst.components.talker:Say("护鞘损坏")
end

local function TakeItem(inst, item, data)
	inst.components.fueled:DoDelta(3999820)
	if inst:HasTag("haihun") then
		inst.components.weapon:SetDamage(35)
	elseif inst:HasTag("longya") then
		inst.components.weapon:SetDamage(45)
	elseif inst:HasTag("xueyin") then
		inst.components.weapon:SetDamage(55+inst.weapon_adddmg)
	end
	inst:RemoveTag("broken")
end

--更新护甲
local function Upgradearmor(inst)
	if not inst:HasTag("broken") then
    inst.components.armor.absorb_percent = 0.5+inst.absorb_extra
	end
	if inst.components.fueled:IsEmpty() then
		inst:AddTag("broken")
    inst.components.armor.absorb_percent = 0.2
	end
end

--将护甲余额转化为燃料数值
local function PercentChanged(inst, data)
local percent = inst.components.armor.condition/inst.components.armor.maxcondition
local tosofpercent = 3600*percent
	inst.components.fueled.currentfuel = tosofpercent
end

local function TakeItems(inst, item)
local player = GetPlayer()
local tosoarmor = inst.components.armor.condition + 360
if tosoarmor <= 3600 then
    inst.components.armor:SetCondition(tosoarmor)
	inst.components.fueled.currentfuel = tosoarmor
else
    inst.components.armor:SetCondition(3600)
	inst.components.fueled.currentfuel = 3600
end
end

local function ShouldAcceptItem(inst, item)
	if item.prefab == "nightmarefuel" then return true else return false end
end

--只能放固定物品
local function itemtest(inst, item, slot)
	return item:HasTag("weapon_sword")
end

-------护盾特效
local function texiaozu(inst, owner)
    inst.components.armor:SetAbsorption(1)
    
    local fx = SpawnPrefab("forcefieldfx")
    fx.entity:SetParent(owner.entity)
    fx.Transform:SetPosition(0, 0.2, 0)
    fx.Transform:SetScale(1.2, 1.2, 1.2)
    fx.AnimState:PlayAnimation("hit")
    fx.AnimState:PushAnimation("idle_loop")

    inst.components.armor.ontakedamage = function(inst, damage_amount)
        if owner then
            local sanity = owner.components.sanity
            if sanity then
                local unsaneness = damage_amount * 0.05
                sanity:DoDelta(-unsaneness, false)
            end
        end
    end
    inst.active = true
    --冷却时间5s
    owner:DoTaskInTime(5, function()
        fx.kill_fx(fx)
        if inst:IsValid() then
            inst.components.armor.ontakedamage = nil
            --owner.components.health.absorb = 0
            inst.components.armor:SetAbsorption(0.8)
            owner:DoTaskInTime(15, function() inst.active = false end)
        end
    end)
end


local function tryproc(inst, owner)
    if not inst.active and math.random() < 0.2 then
        texiaozu(inst, owner)
    end
end

-----装备函数
local function OnEquipSheath(inst, owner) 
    owner.AnimState:OverrideSymbol("swap_body", "swap_jianqiao", "backpack")
    owner.AnimState:OverrideSymbol("swap_body", "swap_jianqiao", "swap_body")
end

local function OnUnequipSheath(inst, owner) 
    owner.AnimState:ClearOverrideSymbol("swap_body")
    owner.AnimState:ClearOverrideSymbol("backpack")
end

local function OnEquipSheath2(inst, owner) 
    owner.AnimState:OverrideSymbol("swap_body", "swap_jianqiao2", "backpack")
    owner.AnimState:OverrideSymbol("swap_body", "swap_jianqiao2", "swap_body")
        inst.procfn = function() tryproc(inst, owner) end
        owner:ListenForEvent("attacked", inst.procfn) 
end

local function OnUnequipSheath2(inst, owner) 
    owner.AnimState:ClearOverrideSymbol("swap_body")
    owner.AnimState:ClearOverrideSymbol("backpack")
 owner:RemoveEventCallback("attacked", inst.procfn)
end

--开关音效
local function onopen(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_open", "open")
end

local function onclose(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_close", "open")
end


--格子
local slotpos_1x2 = {}

for x = 2, 3 do
	table.insert(slotpos_1x2, Vector3(-x*75 - 1060 , -162 - 330,0))
end

local slotpos_1x5 = {}

for x = 0, 4 do
	table.insert(slotpos_1x5, Vector3(-x*75 - 1060 , -162 - 330,0))
end
------------------------------------------------------------



local function onpreload(inst, data)
	if data then
	inst.absorb_extra = data.absorb_extra
	Upgradearmor(inst)
	end
end

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

local function OnLoad(inst, data)
	if data then
	inst.absorb_extra = data.absorb_extra
	Upgradearmor(inst)
	end
end

------------------------------------------------------------

local function sheathfn(colour)
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    inst.entity:AddSoundEmitter()

	--inst.absorb_extra = 0

	anim:SetBank("jianqiao")
	anim:SetBuild("jianqiao")
    anim:PlayAnimation("idle")

	inst:AddTag("sheath")
    inst:AddComponent("talker")
    inst.components.talker.fontsize = 24
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(1, 0.8, 0.65, 1)
    inst.components.talker.offset = Vector3(200,-250,0)
    inst.components.talker.symbol = "swap_object"
	
    inst:AddComponent("armor")
    inst.components.armor:InitCondition(3600, 0.5)

    inst:AddComponent("container")
    inst.components.container.itemtestfn = itemtest	
    inst.components.container:SetNumSlots(#slotpos_1x2)
    inst.components.container.widgetslotpos = slotpos_1x2
    --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.onopenfn = onopen
    inst.components.container.onclosefn = onclose	
	
    inst:AddComponent("fueled")
    inst.components.fueled.fueltype = "NIGHTMARE"
    inst.components.fueled:InitializeFuelLevel(10000000)
    inst.components.fueled:SetDepletedFn(OnDurability)
    inst.components.fueled.ontakefuelfn = TakeItem
    inst.components.fueled.accepting = true
    inst.components.fueled:StopConsuming()
	inst:ListenForEvent("percentusedchange", PercentChanged)	

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "jianqiao.tex" )
    inst:AddComponent("inspectable")	
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "jianqiao"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/jianqiao.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.BODY
    inst.components.equippable:SetOnEquip( OnEquipSheath )
    inst.components.equippable:SetOnUnequip( OnUnequipSheath )
	if not IsDLCEnabled(REIGN_OF_GIANTS) then
	MakeInventoryFloatable(inst, "idle_water", "idle")
	end
    return inst
end

local function sheath2fn(colour)
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    inst.entity:AddSoundEmitter()
	inst.absorb_extra = 0
	
	
	
    inst.OnLoad = OnLoad
    inst.OnSave = onsave
    inst.OnPreLoad = onpreload
	
	anim:SetBank("jianqiao2")
	anim:SetBuild("jianqiao2")
    anim:PlayAnimation("idle")
	
	inst:AddTag("sheath2")
    inst:AddComponent("talker")
    inst.components.talker.fontsize = 24
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(1, 0.8, 0.65, 1)
    inst.components.talker.offset = Vector3(200,-250,0)
    inst.components.talker.symbol = "swap_object"

    inst:AddComponent("armor")
    inst.components.armor:InitCondition(3600, 0.5+inst.absorb_extra)

    inst:AddComponent("container")
    inst.components.container.itemtestfn = itemtest	
    inst.components.container:SetNumSlots(#slotpos_1x5)
    inst.components.container.widgetslotpos = slotpos_1x5
    --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.onopenfn = onopen
    inst.components.container.onclosefn = onclose		
	
    inst:AddComponent("fueled")
    inst.components.fueled.fueltype = "NIGHTMARE"
    inst.components.fueled:InitializeFuelLevel(17500000)
    inst.components.fueled:SetDepletedFn(OnDurability)
    inst.components.fueled.ontakefuelfn = TakeItem
    inst.components.fueled.accepting = true
    inst.components.fueled:StopConsuming()
	inst:ListenForEvent("percentusedchange", PercentChanged)
	
	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "jianqiao2.tex" )
    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "jianqiao2"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/jianqiao2.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.BODY
    inst.components.equippable:SetOnEquip( OnEquipSheath2 )
    inst.components.equippable:SetOnUnequip( OnUnequipSheath2 )
	if not IsDLCEnabled(REIGN_OF_GIANTS) then
		MakeInventoryFloatable(inst, "idle_water", "idle")
	end
    return inst
end

return  Prefab("common/inventory/sheath", sheathfn, assets, prefabs),
		Prefab("common/inventory/sheath2", sheath2fn, assets, prefabs)
