local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local _homeBuildingGroupTpl = L_GameTpl:getHomeBuildingGroupTpl()
local _homeBuildingRemoteUpTpl = L_GameTpl:getHomeBuildingRemoteUpTpl()
local _homeCollectionWorldTpl = L_GameTpl:getHomeCollectionWorldTpl()
local this = class("moduleHomeBuildingTop", G_UIModuleBase)

function this.bind()
  return {
    txt_name = nil,
    txt_level = nil,
    go_working = false,
    go_upgrade = false,
    text_maxLevelActive = false,
    txt_levelActive = true,
    active_canUpgrade = false
  }
end

function this.methods()
  return {
    onClickUpgrade = function(self)
      if not math.isEmpty(self._souceBuildId) then
        local data = L_HomeStore:getHomeBuildingByGuid(self._souceBuildId)
        local tpl = _homeBuildingRemoteUpTpl:getTplById(data.build_id)
        if tpl then
          local levelLockMap = _homeBuildingRemoteUpTpl:getRemoteUpMap(tpl)
          local collectionWorldTpl = _homeCollectionWorldTpl:getTplById(self._collectionId)
          local type = _homeCollectionWorldTpl:getCollectionType(collectionWorldTpl)
          if levelLockMap[type] then
            L_UI:open("pageManagerUpgrade", {
              guid = self._guid,
              collectionId = self._collectionId,
              sourceBuildId = self._souceBuildId
            })
          else
            for i, v in ipairs(_homeBuildingRemoteUpTpl:getSortData()) do
              local map = _homeBuildingRemoteUpTpl:getRemoteUpMap(v)
              if map[type] then
                return L_FlyMsgManager:showNormalMsgByKey("ui_moduleHomeBagCropInfo_05", {
                  [0] = _homeBuildingRemoteUpTpl:getGroupId(v)
                })
              end
            end
            L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_homeProduction_33"))
          end
        end
      else
        L_UI:open("pageBuildingUpgrade", {
          guid = self._guid,
          collectionId = self._collectionId
        })
      end
    end
  }
end

function this:open(options)
  L_HomeStore:listenCallFunc(L_HomeStore.event.syncWorldMapHomeItem, self.refreshAll, self)
end

function this:close(options)
  L_HomeStore:unListenCallFunc(L_HomeStore.event.syncWorldMapHomeItem, self.refreshAll, self)
  self.super.close(self, options)
end

function this:setGuid(guid)
  self._guid = guid
  self:refreshAll()
end

function this:refreshAll()
  local guid = self._guid
  if not guid then
    return
  end
  local serverData = L_HomeStore:getHomeBuildingByGuid(guid)
  local buildingId = serverData.build_id
  local tpl = _homeBuildingTpl:getTplById(buildingId)
  local groupId = _homeBuildingTpl:getGroupId(tpl)
  local tpl_buildingGroup = _homeBuildingGroupTpl:getTplById(groupId)
  self.bind.txt_name = _homeBuildingGroupTpl:getName(tpl_buildingGroup)
  self.bind.txt_level = string.concat("Lv.", _homeBuildingTpl:getLevel(tpl))
  if buildingId == 10000 then
    self.bind.txt_level = "   "
  end
  local isMax = math.isEmpty(_homeBuildingTpl:getNextLevel(tpl))
  local isWorking = L_ProductManager:isBuildingOnProducing(guid)
  self.bind.go_working = isWorking
  self.bind.go_upgrade = not isMax and not isWorking
  self.bind.text_maxLevelActive = isMax
  self.bind.txt_levelActive = not isMax
end

function this:setCollectionId(collectionId, souceBuildId)
  self._collectionId = collectionId
  self._souceBuildId = souceBuildId or 0
  self:setCollectionData()
end

function this:setCollectionData()
  local guid = self._collectionId
  if not guid then
    return
  end
  local serverData = L_CollectionStore:getCollection(guid)
  local homeBuildingCollectionTpl = L_GameTpl:getHomeBuildingCollectionTpl()
  local tpl_building = homeBuildingCollectionTpl:getTplById(serverData.build_id)
  self.bind.txt_name = homeBuildingCollectionTpl:getName(tpl_building)
  self.bind.txt_level = string.concat("Lv.", homeBuildingCollectionTpl:getLevel(tpl_building))
  local isMax = L_CollectionManager:getIsCollectionMaxLevel(guid)
  local isWorking = not math.isEmpty(serverData.start_time)
  self.bind.go_working = isWorking
  self.bind.go_upgrade = not isMax and not isWorking
  self.bind.text_maxLevelActive = isMax
  self.bind.txt_levelActive = not isMax
  self.bind.active_canUpgrade = L_HomeUtil.canCollectUpgrade(guid)
end

return this
