local this = class("pageCollectionTool", G_UIPageBase)

function this.bind()
  return {
    list_item = L_Const.ModuleInfo.CellIconBag,
    active_list = true,
    txt_desc = "",
    moduleCommonTipMedium = L_Const.ModuleInfo.CommonTipMedium
  }
end

function this.methods()
  return {
    moduleCommonTipMedium = {
      onClick_close = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_cancel = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_confirm = function(self)
        local isConfirm = self.bind.active_list
        local isToHome = not self.bind.active_list and L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.home)
        if isConfirm then
          self:onClickConfirm()
        end
        if isToHome then
          self:onClickGoHome()
        end
      end,
      onClick_bg = function(self)
        L_UI:close(self.pageName)
      end
    }
  }
end

function this:preOpen(options)
  local toolType = options.toolType
  self._toolType = toolType
  local tools = L_CollectionToolStore:getToolsMap()
  local dataList = {}
  for i, tool in pairs(tools) do
    if toolType == tool.type then
      local conf = L_ItemTplManager:getItemConfig(L_Const.resType.collectionTool, tool.id)
      local itemData = {
        callback = function()
          self:onClickItem(i)
        end,
        itemType = L_Const.resType.collectionTool,
        itemId = tool.id,
        guid = i,
        quality = conf.quality,
        endurance = tool.endurance,
        isSelect = L_CollectionToolStore:getSelectTool(toolType) == i and 1 or 0
      }
      table.insert(dataList, itemData)
    end
  end
  self.bind.list_item:clear()
  if #dataList < 1 then
    self.bind.active_list = false
  else
    self.bind.active_list = true
    local data = self:sortData(dataList)
    self.bind.list_item:insert_array(data)
  end
  self:onClickItem(L_CollectionToolStore:getSelectTool(toolType))
  if not self.bind.active_list then
    self.bind.txt_desc = "\230\154\130\230\151\160\229\189\147\229\137\141\230\137\128\233\156\128\233\135\135\233\155\134\229\183\165\229\133\183"
  end
  self:initCommonTipModule()
end

function this:onClickItem(guid)
  for i, module in ipairs(self.modules.list_item) do
    module:setSel(guid == module.bind.guid)
    if guid == module.bind.guid then
      self.bind.txt_desc = L_ItemTplManager:getCollectionToolItem(module.bind.itemId).desc
    end
  end
  self._selectedGuid = guid
end

function this:onClickConfirm()
  if self._selectedGuid then
    if L_CollectionToolStore:getSelectTool(self._toolType) ~= self._selectedGuid then
      L_CollectionToolStore:req_CSProtoSelectTool(self._selectedGuid)
    end
    printf("\233\128\137\230\139\169", self._selectedGuid)
  end
  L_UI:close("pageCollectionTool")
end

function this:sortData(datalist)
  local data = datalist
  if 1 < #data then
    table.sort(data, function(a, b)
      if a.isSelect ~= b.isSelect then
        return a.isSelect > b.isSelect
      elseif a.quality ~= b.quality then
        return a.quality > b.quality
      elseif a.endurance ~= b.endurance then
        return a.endurance > b.endurance
      else
        return a.guid < b.guid
      end
    end)
  end
  return data
end

function this:onClickGoHome()
  L_HomeStore:req_enterHome()
  L_UI:close(self.pageName)
end

function this:initCommonTipModule()
  local moduleTip = self.modules.moduleCommonTipMedium
  local param = {}
  local isConfirm = self.bind.active_list
  local isToHome = not self.bind.active_list and L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.home)
  param.hideConfirm = not isConfirm and not isToHome
  param.hideCancel = not isConfirm and not isToHome
  param.txtConfirm = isConfirm and "\231\161\174\232\174\164" or "\229\137\141\229\190\128\229\174\182\229\155\173\229\136\182\228\189\156"
  param.txt_title = "\233\135\135\233\155\134\229\183\165\229\133\183"
  moduleTip:initModule(param)
end

return this
