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

function this.bind()
  return {
    toggle_itemListAlignLeft = false,
    scroll_item = L_Const.ModuleInfo.CellIconBag,
    list_cookItem = L_Const.ModuleInfo.CellIconBag,
    txt_desc = ""
  }
end

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

function this:preOpen(options)
  local groupId = options.groupId
  local itemIds = _materialTpl:getGroupItemIds(groupId)
  local dataList = {}
  for i, id in pairs(itemIds) do
    local itemData = {
      callback = function()
        self:onClickItem(id, i)
      end,
      itemType = L_Const.resType.commonItem,
      itemId = id,
      itemNum = L_BagStore:getItemNum(id)
    }
    table.insert(dataList, itemData)
  end
  self.bind.list_cookItem:clear()
  self.bind.scroll_item:clear()
  if 5 < #dataList then
    self.bind.scroll_item:insert_array(dataList)
    self.bind.toggle_itemListAlignLeft = true
  else
    self.bind.list_cookItem:insert_array(dataList)
    self.bind.toggle_itemListAlignLeft = false
  end
  self._callback = options.callback
  self:onClickItem(itemIds[options.select], options.select)
end

function this:onClickItem(id, rar)
  for i, module in ipairs(self.modules.list_cookItem) do
    module:setSel(id == module.bind.itemId)
  end
  for i, module in ipairs(self.modules.scroll_item) do
    module:setSel(id == module.bind.itemId)
  end
  self._selectedRar = rar
  local _itemTpl = L_GameTpl:getCommonItemTpl()
  local tpl = _itemTpl:getTplById(id)
  self.bind.txt_desc = _itemTpl:getSpecialDesc(tpl)
end

function this:onClickConfirm()
  if self._selectedRar then
    self._callback(self._selectedRar)
    L_UI:close("pageCookSelectMaterial")
  end
end

function this:close()
end

return this
