local this = class("cellIconSquare", G_UIModuleBase)
local _commonItemTpl = L_GameTpl:getCommonItemTpl()

function this.bind()
  return {
    img_qualityLine = nil,
    img_icon = nil,
    go_icon = false,
    img_spiritIcon = nil,
    go_spiritIcon = false,
    text_num = "",
    txt_level = "",
    list_star = {},
    go_lock = false,
    go_owner = false,
    img_owner = "",
    activeSelect = false,
    activeSelectNum = false,
    activeSelectNumBg = 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, spiritParams)
  params = params or {}
  self._itemType = itemType
  self._itemId = itemId
  self.bind.text_num = params.itemNum and tostring(params.itemNum) or ""
  if params.itemNumTxt then
    self.bind.text_num = params.itemNumTxt
  end
  local conf = L_ItemTplManager:getItemConfig(itemType, itemId)
  self.isSpiritron = itemType == L_Const.resType.spiritron
  self.bind.go_icon = not self.isSpiritron
  self.bind.go_spiritIcon = self.isSpiritron
  if self.isSpiritron then
    local serverData = spiritParams
    self.bind.img_spiritIcon = conf.icon
    self.bind.txt_level = string.format("LV%s", serverData.spiritronLv)
  else
    self.bind.img_icon = params.customIcon or conf.icon
  end
  self.bind.img_qualityLine = string.format("UI/Atlas/CellIcon/tex_icon_raritybsqrt_%s.png", conf.quality or 0)
  self:_setStars(conf.quality + 1)
  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
  if self.isSpiritron then
    self.bind.activeSelect = true
    self.bind.activeSelectNum = false
    self.bind.activeSelectNumBg = false
    self.bind.text_num = ""
  else
    self.bind.activeSelect = self._selectedNum > 0
    self.bind.activeSelectNum = true
    self.bind.activeSelectNumBg = true
    self.bind.textSelectNum = tostring(self._selectedNum)
  end
  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

function this:_setStars(num)
  if not self.isBind then
    return
  end
  local stars = {}
  for i = 1, num do
    table.insert(stars, {})
  end
  self.bind.list_star:clear()
  self.bind.list_star:insert_array(stars)
end

return this
