local this = {}
local _homeCollectionWorldTpl = L_GameTpl:getHomeCollectionWorldTpl()
local _homeBuildingCollectionTpl = L_GameTpl:getHomeBuildingCollectionTpl()

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

function this:executeInteract(entity, param)
  local paramTable = string.split(param, ",")
  local guid = tonumber(paramTable[2])
  local serverData = L_CollectionStore:getCollection(guid)
  if math.isEmpty(serverData.build_id) then
    local buildings = _homeBuildingCollectionTpl:getTplByCollectionId(guid)
    local materialList = _homeBuildingCollectionTpl:getMaterial(buildings[1])
    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)
      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:getValue("notice_interact_fixBuilding_01", {
        [0] = _homeBuildingCollectionTpl:getName(buildings[1])
      }),
      itemList = item,
      confirmCallback = function()
        if L_CollectionManager:checkCanFixCollectionBuilding(guid) then
          L_CollectionManager:reqBuildCollect(guid)
        end
      end
    })
  else
    L_UI:open("pageCollection", {entity = entity, guid = guid})
  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 noFixed = math.isEmpty(L_CollectionStore:getCollection(guid).build_id)
  if noFixed and entity then
    local workingPoint = AzurWorld.WorldMapMgr:GetWorldMapUnit(entity.worldData.worldMapId)
    local level = -1
    if workingPoint and workingPoint.NewBuildData then
      local building = _homeBuildingCollectionTpl:getTplById(workingPoint.NewBuildData.BuildId)
      level = _homeBuildingCollectionTpl:getLevel(building)
    end
    noFixed = not workingPoint or not workingPoint.NewBuildData or not (1 <= level)
  end
  local front = noFixed and L_WordsTpl:getValue("notice_interact_fixBuilding_02") or ""
  return string.concat(front, name)
end

return this
