local this = class("pageMapCollection", G_UIPageBase)
local _worldCollectingTypeTpl = L_GameTpl:getWorldCollectingTypeTpl()
local _worldCollectionTpl = L_GameTpl:getWorldCollectingTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local tabIconPathList = {
  [1] = "UI/Atlas/MapCollection/tex_worldcollecting_icon_jg.png",
  [2] = "UI/Atlas/MapCollection/tex_worldcollecting_icon_yc.png",
  [3] = "UI/Atlas/MapCollection/tex_worldcollecting_icon_hh.png",
  [4] = "UI/Atlas/MapCollection/tex_worldcollecting_icon_sc.png",
  [5] = "UI/Atlas/MapCollection/tex_worldcollecting_icon_jc.png"
}

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {}
end

function this.bind()
  return {
    currentTabId = 1,
    scrollList_mapCollection = {
      moduleName = "pages/map/bigMap/cellMapCollectionItem"
    },
    tabList = {
      moduleName = "pages/tutorial/cellTutorialTab"
    },
    go_bagTip = false,
    module_bagTip = {
      moduleName = "pages/bag/moduleBagTip"
    },
    show_mapCollectionTraceTip = false,
    trace_txt = ""
  }
end

function this.methods()
  return {
    onClick_back = function(self)
      L_UI:close(self.pageName)
    end,
    scrollList_mapCollection = {
      onClick_unlock = function(self, bind)
        self:refreshMapCollection(bind)
      end
    },
    onSelectTab = function(self, tabId)
      for i = 1, #self.bind.tabList do
        self.bind.tabList:getItemCls(i):refreshSelect(i == tabId)
      end
      self.bind.currentTabId = tabId
      self:initListDataByType(tabId)
    end,
    onClick_trace = function(self)
      local canTrace, msg, otherScene = L_MapManager:checkCanTrace(self.collectionId, self.sceneId)
      if canTrace then
        L_MapStore:setMapCollectionTrace(self.collectionId)
        L_UI:close(self.pageName)
      elseif otherScene then
        local page = L_UI:getPage("pageBigMap")
        if page then
          page.cls:clearMap()
          page.cls:initMap(otherScene)
        end
        L_MapStore:setMapCollectionTrace(self.collectionId)
        L_UI:close(self.pageName)
      else
        L_FlyMsgManager:showNormalMsg(msg)
      end
    end,
    onClick_cancelMapCollectionTrace = function(self)
      local data = {
        txtTitle = "\229\143\150\230\182\136\233\135\135\233\155\134\231\137\169\232\191\189\232\184\170",
        txtContent = L_Lang:get(L_WordsTpl:getValue("ui_world_collect_cance_track"), {
          [1] = self.traceData.name
        }),
        confirmCallback = function(txt)
          self:setMapCollectionTraceTip(false)
        end
      }
      L_GameUtil.showCommonTip(data)
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self.sceneId = options and options.sceneId or 0
  self.jumpParams = options and options.jumpParams or {}
  self:initMenu()
  self.bind.currentTabId = not table.isEmpty(self.jumpParams) and tonumber(self.jumpParams[1]) or self.bind.tabList[1].tabId
  self:initListDataByType(self.bind.currentTabId)
  if L_MapStore:getCurTraceCollectionId() == nil then
    return
  end
  self:setMapCollectionTraceTip(true)
end

function this:initListDataByType(tabId)
  local allTpl = _worldCollectionTpl:getWorldCollectionByType(tabId)
  local allCollectionUnlock = L_MapStore:getMapCollectionDict()
  local datas = {}
  for i, tpl in pairs(allTpl) do
    local tplData = L_ItemTplManager:getItemConfig(_worldCollectionTpl:getItemType(tpl), _worldCollectionTpl:getItemId(tpl))
    table.insert(datas, {
      itemId = _worldCollectionTpl:getItemId(tpl),
      itemType = _worldCollectionTpl:getItemType(tpl),
      itemNumTxt = _worldCollectionTpl:getName(tpl),
      desc = tplData.desc,
      quality = tplData.quality,
      spawnerId = _worldCollectionTpl:getSpawnerId(tpl),
      collectionId = _worldCollectionTpl:getId(tpl),
      toggleUnlock = allCollectionUnlock[_worldCollectionTpl:getId(tpl)] and true or false
    })
  end
  table.sort(datas, function(a, b)
    if a.toggleUnlock == b.toggleUnlock then
      if a.quality == b.quality then
        return a.itemId < b.itemId
      else
        return a.quality > b.quality
      end
    elseif a.toggleUnlock == true then
      return true
    else
      return false
    end
  end)
  self.bind.scrollList_mapCollection:clear()
  self.bind.scrollList_mapCollection:insert_array(datas)
  if not table.isEmpty(self.jumpParams) then
    local findData = table.find(datas, function(data)
      return data.collectionId == tonumber(self.jumpParams[2])
    end)
    self:refreshMapCollection(findData or {})
  elseif 0 < #datas then
    self:refreshMapCollection(datas[1])
  end
end

function this:close(options)
  this.super.close(self, options)
end

function this:initMenu()
  local allTpl = _worldCollectingTypeTpl:getAllType()
  local datas = {}
  for i, tpl in ipairs(allTpl) do
    if _worldCollectingTypeTpl:getShow(tpl) == 1 then
      table.insert(datas, {
        id = _worldCollectingTypeTpl:getId(tpl),
        order = _worldCollectingTypeTpl:getId(tpl),
        txt_name = _worldCollectingTypeTpl:getName(tpl),
        txt_nameEn = "WORLDCOLLECTTYPE",
        img_icon = tabIconPathList[_worldCollectingTypeTpl:getId(tpl)]
      })
    end
  end
  table.sort(datas, function(a, b)
    return a.order < b.order
  end)
  datas[#datas].activeLine = false
  self.bind.tabList:clear()
  self.bind.tabList:insert_array(datas)
end

function this:refreshMapCollection(selectData)
  if self.bind.scrollList_mapCollection[1].toggleUnlock then
    self.bind.go_bagTip = true
  else
    self.bind.go_bagTip = false
  end
  for i = 1, #self.bind.scrollList_mapCollection do
    local data = self.bind.scrollList_mapCollection[i]
    if data.itemId == selectData.itemId and data.itemType == selectData.itemType then
      self.bind.scrollList_mapCollection:change(i, {select = true})
      self.collectionId = data.collectionId
      self:refreshCollectionInfo(data)
    else
      self.bind.scrollList_mapCollection:change(i, {select = false})
    end
  end
end

function this:refreshCollectionInfo(data)
  self.modules.module_bagTip:reset()
  self.modules.module_bagTip:setData(data.itemType, data.itemId)
  self.modules.module_bagTip:setMapCollectionShow(false)
  self.traceConf = L_ItemTplManager:getItemConfig(data.itemType, data.itemId)
end

function this:setMapCollectionTraceTip(bool)
  local tpl_worldCollection = _worldCollectionTpl:getTplById(L_MapStore:getCurTraceCollectionId())
  self.traceData = L_ItemTplManager:getItemConfig(_worldCollectionTpl:getItemType(tpl_worldCollection), _worldCollectionTpl:getItemId(tpl_worldCollection))
  if bool then
    self.bind.show_mapCollectionTraceTip = true
    self.bind.trace_txt = L_Lang:get(L_WordsTpl:getValue("ui_world_collecting"), {
      [1] = self.traceData.name
    }) .. ".."
  else
    self.bind.show_mapCollectionTraceTip = false
    L_FlyMsgManager:showNormalMsg(L_Lang:get(L_WordsTpl:getValue("ui_world_collect_cance_track_tips"), {
      [1] = self.traceData.name
    }))
    L_MapStore:setMapCollectionTrace(nil)
    self:initListDataByType(self.bind.currentTabId)
  end
end

return this
