local this = class("cellSnackBagSlot", G_UIModuleBase)
local _foodTpl = L_GameTpl:getFoodTpl()

function this.bind()
  return {
    isOn_item = false,
    img_icon = nil,
    img_outLineIcon = nil,
    txt_num = nil,
    txt_satiety = nil,
    color_bg = C_Color(0.9333333, 0.8666667, 0.7882353, 1),
    contentAnchorPos = C_Vector2(0, 0)
  }
end

function this.methods()
  return {
    onPointEnter_slot = function(self)
      if not L_DeviceTpl:getIsPc() then
        return
      end
      if not self._parentDragging then
        return
      end
      self:setSelect(true)
    end,
    onPointExit_slot = function(self)
      if not L_DeviceTpl:getIsPc() then
        return
      end
      if not self._parentDragging then
        return
      end
      self:setSelect(false)
    end,
    onClick_slot = function(self)
      L_SnackBagStore:selectBagItemUseType(self.bind.bagType)
    end
  }
end

function this:open()
  if self.isBind then
    self:initCell()
  end
end

function this:initCell()
  self.bind.contentAnchorPos = self.bind.slotPosition
  local roleType = self.bind.roleType
  local hasItem = false
  local slot = {}
  slot = L_SnackBagStore:getSnackBagSlot(self.bind.bagType, self.bind.slotId)
  hasItem = not L_SnackBagStore:getIsSnackBagSlotEmpty(self.bind.bagType, self.bind.slotId)
  self.bind.isOn_item = hasItem
  if hasItem then
    local configData = L_ItemTplManager:getCommonItem(slot.item_id)
    self.bind.img_icon = configData.icon
    self.bind.img_outLineIcon = configData.icon
    self.bind.txt_num = tostring(slot.item_num)
    local tpl_food = _foodTpl:getTplById(slot.item_id)
    local tpl_rarity = L_RarityTpl:getTplById(configData.quality)
    self.bind.txt_satiety = L_GameUtil.fillColor(string.format("+%d", _foodTpl:getSatietyScore(tpl_food)), L_RarityTpl:getColorCircle(tpl_rarity))
    if self.bind.bagType == L_Const.snackBagType.hero then
      self.bind.txt_satiety = L_GameUtil.fillColor(string.format("+%d", _foodTpl:getCharge(tpl_food)), L_RarityTpl:getColorCircle(tpl_rarity))
    end
  end
end

function this:getRaycastGo()
  if self.isBind then
    return self.bindComponents.btn_cellSnackBagSlot.gameObject
  end
end

function this:refreshCell(itemId, itemNum)
  local slot = L_SnackBagStore:getSnackBagSlot(self.bind.bagType, self.bind.slotId)
  local isEmpty = L_SnackBagStore:getIsSnackBagSlotEmpty(self.bind.bagType, self.bind.slotId)
  if not isEmpty and slot.item_id == itemId then
    itemNum = slot.item_num + itemNum
  end
  local configData = L_ItemTplManager:getCommonItem(itemId)
  self.bind.isOn_item = true
  self.bind.img_icon = configData.icon
  self.bind.img_outLineIcon = configData.icon
  self.bind.txt_num = tostring(itemNum)
  local tpl_food = _foodTpl:getTplById(itemId)
  local tpl_rarity = L_RarityTpl:getTplById(configData.quality)
  self.bind.txt_satiety = L_GameUtil.fillColor(string.format("+%d", _foodTpl:getSatietyScore(tpl_food)), L_RarityTpl:getColorCircle(tpl_rarity))
  if self.bind.bagType == L_Const.snackBagType.hero then
    self.bind.txt_satiety = L_GameUtil.fillColor(string.format("+%d", _foodTpl:getCharge(tpl_food)), L_RarityTpl:getColorCircle(tpl_rarity))
  end
end

function this:setSelect(select)
  self.bind.color_bg = select and C_Color(1, 1, 1, 1) or C_Color(0.9333333, 0.8666667, 0.7882353, 1)
end

function this:setParentDragging(dragging)
  self._parentDragging = dragging
end

function this:playFX(name)
  if not self.isBind then
    return
  end
  self.bindComponents.ani_cellMountSnackBagSlot:Stop()
  self.bindComponents.ani_cellMountSnackBagSlot:Play(name)
end

return this
