local this = class("moduleProductAccessory", G_UIModuleBase)
local _productsTpl = L_GameTpl:getProductsTpl()
local _homeBuildingProductionTpl = L_GameTpl:getHomeBuildingProductionTpl()
local _homeBuildingTpl = L_GameTpl:getHomeBuildingTpl()
local Cell = require("ui/pages/product/cellProductAccessory")

function this.bind()
  return {
    list_cellRootBg = {
      moduleName = "pages/product/cellProductAccessoryBg"
    },
    list_cellRoot = {
      moduleName = "pages/product/cellProductAccessory"
    },
    size_scrollContent = C_Vector2(0, 0),
    module_tabList = {
      moduleName = "modulePages/tabList/moduleNewCommonTabListVertical"
    },
    normalizedPosition = C_Vector2.one
  }
end

function this.methods()
  return {}
end

function this:open()
  self._productIds = {}
  L_HomeStore:listenCallFunc(L_HomeStore.event.syncWorldMapHomeItem, self.refreshBuildingInfo, self)
end

function this:close()
  L_HomeStore:unListenCallFunc(L_HomeStore.event.syncWorldMapHomeItem, self.refreshBuildingInfo, self)
end

function this:initModule(guid, selectTabId)
  self._buildingGuid = guid
  local buildingInfo = L_HomeStore:getHomeBuildingByGuid(self._buildingGuid)
  local buildProductionTpl = _homeBuildingProductionTpl:getTplById(buildingInfo.build_id)
  local tabs = _homeBuildingProductionTpl:getTab(buildProductionTpl)
  self.modules.module_tabList:setData(tabs, handler(self, self.onSelectTabId))
  self.modules.module_tabList:setSelectTabId(selectTabId or tabs[1])
  self:refreshBuildingInfo()
end

function this:refreshBuildingInfo()
  local buildingInfo = L_HomeStore:getHomeBuildingByGuid(self._buildingGuid)
  if not buildingInfo then
    return
  end
  local tpl = _homeBuildingTpl:getTplById(buildingInfo.build_id)
  local groupId = _homeBuildingTpl:getGroupId(tpl)
  local homeBuildingGroupTpl = L_GameTpl:getHomeBuildingGroupTpl()
  local tpl_buildingGroup = homeBuildingGroupTpl:getTplById(groupId)
  self.bind.txt_buildingName = homeBuildingGroupTpl:getName(tpl_buildingGroup)
  local levelStr = string.concat("Lv.", _homeBuildingTpl:getLevel(tpl))
  self.bind.txt_buildingLv = levelStr
  self.bind.toggle_isMax = math.isEmpty(_homeBuildingTpl:getNextLevel(tpl))
  self.bind.active_canUpgrade = L_HomeUtil.canBuildUpgrade(self._buildingGuid)
end

function this:onSelectTabId(tab)
  self.parent:refreshInfo(false)
  self.bind.normalizedPosition = C_Vector2.one
  local buildingInfo = L_HomeStore:getHomeBuildingByGuid(self._buildingGuid)
  local buildProductionTpl = _homeBuildingProductionTpl:getTplById(buildingInfo.build_id)
  local products = _productsTpl:getTplsByGroup(_homeBuildingProductionTpl:getGroupId(buildProductionTpl))
  self._productIds = {}
  for _, tpl in ipairs(products) do
    if _productsTpl:getTabId(tpl) == tab or _productsTpl:getTabId(tpl) == 0 then
      table.insert(self._productIds, tpl.id)
    end
  end
  self:refreshMainView()
  local offset = C_Vector2.zero
  self.bindComponents.ScrollContent.anchoredPosition = offset
end

function this:refreshMainView()
  local mapTreeList, mapIdList, maxRow, maxCol = _productsTpl:generateWeaponStationMapByIds(self._productIds)
  local dataList = {}
  for row = 1, maxRow do
    for col = 1, maxCol do
      local item
      if mapTreeList[row] ~= nil and mapTreeList[row][col] ~= nil then
        local mapData = mapTreeList[row][col]
        item = {
          curRow = row,
          curCol = col,
          curProductId = mapData.curProductId,
          lastRow = 0,
          lastCol = 0,
          lastProductId = mapData.lastProductId,
          nextRow = 0,
          nextCol = 0,
          nextProductId = mapData.nextProductId
        }
        if 0 < item.lastProductId and mapIdList[item.lastProductId] ~= nil then
          local lastData = mapIdList[item.lastProductId]
          item.lastRow = lastData.curRow
          item.lastCol = lastData.curCol
        end
        if 0 < item.nextProductId and mapIdList[item.nextProductId] ~= nil then
          local nextData = mapIdList[item.nextProductId]
          item.nextRow = nextData.curRow
          item.nextCol = nextData.curCol
        end
      else
      end
      if item ~= nil then
        if 0 < item.lastProductId and mapIdList[item.lastProductId] == nil then
          item.lastProductId = 0
          item.lastRow = 0
          item.lastCol = 0
        end
        
        function item.callback(clickId, pos)
          self:onSelectProduct(clickId, pos)
          self.parent:refreshInfo(true)
        end
        
        item.guid = self._buildingGuid
        table.insert(dataList, item)
      end
    end
  end
  local widthContent = maxCol * (Cell.CellWidth + Cell.CellSpaceWidth) + Cell.CellColOffset * 2 - Cell.CellSpaceWidth
  local HeightContent = maxRow * (Cell.CellHeight + Cell.CellSpaceHeight)
  self.bind.size_scrollContent = C_Vector2(widthContent, HeightContent)
  self.bind.list_cellRoot:clear()
  self.bind.list_cellRoot:insert_array(dataList)
  local tmp = {}
  local hasCell = 0 >= #self.bind.list_cellRoot
  for i = 1, maxRow do
    table.insert(tmp, {
      index = i,
      activeDec = hasCell,
      width = widthContent
    })
  end
  self.bind.list_cellRootBg:clear()
  self.bind.list_cellRootBg:insert_array(tmp)
  Timer.once(0.2, function()
    if maxRow < 6 then
      self.bindComponents.ScrollView.vertical = false
    else
      self.bindComponents.ScrollView.vertical = true
    end
    if maxCol < 4 then
      self.bindComponents.ScrollView.horizontal = false
    else
      self.bindComponents.ScrollView.horizontal = true
    end
  end)
end

function this:onSelectProduct(id, pos)
  for _, module in ipairs(self.modules.list_cellRoot) do
    module:refreshSelect(id)
  end
  self.parent:onSelectId(id, pos)
end

return this
