local assets=
{
	Asset("ANIM", "anim/venus_liang.zip"),
	Asset("ANIM", "anim/swap_venus_liang.zip"),
    Asset("SOUND", "sound/wilson.fsb"),
    --Asset("INV_IMAGE", "lantern_lit"),
}

local function turnon(inst)
    if not inst.components.fueled:IsEmpty() then
        if not inst.components.machine.ison then
            if inst.components.fueled then
                inst.components.fueled:StartConsuming()        
            end
            inst.Light:Enable(true)
			if IsDLCEnabled(CAPY_DLC) then
            inst.components.floatable:UpdateAnimations("idle_on_water", "idle_on")
			else
			inst.AnimState:PlayAnimation("idle_on")
			end
            if inst.components.equippable:IsEquipped() then
                inst.components.inventoryitem.owner.AnimState:OverrideSymbol("swap_object", "swap_venus_liang", "swap_venus_liang")
                inst.components.inventoryitem.owner.AnimState:Show("LANTERN_OVERLAY") 
                
            end
            inst.components.machine.ison = true

            inst.SoundEmitter:PlaySound("dontstarve/wilson/lantern_on")
            inst.SoundEmitter:PlaySound("dontstarve/wilson/lantern_LP", "loop")
			inst:AddTag("cooker")
			inst:AddTag("HASHEATER")
			if not inst.components.cooker ~= nil then
			inst:AddComponent("cooker")
			end
			if not inst.components.heater ~= nil then
			inst:AddComponent("heater")
			inst.components.heater.heat = 75
			end	
        end
    end
end

local function turnoff(inst)
    if inst.components.fueled then
        inst.components.fueled:StopConsuming()        
    end
    inst.Light:Enable(false)
	if IsDLCEnabled(CAPY_DLC) then
    inst.components.floatable:UpdateAnimations("idle_off_water", "idle_off")
	else
	inst.AnimState:PlayAnimation("idle_off")
	end
    if inst.components.equippable:IsEquipped() then
        inst.components.inventoryitem.owner.AnimState:OverrideSymbol("swap_object", "swap_venus_liang", "swap_venus_liang")
        inst.components.inventoryitem.owner.AnimState:Hide("LANTERN_OVERLAY") 
    end

    inst.components.machine.ison = false

    inst.SoundEmitter:KillSound("loop")
    inst.SoundEmitter:PlaySound("dontstarve/wilson/lantern_off")

    inst:RemoveTag("cooker")
    inst:RemoveTag("HASHEATER")
	if inst.components.cooker ~= nil then
	inst:RemoveComponent("cooker")
	end
	if inst.components.heater ~= nil then
	inst:RemoveComponent("heater")
	end
end

--local function OnLoad(inst, data)
--    if inst.components.machine and inst.components.machine.ison then
--        turnon(inst)
--    else
--        turnoff(inst)
--    end
--end


local function ondropped(inst)
    turnoff(inst)
    turnon(inst)
end

local function onpickup(inst)
	turnon(inst)
end

local function onputininventory(inst)
    turnoff(inst)
end
--
local function onequip(inst, owner) 
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal")
    owner.AnimState:OverrideSymbol("lantern_overlay", "swap_venus_liang", "venus_liang_overlay")
	
    if inst.components.fueled:IsEmpty() then
        owner.AnimState:OverrideSymbol("swap_object", "swap_venus_liang", "swap_venus_liang")
		owner.AnimState:Hide("LANTERN_OVERLAY") 
    else
        owner.AnimState:OverrideSymbol("swap_object", "swap_venus_liang", "swap_venus_liang")
		owner.AnimState:Show("LANTERN_OVERLAY") 
    end
    turnon(inst)
end

local function onunequip(inst, owner) 
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal")
    owner.AnimState:ClearOverrideSymbol("lantern_overlay")
	owner.AnimState:Hide("LANTERN_OVERLAY") 	
end

local function nofuel(inst)
    local owner = inst.components.inventoryitem and inst.components.inventoryitem.owner
    if owner then
        owner:PushEvent("torchranout", {torch = inst})
    end

    turnoff(inst)
end

local function takefuel(inst)
    if inst.components.equippable and inst.components.equippable:IsEquipped() then
        turnon(inst)
    end
end

local function fuelupdate(inst)
    local fuelpercent = inst.components.fueled:GetPercent()
    inst.Light:SetIntensity(Lerp(0.4, 0.6, fuelpercent))
    inst.Light:SetRadius(Lerp(3, 5, fuelpercent))
    inst.Light:SetFalloff(.9)
end

local function OnLoad(inst, data)
    if inst.components.fueled:IsEmpty() then
        nofuel(inst)
    end
end

local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()        
    MakeInventoryPhysics(inst)
    
    inst.AnimState:SetBank("venus_liang")
    inst.AnimState:SetBuild("venus_liang")
    inst.AnimState:PlayAnimation("idle_off")
	if IsDLCEnabled(CAPY_DLC) then	
    MakeInventoryFloatable(inst, "idle_off_water", "idle_off")
	end
    inst:AddTag("light")

    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/venus_liang.xml"
    inst.components.inventoryitem:SetOnDroppedFn(ondropped)
    inst.components.inventoryitem:SetOnPutInInventoryFn(onputininventory)    

    inst:AddComponent("equippable")
	
	inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(10)
    
	inst:AddComponent("fueled")

    inst:AddComponent("machine")
    inst.components.machine.turnonfn = turnon
    inst.components.machine.turnofffn = turnoff
    inst.components.machine.cooldowntime = 0
    inst.components.machine.caninteractfn = function() return not inst.components.fueled:IsEmpty() and (inst.components.inventoryitem.owner == nil or inst.components.equippable.isequipped) end


    --inst.components.fueled.fueltype = "CAVE"
    inst.components.fueled:InitializeFuelLevel(TUNING.LANTERN_LIGHTTIME*2)
    inst.components.fueled:SetDepletedFn(nofuel)
    inst.components.fueled:SetUpdateFn(fuelupdate)
    inst.components.fueled.ontakefuelfn = takefuel
    inst.components.fueled.accepting = true

    inst.entity:AddLight()
	inst.Light:SetColour(255 / 255, 255 / 255, 192 / 255)

    fuelupdate(inst)

    inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip ) 

    inst:ListenForEvent( "startrowing", function(inst,data) 
        turnoff(inst)
        end, inst)  
    inst:ListenForEvent( "stoprowing", function(inst, data) 
        turnon(inst)
        end, inst)
    inst.OnLoad = OnLoad
    return inst
end


return Prefab( "common/inventory/venus_liang", fn, assets) 
