require "prefabutil"
local prefabs = {"collapse_small", }
local assets = {  Asset("ANIM", "anim/diningtable.zip"),
                  Asset("SOUND", "sound/sfx.fsb"),
                }

local function onhammered(inst)
  inst.components.dining:DropFood()
  local fx = SpawnPrefab("collapse_small")
  fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
  fx:SetMaterial("wood")
  inst:Remove()
end

local function onhit(inst, worker, workleft)
  inst.components.dining:DropFood()
  if workleft > 0 and not inst:HasTag("burnt") then
    inst.SoundEmitter:PlaySound("dontstarve/creatures/together/stagehand/hit")
    inst.AnimState:PlayAnimation("hit")
    inst.AnimState:PushAnimation("idle")
  end
end

local function onbuilt(inst)
  inst.AnimState:PlayAnimation("place")
  inst.AnimState:PushAnimation("idle", false)
  inst.SoundEmitter:PlaySound("dontstarve/creatures/together/stagehand/hit")
end

local function ongetitem(inst, giver, item)
  local wilttime = item.components.perishable ~= nil and item.components.perishable:GetPercent() * TUNING.ENDTABLE_FLOWER_WILTTIME or TUNING.ENDTABLE_FLOWER_WILTTIME
  GiveFlower(inst, GetRandomItem(FLOWER_MAP[item.prefab].foodnames), wilttime, giver)
  inst.SoundEmitter:PlaySound("dontstarve/creatures/together/stagehand/hit")
  inst.AnimState:PlayAnimation("hit")     inst.AnimState:PushAnimation("idle")
end

local function onignite(inst)
  if inst.components.trader ~= nil then
    inst.components.trader:Disable()
  end
  DefaultBurnFn(inst)
end

local function onextinguish(inst)
  if inst.components.trader ~= nil then
    inst.components.trader:Enable()
  end
  DefaultExtinguishFn(inst)
end

local function onburnt(inst)
  if inst.components.trader ~= nil then
    inst:RemoveComponent("trader")
  end
  inst.components.dining:DropFood()
  DefaultBurntStructureFn(inst)
end

local function onsave(inst, data)
  if inst.components.burnable ~= nil and inst.components.burnable:IsBurning() or inst:HasTag("burnt") then
    data.burnt = true
  elseif inst.foodname ~= nil then
    data.foodname = inst.foodname
    data.foodnum = inst.foodnum
  end
end

local function onload(inst, data)
  if data ~= nil then
    if data.burnt then
      inst.components.burnable.onburnt(inst)
    end
    if data.foodname ~= nil then
      inst.foodname = data.foodname
      inst.foodnum = data.foodnum
      local product = inst.foodname
      inst.AnimState:ShowSymbol("swap_flower")
      if IsModCookingProduct("cookpot", product) then
        inst.AnimState:OverrideSymbol("swap_flower", product, product)
      else
        inst.AnimState:OverrideSymbol("swap_flower", "cook_pot_food", product)
      end
    end
  end
end

local function abletoaccepttest(inst, item)
  if not item:HasTag("preparedfood") or not item:HasTag("fresh") then
    return false, "WRONGTYPE"
  elseif inst.foodname and inst.foodname ~= item.prefab then
    return false, "SLOTFULL"
  end
  return true
end

local function ongivenitem(inst, giver, item)
  local count = 1
  if item.components.stackable then
    count = item.components.stackable.stacksize
  end
  if inst.foodname == nil then
    inst.foodname = item.prefab
    inst.foodnum = count
    local product = inst.foodname
    inst.AnimState:ShowSymbol("swap_flower")
    if IsModCookingProduct("cookpot", product) then
      inst.AnimState:OverrideSymbol("swap_flower", product, product)
    else
      inst.AnimState:OverrideSymbol("swap_flower", "cook_pot_food", product)
    end
  else
    inst.foodnum = inst.foodnum + count
  end
end

local function getDescription(inst,viewer)
  local desc = STRINGS.NAMES.DININGTABLE..":"
  if inst.foodname then
    desc = desc..inst.foodnum..STRINGS.NAMES[string.upper(inst.foodname)]
  else
    desc = desc..STRINGS.DININGTABLE_NONE
  end
  return desc
end

local function fn()
  local inst = CreateEntity()
  inst.entity:AddTransform()
  inst.entity:AddAnimState()
  inst.entity:AddLight()
  inst.entity:AddSoundEmitter()
  inst.entity:AddNetwork()
  MakeObstaclePhysics(inst, .3)
  inst:AddTag("structure")
  inst:AddTag("trader")
  inst:AddTag("stacktrader")
  inst:AddTag("diningtable")
  inst.AnimState:SetBank("diningtable")
  inst.AnimState:SetBuild("diningtable")
  inst.AnimState:PlayAnimation("idle")
  inst.AnimState:HideSymbol("swap_flower")
  inst.AnimState:HideSymbol("swap_vase")
  MakeSnowCoveredPristine(inst)
  inst.entity:SetPristine()
  if not TheWorld.ismastersim then
    return inst
  end
  inst:AddComponent("dining")
  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("trader")
  inst.components.trader:SetAbleToAcceptTest(abletoaccepttest)
  inst.components.trader.onaccept = ongivenitem
  inst:AddComponent("inspectable")
  inst.components.inspectable.descriptionfn = getDescription
  MakeSmallBurnable(inst, nil, nil, true)
  inst.components.burnable:SetOnIgniteFn(onignite)
  inst.components.burnable:SetOnExtinguishFn(onextinguish)
  inst.components.burnable:SetOnBurntFn(onburnt)
  MakeSmallPropagator(inst)
  MakeHauntableWork(inst)
  MakeSnowCovered(inst)
  inst:ListenForEvent("onbuilt", onbuilt)
  inst.OnSave = onsave
  inst.OnLoad = onload      
  return inst
end
return Prefab("diningtable", fn, assets, prefabs),MakePlacer("diningtable_placer", "diningtable", "diningtable", "idle", nil, nil, nil, nil, nil, nil,function(inst) inst.AnimState:HideSymbol("swap_flower") inst.AnimState:HideSymbol("swap_vase") end)
