local this = class("cellIconCircle", G_UIModuleBase)
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local _, DEFAULT_COLOR = C_ColorUtility.TryParseHtmlString(L_RarityTpl:getColorCircle(L_RarityTpl:getTplById(0)))

function this.bind()
  return {
    color_bg = DEFAULT_COLOR,
    go_item = false,
    img_item = nil,
    go_pet = false,
    img_pet = nil,
    text_num = "",
    activeSelect = false,
    activeSelectNum = false,
    textSelectNum = "",
    activeReduce = true
  }
end

function this.methods()
  return {
    onClick = function(self)
      if self._selectMode then
        if not self.bind.mutiSelect then
          self:setSelectNum(1 - self._selectedNum)
        else
          self:setSelectNum(self._selectedNum + 1)
        end
      end
      if self._callback then
        self._callback(self)
      end
      if not self._selectMode and not self._callback and not self._noTip then
        L_ItemTplManager:showInfoTip(self._itemType, self._itemId, self.bind.guid)
      end
    end,
    onClickReduce = function(self)
      self:setSelectNum(self._selectedNum - 1)
    end,
    onLongPress = function(self)
      if (self._callback or self._selectMode) and not self._noTip then
        L_ItemTplManager:showInfoTip(self._itemType, self._itemId, self.bind.guid)
      end
    end
  }
end

function this:open()
  this.super.open(self)
  self:refreshView()
end

function this:refreshView()
  if not self.isBind then
    return
  end
  local itemType = self.bind.itemType
  if not itemType then
    return
  end
  self.bind.itemNum = self.bind.itemNum or 1
  if not math.isEmpty(self.bind.guid) then
    self:setDataContent(itemType, self.bind.guid, self.bind)
  else
    self:setGeneralContent(itemType, self.bind.itemId, self.bind)
  end
  if self._selectMode then
    self._selectedNum = 0
  end
end

function this:close()
  L_PhotoManager:clearPhotoEntity(self)
end

function this:setGeneralContent(itemType, itemId, params)
  params = params or {}
  self._itemType = itemType
  self._itemId = itemId
  local conf = L_ItemTplManager:getItemConfig(itemType, itemId)
  self.bind.go_item = false
  self.bind.go_pet = false
  if itemType == L_Const.resType.pet then
    self.bind.go_pet = true
    self.bind.img_pet = conf.icon
  else
    self.bind.go_item = true
    self.bind.img_item = conf.icon
  end
  self.bind.text_num = params.itemNum and tostring(params.itemNum) or ""
  if params.itemNumTxt then
    self.bind.text_num = params.itemNumTxt
  end
  local color = DEFAULT_COLOR
  if conf.quality then
    _, color = C_ColorUtility.TryParseHtmlString(L_RarityTpl:getColorCircle(L_RarityTpl:getTplById(conf.quality)))
  end
  self.bind.color_bg = color
  self._callback = params.callback
  self._noTip = params.noTip
  self._selectedNum = params.selectNum or 0
  self.bind.activeReduce = params.mutiSelect == true
  self._selectCallback = params.selectCallback
  self._selectMode = params.selectMode or params.selectCallback ~= nil
  self._validateSelectFunc = params.validateSelectFunc
  if params.mutiSelect then
    self.bind.mutiSelect = params.mutiSelect
    self:updateSelectView(true)
  end
  if itemType == L_Const.resType.hero then
    if self._guid == L_HeroStore:getDefaultHeroGuid() then
      L_PhotoManager:newOrBindPhotoEntity(self, self.bindComponents.img_icon, L_PlayerStore:getPlayerModHeadImgName())
    else
      L_PhotoManager:clearPhotoEntity(self)
    end
  end
end

function this:updateSelectView(notCall)
  if not self.bind.mutiSelect then
    self.bind.activeSelectNum = false
    self._selectedNum = math.min(self._selectedNum, 1)
  else
    self.bind.activeSelectNum = true
    self._selectedNum = math.min(self._selectedNum, self.bind.itemNum or self._selectedNum)
  end
  self.bind.activeSelect = self._selectedNum > 0
  self.bind.textSelectNum = tostring(self._selectedNum)
  if self._selectCallback and not notCall then
    self._selectCallback(self)
  end
end

function this:setSelectState(state)
  self._selectedNum = state and 1 or 0
  self:updateSelectView()
end

function this:getSelectState()
  return self._selectedNum and self._selectedNum > 0
end

function this:setSelectNum(num, notCall)
  if self._validateSelectFunc and not self._validateSelectFunc(self._selectedNum, num) then
    return
  end
  self._selectedNum = num
  self:updateSelectView(notCall)
end

function this:getSelectNum()
  return self._selectedNum or 0
end

function this:setDisable(state)
  self.bind.activeDisable = state
end

local GuidType2Func = {
  [L_Const.resType.commonItem] = "setCommonItemDataContent",
  [L_Const.resType.hero] = "setHeroDataContent",
  [L_Const.resType.spiritron] = "setSpiritronDataContent",
  [L_Const.resType.accessory] = "setJewelDataContent",
  [L_Const.resType.pet] = "setPetDataContent",
  [L_Const.resType.mount] = "setMountDataContent"
}

function this:setDataContent(itemType, guid, params)
  if self[GuidType2Func[itemType]] then
    self[GuidType2Func[itemType]](self, guid, params)
  else
    errorf("\231\177\187\229\158\139" .. itemType .. "\228\184\141\230\148\175\230\140\129\230\149\176\230\141\174\230\152\190\231\164\186 \232\175\183\228\184\141\232\166\129\228\188\160\229\133\165guid")
  end
end

function this:setMountDataContent(guid, params)
end

function this:setCommonItemDataContent(guid, params)
  self._guid = guid
  local itemData = L_BagStore:getItem(guid)
  if itemData then
    params.itemNum = itemData.itemNum
    self:setGeneralContent(L_Const.resType.commonItem, guid, params)
  end
end

function this:setHeroDataContent(guid, params)
end

function this:setSpiritronDataContent(guid, params)
end

function this:setJewelDataContent(guid, params)
end

function this:setPetDataContent(guid, params)
end

return this
