local this = {}
local _homeCollectionWorldTpl = L_GameTpl:getHomeCollectionWorldTpl()
local _homeBuildingBuffWorldTpl = L_GameTpl:getHomeBuildingBuffWorldTpl()

function this:checkCondition(entity, param)
  return true
end

function this:executeInteract(entity, param)
  local paramTable = string.split(param, ",")
  local guid = tonumber(paramTable[2])
  if entity == nil then
    return
  end
  local worldMapId = entity.worldData.worldMapId
  local WildBuffBuilding = AzurWorld.WorldMapMgr:GetWorldMapUnit(worldMapId)
  if WildBuffBuilding == nil then
    return
  end
  if WildBuffBuilding:OnIsHadBuild() then
    local param = {worldMapId = worldMapId}
    L_UI:open("pageWildBuffBuildUp", param)
  else
    local curBuffBuildId = WildBuffBuilding:OnGetBuffBuildId()
    local tpl = _homeBuildingBuffWorldTpl:getTplById(curBuffBuildId)
    local materialList = _homeBuildingBuffWorldTpl:getMaterial(tpl)
    local isEnoughItem = true
    local item = {}
    for i, v in pairs(materialList) do
      local itemType, itemId, ItemNum = v[1], v[2], v[3]
      local hasNum = L_ItemTplManager:getItemNum(itemType, itemId)
      if ItemNum > hasNum then
        isEnoughItem = false
      end
      table.insert(item, {
        itemType = itemType,
        itemId = itemId,
        itemNumTxt = string.format("%s/%s", L_GameUtil.fillColor(hasNum, ItemNum > hasNum and L_Const.colorHtml.red001 or L_Const.colorHtml.white001), v[3])
      })
    end
    L_GameUtil.showCommonTip({
      txtTitle = L_WordsTpl:getValue("notice_interact_fixBuilding_02"),
      txtContent = L_WordsTpl:getValueParam("notice_interact_fixBuilding_01", _homeBuildingBuffWorldTpl:getName(tpl)),
      itemList = item,
      confirmCallback = function()
        if isEnoughItem then
          WildBuffBuilding:OnReqCreatBuffBuild()
        else
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getTplById("notice_common_notEnoughMaterial"))
        end
      end
    })
  end
end

function this:getDisplayName(entity, param)
  local paramTable = string.split(param, ",")
  local guid = tonumber(paramTable[2])
  local tpl = _homeCollectionWorldTpl:getTplById(guid)
  local name = _homeCollectionWorldTpl:getName(tpl)
  local worldMapId = entity.worldData.worldMapId
  local WildBuffBuilding = AzurWorld.WorldMapMgr:GetWorldMapUnit(worldMapId)
  if WildBuffBuilding == nil then
    return
  end
  local front = WildBuffBuilding:OnIsHadBuild() and name or L_WordsTpl:getValue("notice_interact_fixBuilding_02")
  return front
end

return this
