local this = class("cellIcon", G_UIModuleBase)
local _commonItemTpl = L_GameTpl:getCommonItemTpl()
local _foodTpl = L_GameTpl:getFoodTpl()
local _accessoryTpl = L_GameTpl:getAccessoryTpl()
local GuidType2Func = {
  [L_Const.resType.commonItem] = "setCommonItemDataContent",
  [L_Const.resType.spiritron] = "setSpiritronDataContent",
  [L_Const.resType.accessory] = "setAccessoryDataContent",
  [L_Const.resType.taskItem] = "setTaskItemDataContent",
  [L_Const.resType.petEgg] = "setPetEggDataContent",
  [L_Const.resType.collectionTool] = "setCollectionToolDataContent"
}

function this.bind()
  return {
    active_content = false,
    active_empty = false,
    img_qualityBg = "",
    img_qualityLine = "",
    active_qualityLine = false,
    active_icon = false,
    img_icon = "",
    img_iconFilter = "",
    go_spiritronIcon = false,
    img_spiritronIcon = "",
    active_rectPet = false,
    img_petIcon = "",
    active_index = false,
    txt_index = "",
    active_foodBuff = false,
    img_foodBuff = "",
    txt_level = "",
    txt_num = "",
    list_star = {},
    active_sel = false,
    active_canobtain = false,
    active_hasobtain = false,
    active_forbidden = false,
    active_lock = false,
    active_limit = false,
    active_owner = false,
    img_owner = "",
    active_rarity = false,
    img_rarity = "",
    active_remain = false,
    txt_remian = "",
    list_spiritronStar = {},
    active_light = false
  }
end

function this.methods()
  return {
    onClick = function(self)
      if self.bind.callback then
        self.bind.callback(self)
      end
      if not self.bind.callback and not self.bind.noTip then
        L_ItemTplManager:showInfoTip(self._itemType, self._itemId, self._guid)
      end
    end,
    onLongClick = function(self)
      if self.bind.longPressCallback then
        self.bind.longPressCallback(self)
      end
      if not self.bind.longPressCallback and not self.bind.noTip then
        L_ItemTplManager:showInfoTip(self._itemType, self._itemId, self._guid)
      end
    end
  }
end

function this:open()
  self:refreshView()
  if self._warningReddotKey and not self.bind.noReddot then
    L_ReddotManager:registerReddot(self.bindComponents.rect_reddot, string.format(self._warningReddotKey, self._warningReddotId))
  else
    self.bindComponents.rect_reddot.gameObject:SetActive(false)
  end
  if self._newReddotKey then
    L_ReddotManager:registerReddot(self.bindComponents.reddotNew, string.format(self._newReddotKey, self._newReddotId))
  else
    self.bindComponents.reddotNew.gameObject:SetActive(false)
  end
  L_AccessoryStore:listenCallFunc(L_AccessoryStore.event.refreshAccessory, self.OnAccessoryChanged, self, nil, 0)
end

function this:close()
  L_AccessoryStore:unListenCallFunc(L_AccessoryStore.event.refreshAccessory, self.refreshView, self)
  L_PhotoManager:clearPhotoEntity(self)
end

function this:refreshView()
  if not self.isBind then
    return
  end
  self._itemType = self._itemType or self.bind.itemType
  self._itemId = self._itemId or self.bind.itemId
  self._guid = self._guid or self.bind.guid
  if not self._itemType then
    self:setContentActive(false)
    return
  end
  self:setDataContent(self._itemType, self._guid, self._itemId, self.bind)
  if self.bind.active_sel then
    self:clearNew()
  end
  self:setReceiveActive(self.bind.isReceived)
end

function this:setDataContent(itemType, guid, itemId, params)
  if not math.isEmpty(guid) and self[GuidType2Func[itemType]] then
    self._guid = guid
    self._itemId = itemId
    self._itemType = itemType
    self:setContentActive(true)
    self[GuidType2Func[itemType]](self, guid, params)
  else
    self:setGeneralContent(itemType, itemId, params)
  end
end

function this:setGeneralContent(itemType, itemId, params)
  params = params or {}
  self._itemType = itemType
  self._itemId = itemId
  self.bind.index = params.index
  self:setContentActive(self._itemId ~= nil)
  if self._itemId == nil then
    return
  end
  self.bind.txt_num = not params.itemNumTxt and params.itemNum and L_GameUtil.itemShowNum(params.itemNum)
  local conf = L_ItemTplManager:getItemConfig(itemType, itemId)
  if params.bShowName then
    self.bind.txt_num = conf.name
  end
  self:SetMainIcon(itemType, conf, params)
  local quality = conf.quality or params.quality or 0
  self.bind.img_qualityBg = string.format("UI/Atlas/CellIcon/tex_icon_raritybsqrtd_%s.png", quality)
  self:setQualityLineActive(true)
  self.bind.img_qualityLine = string.format("UI/Atlas/CellIcon/tex_icon_raritybsqrt_%s.png", quality)
  if self._itemType ~= L_Const.resType.spiritron and self._itemType ~= L_Const.resType.warehouse then
    self:setStars(conf.quality + 1)
  end
  self.bind.callback = params.callback
  self.bind.longPressCallback = params.longPressCallback
  self.bind.noTip = params.noTip
end

function this:SetMainIcon(itemType, conf, params)
  self.bind.active_icon = false
  self.bind.go_spiritronIcon = false
  self.bind.active_rectPet = false
  if itemType == L_Const.resType.spiritron then
    self.bind.go_spiritronIcon = true
    self.bind.img_spiritronIcon = conf.icon
  elseif itemType == L_Const.resType.pet then
    self.bind.active_rectPet = true
    self.bind.img_petIcon = conf.icon
  else
    self.bind.active_icon = true
    if itemType == L_Const.resType.hero then
      self.bind.img_icon = conf.icon_mid
    else
      self.bind.img_icon = conf.icon or params.icon
    end
  end
  if itemType == L_Const.resType.commonItem or itemType == L_Const.resType.warehouse then
    local commonItemTpl = _commonItemTpl:getTplById(self._itemId)
    self.bind.img_iconFilter = commonItemTpl and _commonItemTpl:getFilterIcon(commonItemTpl) or ""
  end
  if self.bindComponents then
    if params.enlarge and itemType == L_Const.resType.hero then
      self.bindComponents.img_icon.transform.localScale = L_Vector3.new(1.5, 1.5, 1.5)
      if self.bindComponents.mask then
        self.bindComponents.mask.enabled = true
      end
    elseif self.bindComponents.mask then
      self.bindComponents.mask.enabled = false
    end
  end
  if params.blight then
    self.bind.active_light = true
  else
    self.bind.active_light = false
  end
end

function this:setCommonItemDataContent(guid, params)
  local itemData = L_BagStore:getItem(guid)
  if itemData then
    if params.isStackMode then
      params.itemNum = L_BagManager:getItemNumByStackIndex(guid, params.stackIndex)
    else
      params.itemNum = L_BagManager:getItemNum(guid)
    end
    local tpl = _commonItemTpl:getTplById(guid)
    if _commonItemTpl:getType(tpl) == L_BagConst.bagItemType.food then
      local tpl_food = _foodTpl:getTplById(guid)
      self.bind.active_foodBuff = true
      self.bind.img_foodBuff = string.format("UI/Atlas/FoodType/tex_icon_foodtype_tag_0%s.png", _foodTpl:getFoodType(tpl_food))
      self.bindComponents.ani_cellIconBag:Stop()
      self.bindComponents.ani_cellIconBag:Play("anim_cellicon_init")
    end
    self:setGeneralContent(L_Const.resType.commonItem, guid, params)
  end
end

function this:setSpiritronDataContent(guid, params)
  params = params or {}
  self._warningReddotKey = L_ReddotManager.DotDef.SpiritNew
  self._warningReddotId = guid
  local serverData = L_SpiritronStore:getSpiritronItem(guid)
  if serverData then
    self:setGeneralContent(L_Const.resType.spiritron, serverData.spiritronId, params)
    self:setSpiritStars(serverData.star - 1)
    self.bind.txt_level = string.concat("LV", serverData.spiritronLv)
    self.bind.active_lock = serverData.lock
    local isEquipped = not math.isEmpty(serverData.spiritronHero)
    self.bind.active_owner = isEquipped
    if isEquipped then
      local heroData = L_HeroStore:getHero(serverData.spiritronHero)
      if heroData then
        local heroConf = L_ItemTplManager:getHeroItem(L_HeroStore:getHeroConfigId(heroData))
        self.bind.img_owner = heroConf.icon_small
        if heroData.type == L_Const.HeroType.HT_MAIN then
          L_PhotoManager:newOrBindPhotoEntity(self, self.bindComponents.rect_img_owner, L_PlayerStore:getPlayerModHeadImgName())
        else
          L_PhotoManager:clearPhotoEntity(self)
        end
      end
    end
  end
end

function this:setAccessoryDataContent(guid, params)
  params = params or {}
  if not guid then
    self:setContentActive(false)
    return
  end
  self._warningReddotId = guid
  self._warningReddotKey = L_ReddotManager.DotDef.JewelNew
  local serverData = L_AccessoryStore:getAccessory(guid)
  if serverData then
    self:setGeneralContent(L_Const.resType.accessory, serverData.accessoryId, params)
    self.bind.txt_level = string.concat("+", serverData.level)
    self.bind.txt_num = ""
    self.bind.active_lock = serverData.lock
    local isEquipped = not math.isEmpty(serverData.wearHero)
    self.bind.active_owner = isEquipped
    if isEquipped then
      local heroData = L_HeroStore:getHero(serverData.wearHero)
      if heroData then
        local heroConf = L_ItemTplManager:getHeroItem(L_HeroStore:getHeroConfigId(heroData))
        self.bind.img_owner = heroConf.icon_small
        if heroData.type == L_Const.HeroType.HT_MAIN then
          L_PhotoManager:newOrBindPhotoEntity(self, self.bindComponents.rect_img_owner, L_PlayerStore:getPlayerModHeadImgName())
        else
          L_PhotoManager:clearPhotoEntity(self)
        end
      end
    end
  end
end

function this:setTaskItemDataContent(guid, params)
  params = params or {}
  self._itemType = L_Const.resType.taskItem
  self._guid = guid
  self:setContentActive(true)
  local itemNum = L_TaskStore:getTaskPropNum(guid)
  if 0 < itemNum then
    params.itemNum = itemNum
    self:setGeneralContent(L_Const.resType.taskItem, guid, params)
  end
end

function this:setPetEggDataContent(guid, params)
  params = params or {}
  self._itemType = L_Const.resType.petEgg
  self._guid = guid
  self:setContentActive(true)
  local serverData = L_PetStore:getPetEggItem(guid)
  if serverData then
    params.itemNum = 1
    self:setGeneralContent(L_Const.resType.petEgg, serverData.configId, params)
  end
end

function this:setCollectionToolDataContent(guid, params)
  self._itemType = L_Const.resType.collectionTool
  self._guid = guid
  self:setContentActive(true)
  local serverData = L_CollectionToolStore:getTool(guid)
  local configTable = CS.Azur.Gameplay.Table.TDCollectionToolTable.GetData(serverData.id)
  local enduranceColor = serverData.endurance < 1 and "#FFA6A6" or "#FFFFFF"
  params.itemNumTxt = string.format("%s/%s", L_GameUtil.fillColor(serverData.endurance, enduranceColor), configTable.endurance)
  self:setGeneralContent(L_Const.resType.collectionTool, serverData.id, params)
end

function this:OnAccessoryChanged(newValue)
  print("==============newValue.guid , self._guid", newValue.guid, self._guid)
  if newValue.guid == self.bind.guid or newValue.guid == self._guid then
    self.bind.lock = newValue.lock
    self.bind.txt_level = tostring(newValue.level)
    self:refreshView()
  end
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

function this:setSel(active)
  if self.bind.active_sel ~= active then
    self.bind.active_sel = active
  end
  if active then
    self:clearNew()
  end
end

function this:setContentActive(active)
  self.bind.active_content = active
  self.bind.active_empty = not active
end

function this:setQualityLineActive(active)
  self.bind.active_qualityLine = active
end

function this:setReceiveActive(active)
  self.bind.active_hasobtain = active and active or false
end

function this:setNumInfo(itemNum)
  if not self.isBind then
    return
  end
  self.bind.txt_num = tostring(itemNum)
end

function this:setOwnerActive(isActive)
  self.bind.active_owner = isActive
end

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

function this:clearNew()
  if self._warningReddotKey and self._warningReddotId then
    L_ReddotManager:clearNew(self._warningReddotKey, self._warningReddotId)
  end
end

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

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

return this
