local this = class("pageHomeLevelInfo", G_UIPageBase)
local _homeLevelShowTpl = L_GameTpl:getHomeLevelShowTpl()

function this.bind()
  return {
    focusIndex_levelInfo = nil,
    scrollList_levelInfo = {
      moduleName = "pages/home/homeLevel/cellHomeLevelInfo"
    },
    go_tip = false,
    module_tip = {
      moduleName = "pages/home/homeLevel/moduleHomeInfoTip"
    },
    localPosition_tip = nil,
    go_newUp = false,
    go_newBottom = false,
    localPosition_curLevel = nil,
    txt_homeName = ""
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_tipMask = function(self)
      self.bind.go_tip = false
      for i, v in pairs(self.modules.scrollList_levelInfo) do
        v:clearSelect()
      end
    end,
    onValueChange_scrollList_levelInfo = function(self, value)
      self:updateNotReadNew()
      self:updateLeftLine()
    end,
    scrollList_levelInfo = {
      onClick_select = function(self, itemBind, pos)
        self:showTip(itemBind, pos)
      end
    },
    onClick_home = function()
      local systemId = L_SystemConst.enum.renameHome
      if L_SystemBreakManager:checkBanSystemAndTip(systemId) then
        return
      end
      local data = {
        txtTitle = L_WordsTpl:getValue("notice_homeProduction_28"),
        placeHolder = L_WordsTpl:getValue("ui_modulePetBoxName_03"),
        limit = 7,
        textBoxType = L_Const.CommonTextBoxType.Rename,
        confirmCallback = function(txt)
          if string.isEmpty(string.trim(txt)) then
            L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_homeProduction_29"))
            return
          end
          if string.gsub(txt, " ", "") ~= txt then
            L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_homeProduction_30"))
            return
          end
          L_HomeStore:req_modifyHomeName(txt, function()
            L_FlyMsgManager:showNormalMsgByKey("ui_modulePetBoxName_05")
          end)
        end
      }
      L_GameUtil.showNormalTextBox(data)
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  options = options or {}
  self._defaultSelectItemType = options.defaultSelectItemType
  self._defaultSelectItemId = options.defaultSelectItemId
  self:initLevelInfoList()
  self:updateNotReadNew()
  self:updateLeftLine()
  self:refreshHomeName()
  L_HomeStore:listenCallFunc(L_HomeStore.event.refreshHomeInfo, self.refreshHomeName, self)
end

function this:close(options)
  local curLevel = L_PlayerStore:getLv()
  for i = 1, curLevel do
    L_ReddotManager:clearNew(L_ReddotManager.DotDef.LevelUnlockHomeContent, i)
  end
  L_HomeStore:unListenCallFunc(L_HomeStore.event.refreshHomeInfo, self.refreshHomeName, self)
end

function this:refreshHomeName()
  self.bind.txt_homeName = L_HomeStore:getHomeName()
end

function this:initLevelInfoList()
  local tmp = {}
  local allTpl = _homeLevelShowTpl.data
  for i, tpl in pairs(allTpl) do
    table.insert(tmp, {
      level = _homeLevelShowTpl:getId(tpl)
    })
  end
  table.sort(tmp, function(a, b)
    return a.level < b.level
  end)
  self.bind.scrollList_levelInfo:clear()
  self.bind.scrollList_levelInfo:insert_array(tmp)
  self:autoSelectItem()
end

function this:showTip(itemBind, pos)
  self.bind.go_tip = true
  local module_tip = self.modules.module_tip
  module_tip:reset()
  module_tip:setData(itemBind.type, itemBind.id)
  local targetPos = self.bindComponents.rect_Board.transform:InverseTransformPoint(pos) + L_Vector3.new(50, 20, 0)
  local height = self.bindComponents.rect_moduleHomeLevelInfoTip.sizeDelta.y
  local width = self.bindComponents.rect_moduleHomeLevelInfoTip.sizeDelta.x
  local halfScreenHeight = self.bindComponents.rect_Board.transform.rect.height / 2
  if targetPos.y - height < -halfScreenHeight then
    targetPos.y = -halfScreenHeight + height
  end
  local halfScreenWidth = self.bindComponents.rect_Board.transform.rect.width / 2
  if halfScreenWidth < targetPos.x + width then
    targetPos.x = halfScreenWidth - width
  end
  self.bind.localPosition_tip = targetPos
end

function this:updateNotReadNew()
  local haveNewBottom, haveNewUp = false, false
  local showMinLv = math.maxinteger
  local showMaxLv = math.mininteger
  for i, v in ipairs(self.modules.scrollList_levelInfo) do
    if v.isBind then
      showMinLv = math.min(showMinLv, v.bind.level)
      showMaxLv = math.max(showMaxLv, v.bind.level)
    elseif showMinLv > v.bind.level then
      if L_ReddotManager:haveNew(L_ReddotManager.DotDef.LevelUnlockHomeContent, v.bind.level) then
        haveNewUp = true
      end
    elseif showMaxLv < v.bind.level and L_ReddotManager:haveNew(L_ReddotManager.DotDef.LevelUnlockHomeContent, v.bind.level) then
      haveNewBottom = true
    end
  end
  self.bind.go_newUp = haveNewUp
  self.bind.go_newBottom = haveNewBottom
end

function this:updateLeftLine()
  local curLv = L_PlayerStore:getLv()
  for i, v in pairs(self.modules.scrollList_levelInfo) do
    if v.isBind and v.bind.level == curLv then
      local targetPos = self.bindComponents.rect_Board.transform:InverseTransformPoint(v.uiBinding.transform.position)
      self.bind.localPosition_curLevel = L_Vector3.new(2.6, targetPos.y, 0)
      return
    end
  end
  self.bind.localPosition_curLevel = L_Vector3.new(2.6, -2000, 0)
end

function this:autoSelectItem()
  if math.isEmpty(self._defaultSelectItemType) or math.isEmpty(self._defaultSelectItemId) then
    return
  end
  local itemIndex = _homeLevelShowTpl:getItemIndexByTypeId(self._defaultSelectItemType, self._defaultSelectItemId)
  if itemIndex then
    FrameScheduler.add(function()
      self.bind.focusIndex_levelInfo = itemIndex
      FrameScheduler.add(function()
        local list = self.modules.scrollList_levelInfo
        for i, v in pairs(list) do
          v:selectItem(self._defaultSelectItemType, self._defaultSelectItemId)
        end
      end, 1)
    end, 1)
  else
    error("autoSelectItem\230\156\170\230\137\190\229\136\176\229\175\185\229\186\148\230\149\176\230\141\174")
  end
end

return this
