local this = class("pageAccessorySelect", G_UIPageBase)
local _materialTpl = L_GameTpl:getFoodMaterialGroupTpl()

function this.bind()
  return {
    list_item = L_Const.ModuleInfo.CellIconBag,
    module_bagTip = {
      moduleName = "pages/bag/moduleBagTip"
    },
    toggle_empty = true
  }
end

function this.methods()
  return {
    onClickClose = function()
      L_UI:close("pageAccessorySelect")
    end,
    onClickConfirm = function(self)
      self:onClickConfirm()
    end
  }
end

function this:preOpen(options)
  local id = options.id
  local guids = L_AccessoryManager:getAccessoryGuidsById(id)
  local dataList = {}
  for i = 1, 28 do
    if guids[i] then
      local itemData = {
        callback = function()
          self:onClickItem(guids[i])
        end,
        itemType = L_Const.resType.accessory,
        itemId = id,
        guid = guids[i]
      }
      table.insert(dataList, itemData)
    else
      table.insert(dataList, {})
    end
  end
  self.bind.list_item:clear()
  self.bind.list_item:insert_array(dataList)
  self._callback = options.callback
  self:onClickItem((math.isEmpty(options.select) or not options.select) and dataList[1] and dataList[1].guid)
end

function this:onClickItem(guid)
  self.bind.toggle_empty = math.isEmpty(guid)
  if self.bind.toggle_empty then
    return
  end
  for i, module in ipairs(self.modules.list_item) do
    module:setSel(guid == module.bind.guid)
  end
  self._selectGuid = guid
  self.modules.module_bagTip:setData(L_BagConst.bagTag.accessory, guid)
end

function this:onClickConfirm()
  if self._selectGuid then
    local serverData = L_AccessoryStore:getAccessory(self._selectGuid)
    if serverData.lock then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_challenge_10"))
      return
    end
    self._callback(self._selectGuid)
    L_UI:close("pageAccessorySelect")
  end
end

function this:close()
end

return this
