local assets =
{
    Asset("ANIM", "anim/san.zip"),
	Asset("ANIM", "anim/swap_san.zip"),
	Asset("ANIM", "anim/swap_tosog.zip"),
	Asset("ATLAS", "images/inventoryimages/san.xml"),
}

local function canuse()
       return true
end

local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "swap_tosog", "swap_tosog")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
		inst.isunfolded = false
	--inst.components.weapon:SetRange(0, 0)
	inst.components.useableitem.inuse = false
end

local function onunequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
		owner.DynamicShadow:SetSize(1.3, 0.6)	
	--owner.components.health.absorb = (0.2 * TUNING.PW)
	--owner:RemoveTag("spring")
	inst.isunfolded = false
	--inst.components.weapon:SetDamage(25 * TUNING.PW + 25)
	inst.components.waterproofer:SetEffectiveness(0)
end

local function unfoldit(inst)

local owner = inst.components.inventoryitem.owner
	if inst.isunfolded then
			owner:PushEvent("unequip", {item=inst, eslot=EQUIPSLOTS.HANDS})
		owner:PushEvent("equip", {item=inst, eslot=EQUIPSLOTS.HANDS})
		owner.AnimState:OverrideSymbol("swap_object", "swap_tosog", "swap_tosog")
		owner.SoundEmitter:PlaySound("dontstarve/wilson/use_umbrella_down")
		owner.DynamicShadow:SetSize(1.3, 0.6)
		--owner.components.health.absorb = (0.2 * TUNING.PW)
		--owner:RemoveTag("spring")
		--inst.components.weapon:SetRange(0, 0)
		inst.components.weapon:SetDamage(20)
		inst.components.waterproofer:SetEffectiveness(0)
		inst.isunfolded = false
	else
        owner:PushEvent("unequip", {item=inst, eslot=EQUIPSLOTS.HANDS})
		owner:PushEvent("equip", {item=inst, eslot=EQUIPSLOTS.HANDS})
		owner.AnimState:OverrideSymbol("swap_object", "swap_san", "swap_san")
		owner.SoundEmitter:PlaySound("dontstarve/wilson/use_umbrella_up") 
		owner.DynamicShadow:SetSize(2.2, 1.4)
		--owner:AddTag("spring")
		--inst.components.weapon:SetRange(15, 15)
		inst.components.weapon:SetDamage(5)
		inst.components.waterproofer:SetEffectiveness(1)
		--owner.components.health.absorb = (0.25 * TUNING.PW)
		inst.isunfolded = true
	end
	
	inst.components.useableitem.inuse = false
end


local function onattack(inst, attacker, target)



       
end

local function common_fn(name)
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank(name)
    inst.AnimState:SetBuild(name)
    inst.AnimState:PlayAnimation("idle")  
	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "san.tex" )
	
	inst.Transform:SetScale(1.8,1.8,1.8)

    inst:AddTag("umbrella")

    --waterproofer (from waterproofer component) added to pristine state for optimization
    inst:AddTag("waterproofer")

    inst:AddComponent("waterproofer")
    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst:AddComponent("equippable")
	inst:AddComponent("useableitem")

    inst:AddComponent("insulator")
    inst.components.insulator:SetSummer()

    return inst
end

local function fn()
    local inst = common_fn("san")

    --inst.components.waterproofer:SetEffectiveness(.8)

    --inst.components.insulator:SetInsulation(TUNING.INSULATION_LARGE)
	inst.components.waterproofer:SetEffectiveness(0)	
	inst.isunfolded = false

    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
	
	inst.components.inventoryitem.imagename = "san"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/san.xml"
	
	inst:AddComponent("weapon")
	inst.components.weapon:SetDamage(5)
 inst:ListenForEvent("onattacker",onattack)
 inst.components.weapon.onattack = onattack
 

    inst.components.useableitem:SetOnUseFn(unfoldit)
    return inst
end

return Prefab("san", fn, assets)