local HeroTrialHeroInnerItem = BaseClass("HeroTrialHeroInnerItem", UIBaseContainer)
local base = UIBaseContainer
local LWHeroRankStar = require("UI.UIHero2.Common.LWHeroRankStar")
local OnCreate = function(self)
  base.OnCreate(self)
  self:ComponentDefine()
  self:DataDefine()
end
local OnDestroy = function(self)
  self:ComponentDestroy()
  self:DataDestroy()
  base.OnDestroy(self)
end
local OnEnable = function(self)
  base.OnEnable(self)
end
local OnDisable = function(self)
  base.OnDisable(self)
end
local ComponentDefine = function(self)
  self.layerNormal = self:AddComponent(UIBaseContainer, "LayerNormal")
  self.img_bg = self:AddComponent(UIImage, "LayerNormal/ImgBg")
  self.img_icon1 = self:AddComponent(UIImage, "LayerNormal/ImgBg/Mask/ImgIcon1")
  self.level_bg = self:AddComponent(UIImage, "LevelBg")
  self.text_level = self:AddComponent(UIText, "LevelBg/TextLevel")
  self.btn_go = self:AddComponent(UIButton, "")
  self.btn_go:SetOnClick(function()
    CS.GameEntry.Sound:PlayEffect(SoundAssets.Music_Effect_Button)
    self:OnBtnClick()
  end)
  if self.transform:Find("typeLayOut/TypeIcon") then
    self.typeIcon = self:AddComponent(UIImage, "typeLayOut/TypeIcon")
  elseif self.transform:Find("TypeIcon") then
    self.typeIcon = self:AddComponent(UIImage, "TypeIcon")
  end
  if self.transform:Find("typeLayOut/jobIcon") then
    self.jobIcon = self:AddComponent(UIImage, "typeLayOut/jobIcon")
  end
  self.heroRankStar = self:AddComponent(LWHeroRankStar, "HeroRankStar")
end
local ComponentDestroy = function(self)
  self.layerNormal = nil
  self.img_bg = nil
  self.img_icon1 = nil
  self.level_bg = nil
  self.text_level = nil
  self.btn_go = nil
  self.typeIcon = nil
end
local DataDefine = function(self)
  self.param = nil
  self.callBack = nil
end
local DataDestroy = function(self)
  self.param = nil
  self.callBack = nil
end
local SetHeroRank = function(self, rank)
  if rank == nil then
    self.heroRankStar:SetActive(false)
  else
    self.heroRankStar:SetActive(true)
    self.heroRankStar:ShowRank(rank, self.heroData.meta.maxRank)
  end
end
local SetData = function(self, heroUuid, callBack)
  self.param = heroUuid
  self.itemId = nil
  self.callBack = callBack
  local heroData = DataCenter.HeroDataManager:GetHeroByUuid(heroUuid)
  local quality = heroData.quality
  self.heroData = heroData
  self.quality = heroData.quality
  self.heroId = heroData.heroId
  self:SetQuality(quality, false)
  local iconPath = HeroUtils.GetHeroIconPath(heroData.modelId, HeroIconType.half_portrait)
  self.img_icon1:LoadSprite(iconPath)
  self.text_level:SetText("Lv." .. tostring(heroData.level))
  self:ToggleLevel(false)
  self:ToggleFragCount(false)
  self:ToggleBlackMask(false)
  SetHeroRank(self, heroData:GetRank())
  if self.typeIcon then
    self.typeIcon:LoadSprite(HeroUtils.GetHeroTypeIcon(heroData.heroType))
  end
  if self.jobIcon then
    self.jobIcon:LoadSprite(HeroUtils.GetHeroJobIcon(heroData.meta.job, 2))
    self.jobIcon:SetActive(true)
  end
end
local InitWithHeroPieceItem = function(self, itemId, callBack)
  self.param = itemId
  self.callBack = callBack
  local item = DataCenter.ItemTemplateManager:GetItemTemplate(itemId)
  local heroConfig = DataCenter.HeroTemplateManager:GetTemplate(toInt(item.para2))
  if heroConfig == nil then
    return
  end
  local quality = heroConfig.quality
  self.heroId = heroConfig.id
  self:SetQuality(quality, true, false)
  local iconPath = HeroUtils.GetHeroIconPath(heroConfig.appearance, HeroIconType.half_portrait)
  self.img_icon1:LoadSprite(iconPath)
  self.typeIcon:LoadSprite(HeroUtils.GetHeroTypeIcon(heroConfig.type))
  self.heroRankStar:SetActive(true)
  self.heroRankStar:ShowRank(0, heroConfig.maxRank)
end
local RefreshPieceItem = function(self)
  local itemCount = DataCenter.ItemData:GetItemCount(self.param)
  local itemNeed = HeroUtils.GetJigsawCost(self.param)
  self:ToggleBlackMask(itemCount < itemNeed)
end
local InitWithConfigId = function(self, heroId, quality, level, rankId)
  self.param = heroId
  self.itemId = nil
  local heroConfig = DataCenter.HeroTemplateManager:GetTemplate(heroId)
  if heroConfig == nil then
    return
  end
  if quality == nil then
    quality = heroConfig.quality
  end
  self.heroId = heroConfig.id
  self:SetQuality(quality, level == nil, false)
  local iconPath = HeroUtils.GetHeroIconPath(heroConfig.appearance, HeroIconType.half_portrait)
  self.img_icon1:LoadSprite(iconPath)
  self.typeIcon:LoadSprite(HeroUtils.GetHeroTypeIcon(heroConfig.type))
  self.text_level:SetText(level ~= nil and "Lv." .. tostring(level) or "")
  SetHeroRank(self, rankId)
end
local SetQuality = function(self, quality, hideLv, isPoster, isWaken, isItem)
  self.img_bg:LoadSprite(HeroUtils.GetQualityIconPath(quality, true, false))
  self.level_bg:LoadSprite(HeroUtils.GetLevelBg(quality))
end
local OnBtnClick = function(self)
  local openHeroExhibit = CommonUtil.PlayerPrefsGetBool("OPEN_HERO_EXHIBIT_IN_HERO_VIEW", false)
  if not openHeroExhibit then
    self:UpdateRedPoint()
    if self.callBack ~= nil then
      self.callBack(self.transform, self.param)
    end
  else
    local heroUuid = self.param
    local heroData = DataCenter.HeroDataManager:GetHeroByUuid(heroUuid)
    if heroUuid ~= nil and heroData ~= nil and DataCenter.HeroDataManager:NeedShowNewHeroWindow(heroUuid) then
      UIManager:GetInstance():OpenWindow(UIWindowNames.UIHeroExhibitPanel, {anim = false}, heroUuid, {heroUuid}, nil, true)
    end
  end
end
local UpdateRedPoint = function(self)
end
local EnableRedPoint = function(self)
  self.enableRedPoint = true
  self:UpdateRedPoint()
end
local DisableRedPoint = function(self)
  self.enableRedPoint = false
end
local GetRarityMaskPath = function(rarity, isWaken)
  if isWaken then
    return string.format(LoadPath.HeroListPath, "ui_quality_mask_cai_big")
  end
  local mask = {
    "ui_quality_mask_orange_big",
    "ui_quality_mask_purple_big",
    "ui_quality_mask_blue_big",
    "ui_quality_mask_green_big"
  }
  local iconName = mask[rarity]
  return string.format(LoadPath.HeroListPath, iconName)
end
local GetRarityLvBgPath = function(rarity, isWaken)
  local path = LoadPath.HeroListPath
  if isWaken then
    return string.format(path, "ui_herolist_lv_cai_bg")
  end
  local bg = {
    "ui_herolist_lv_orange_bg",
    "ui_herolist_lv_purple_bg",
    "ui_herolist_lv_blue_bg",
    "ui_herolist_lv_green_bg"
  }
  local iconName = bg[rarity]
  return string.format(path, iconName)
end
local GetCellPos = function(self)
  return self.layerNormal.transform.position
end
local GetCellSizeDelta = function(self)
  return self.layerNormal:GetSizeDelta()
end
local ToggleLevel = function(self, t)
  self.text_level:SetActive(t)
end
local GetGuideClickBtn = function(self)
  return self.btn_go
end
local HideStar = function(self)
end
local ToggleFragCount = function(self, t)
end
local ToggleBlackMask = function(self, t)
end
HeroTrialHeroInnerItem.OnCreate = OnCreate
HeroTrialHeroInnerItem.OnDestroy = OnDestroy
HeroTrialHeroInnerItem.OnEnable = OnEnable
HeroTrialHeroInnerItem.OnDisable = OnDisable
HeroTrialHeroInnerItem.ComponentDefine = ComponentDefine
HeroTrialHeroInnerItem.ComponentDestroy = ComponentDestroy
HeroTrialHeroInnerItem.DataDefine = DataDefine
HeroTrialHeroInnerItem.DataDestroy = DataDestroy
HeroTrialHeroInnerItem.SetData = SetData
HeroTrialHeroInnerItem.InitWithConfigId = InitWithConfigId
HeroTrialHeroInnerItem.SetQuality = SetQuality
HeroTrialHeroInnerItem.OnBtnClick = OnBtnClick
HeroTrialHeroInnerItem.HideStar = HideStar
HeroTrialHeroInnerItem.EnableRedPoint = EnableRedPoint
HeroTrialHeroInnerItem.DisableRedPoint = DisableRedPoint
HeroTrialHeroInnerItem.UpdateRedPoint = UpdateRedPoint
HeroTrialHeroInnerItem.GetRarityMaskPath = GetRarityMaskPath
HeroTrialHeroInnerItem.GetRarityLvBgPath = GetRarityLvBgPath
HeroTrialHeroInnerItem.GetCellPos = GetCellPos
HeroTrialHeroInnerItem.GetCellSizeDelta = GetCellSizeDelta
HeroTrialHeroInnerItem.ToggleLevel = ToggleLevel
HeroTrialHeroInnerItem.GetGuideClickBtn = GetGuideClickBtn
HeroTrialHeroInnerItem.InitWithHeroPieceItem = InitWithHeroPieceItem
HeroTrialHeroInnerItem.RefreshPieceItem = RefreshPieceItem
HeroTrialHeroInnerItem.ToggleFragCount = ToggleFragCount
HeroTrialHeroInnerItem.ToggleBlackMask = ToggleBlackMask
return HeroTrialHeroInnerItem
