local this = {}
local _staminaChestDropTpl = L_GameTpl:getStaminaChestDropTpl()
local _spawnerTpl = L_GameTpl:getWorldSpawnerTpl()

function this:checkCondition(entity, param)
  local unit = AzurWorld.WorldMapMgr:GetWorldMapUnit(entity.data.worldMapId)
  if not unit then
    return false
  end
  return true
end

function this:executeInteract(entity, param)
  local objUnit = AzurWorld.WorldMapMgr:GetWorldMapUnit(entity.data.worldMapId)
  if not objUnit then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_data_update"))
    return
  end
  local spawnerId = AzurWorld.WorldMapMgr:GetDynamicObjSpawnerID(entity.data.worldMapId)
  local spawnerTpl = _spawnerTpl:getTplById(spawnerId)
  local reward = spawnerTpl.openBoxReward
  local needCost = L_DataUtil.parseRewardConfig(reward)
  local iTpl = L_GameTpl:getCommonItemTpl():getTplById(needCost[1].itemId)
  local tipStr = "\227\128\144" .. L_GameTpl:getCommonItemTpl():getName(iTpl) .. "x" .. needCost[1].itemNum .. "\227\128\145"
  if self:getIsLack(entity, param) then
    tipStr = "<color=#D9425E>\227\128\144" .. L_GameTpl:getCommonItemTpl():getName(iTpl) .. "x" .. needCost[1].itemNum .. "\227\128\145</color>"
  end
  for _, item in ipairs(needCost) do
    item.needNum = item.itemNum
    item.itemNum = L_BagStore:getItemNum(item.itemId)
  end
  local data_tip = {
    isChest = true,
    txtTips = L_Lang:get(L_WordsTpl:getValue("world_open_lock_box"), {
      [1] = tipStr
    }),
    noClose = true,
    confirmCallback = function()
      if L_BagStore:getItemNum(needCost[1].itemId) < needCost[1].needNum then
        local commonItemTpl = L_GameTpl:getCommonItemTpl()
        local itemName = commonItemTpl:getName(commonItemTpl:getTplById(needCost[1].itemId))
        L_FlyMsgManager:showNormalMsg(L_Lang:get(L_WordsTpl:getValue("world_open_lock_box_notenough"), {
          [1] = tostring(itemName)
        }))
      else
        local unit = AzurWorld.WorldMapMgr:GetWorldMapUnit(entity.data.worldMapId)
        if unit then
          AzurWorld.service.map:TryInteractObject(entity.data.worldMapId)
          L_UI:close("pageCommonTip")
        else
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_data_update"))
        end
      end
    end,
    chestItemList = needCost,
    overrideSortingOrder = 9520
  }
  L_GameUtil.showCommonTip(data_tip)
end

function this:getDisplayName(entity, param)
  return
end

function this:getIsLack(entity, param)
  local spawnerId = AzurWorld.WorldMapMgr:GetDynamicObjSpawnerID(entity.data.worldMapId)
  local spawnerTpl = _spawnerTpl:getTplById(spawnerId)
  if not spawnerTpl then
    return
  end
  local reward = spawnerTpl.openBoxReward
  local needCost = L_DataUtil.parseRewardConfig(reward)
  return L_BagStore:getItemNum(needCost[1].itemId) < needCost[1].itemNum
end

return this
