local UIHeroSkillItem = BaseClass("UIHeroSkillItem", UIBaseContainer)
local base = UIBaseContainer
local Localization = CS.GameEntry.Localization
local Resource = CS.GameEntry.Resource
local UIGray = CS.UIGray
local UIHeroSkillStar = require("UI.UILWHero.UIHeroDetailPanel.Component.UIHeroSkillStar")
local OnCreate = function(self)
  base.OnCreate(self)
  self:DataDefine()
  self:ComponentDefine()
end
local OnDestroy = function(self)
  self:DataDestroy()
  self:ComponentDestroy()
  base.OnDestroy(self)
end
local DataDefine = function(self)
  self.showSkillName = true
  self.showSkillLevel = true
  self.selected = false
end
local DataDestroy = function(self)
  self.skillData = nil
  self.callBack = nil
  self.templateItemCallBack = nil
  self.showSkillLevel = nil
  self.showSkillName = nil
  self.selected = nil
end
local OnEnable = function(self)
  base.OnEnable(self)
  self.active = true
end
local OnDisable = function(self)
  base.OnDisable(self)
  self.active = false
end
local ClearStars = function(self)
  if self.stars then
    self.stars:RemoveComponents(UIHeroSkillStar)
    self.starTempalte.gameObject:GameObjectRecycleAll()
    self.newAddStar = nil
  end
end
local ComponentDefine = function(self)
  self.root = self:AddComponent(UIBaseContainer, "")
  self.skillNameText = self:AddComponent(UIText, "ContentContainer/NameText")
  self.skillBtn = self:AddComponent(UIButton, "ContentContainer")
  self.skillIcon = self:AddComponent(UIImage, "ContentContainer/Icon")
  self.levelBg = self:AddComponent(UIImage, "ContentContainer/LevelBg")
  self.levelText = self:AddComponent(UIText, "ContentContainer/LevelBg/LevelText")
  self.lockContent = self:AddComponent(UIImage, "ContentContainer/LockContent")
  self.contentContainer = self:AddComponent(UIBaseContainer, "ContentContainer")
  self.redPoint = self:AddComponent(UIImage, "ContentContainer/RedPoint")
  self.newTag = self:AddComponent(UIImage, "ContentContainer/NewTag")
  self.stars = self:AddComponent(UIBaseContainer, "ContentContainer/Stars")
  self.starTempalte = self.transform:Find("ContentContainer/Stars/StarTemplate").gameObject
  self.starTempalte:GameObjectCreatePool()
  self.selectedBg = self:AddComponent(UIImage, "ContentContainer/SelectedBg")
  self.skillBtn:SetOnClick(BindCallback(self, self.OnItemClick))
end
local ComponentDestroy = function(self)
  self.root = nil
  self.skillNameText = nil
  self.skillBtn = nil
  self.skillIcon = nil
  self.levelBg = nil
  self.levelText = nil
  self.lockContent = nil
  self.contentContainer = nil
  self.redPoint = nil
  self.newTag = nil
  self.stars = nil
  ClearStars(self)
  self.starTempalte = nil
  self.selectedBg = nil
end
local OnAddListener = function(self)
  base.OnAddListener(self)
end
local OnRemoveListener = function(self)
  base.OnRemoveListener(self)
end
local SetSelected = function(self, state)
  self.selected = state
  self.selectedBg:SetActive(state)
end
local GetSelected = function(self)
  return self.selected
end
local GetSkillData = function(self)
  return self.skillData
end
local SetTacticalWeaponSkillData = function(self, skillData, param, callBack)
  if param ~= nil then
    self.showSkillName = param.showSkillName
    self.showSkillLevel = param.showSkillLevel
    self.showLock = param.showLock
    self.showRedPoint = param.showRedPoint
    self.unlockLevel = param.unlockLevel
    self.showStar = param.showStar
  end
  if callBack ~= nil then
    self.callBack = callBack
  end
  self.templateItemCallBack = nil
  if skillData == nil then
    self:SetActive(false)
    return
  else
    self:SetActive(true)
  end
  self.skillData = skillData
  self.skillId = skillData.skillId
  self.level = skillData.level
  self.addMaxLv = skillData.addMaxLv
  if self.showSkillName then
    self.skillNameText:SetActive(true)
    self.skillNameText:SetLocalText(self.skillData.skillTemplateData.name)
  else
    self.skillNameText:SetActive(false)
  end
  self.skillIcon:LoadSprite(self.skillData.skillTemplateData.icon)
  local showSkillLevel = self.showSkillLevel
  if showSkillLevel == nil then
    showSkillLevel = true
  end
  if self.showLock then
    if self.skillData:IsUnlock() then
      UIGray.SetGray(self.skillIcon.transform, false, true)
      self.lockContent:SetActive(false)
    else
      UIGray.SetGray(self.skillIcon.transform, true, true)
      self.lockContent:SetActive(true)
      self.levelBg:SetActive(false)
    end
  else
    UIGray.SetGray(self.skillIcon.transform, false, true)
    self.lockContent:SetActive(false)
  end
  if showSkillLevel then
    self.levelBg:SetActive(true)
    if self.skillData:IsReachGroupMaxLevel() then
      self.levelText:SetLocalText("110000")
    else
      self.levelText:SetText(self.skillData:GetLevel())
    end
  else
    self.levelBg:SetActive(false)
  end
  if self.showRedPoint then
    self.redPoint:SetActive(true)
  else
    self.redPoint:SetActive(false)
  end
  if self.showStar then
    self.stars:SetActive(true)
    ClearStars(self)
    local curStar = self.skillData:GetStar()
    if 0 < curStar then
      local showStarCount = math.min(curStar, 5)
      local leftWindow = math.max(0, curStar - 5)
      for i = 1, showStarCount do
        local item = self.starTempalte:GameObjectSpawn(self.stars.transform)
        item.name = "star" .. i
        local viewStarIndex = leftWindow + i
        local cell = self.stars:AddComponent(UIHeroSkillStar, item.name)
        cell:SetFilled(true)
        cell:SetStarIndex(viewStarIndex)
      end
    end
    self.curStar = curStar
  else
    self.stars:SetActive(false)
  end
  self.newTag:SetActive(false)
  SetSelected(self, false)
end
local SetData = function(self, skillData, param, callBack)
  if param ~= nil then
    self.showSkillName = param.showSkillName
    self.showSkillLevel = param.showSkillLevel
    self.showLock = param.showLock
    self.showRedPoint = param.showRedPoint
    self.unlockLevel = param.unlockLevel
    self.showStar = param.showStar
    self.showSkillMaxLevel = param.showSkillMaxLevel
  end
  if callBack ~= nil then
    self.callBack = callBack
  end
  self.templateItemCallBack = nil
  if skillData == nil then
    self:SetActive(false)
    return
  else
    self:SetActive(true)
  end
  self.skillData = skillData
  self.skillId = skillData.skillId
  self.level = skillData.level
  self.addMaxLv = skillData.addMaxLv
  if self.showSkillName then
    self.skillNameText:SetActive(true)
    self.skillNameText:SetLocalText(self.skillData.skillTemplateData.name)
  else
    self.skillNameText:SetActive(false)
  end
  self.skillIcon:LoadSprite(self.skillData.skillTemplateData.icon)
  local showSkillLevel = self.showSkillLevel
  if showSkillLevel == nil then
    showSkillLevel = true
  end
  if self.showLock then
    if self.skillData:IsUnlock() then
      UIGray.SetGray(self.skillIcon.transform, false, true)
      self.lockContent:SetActive(false)
    else
      UIGray.SetGray(self.skillIcon.transform, true, true)
      self.lockContent:SetActive(true)
      self.levelBg:SetActive(false)
    end
  else
    UIGray.SetGray(self.skillIcon.transform, false, true)
    self.lockContent:SetActive(false)
  end
  if showSkillLevel then
    self.levelBg:SetActive(true)
    if (self.showSkillMaxLevel == nil or self.showSkillMaxLevel == true) and self.skillData:IsReachGroupMaxLevel() then
      self.levelText:SetLocalText("110000")
    else
      self.levelText:SetText(self.skillData:GetLevel())
    end
  else
    self.levelBg:SetActive(false)
  end
  if self.showRedPoint then
    self.redPoint:SetActive(true)
  else
    self.redPoint:SetActive(false)
  end
  if self.showStar and not self.skillData:IsUnlock() then
    self.showStar = false
  end
  if self.showStar then
    self.stars:SetActive(true)
    ClearStars(self)
    local curStar = self.skillData:GetStar()
    if 0 < curStar then
      local showStarCount = math.min(curStar, 5)
      local leftWindow = math.max(0, curStar - 5)
      for i = 1, showStarCount do
        local item = self.starTempalte:GameObjectSpawn(self.stars.transform)
        item.name = "star" .. i
        local viewStarIndex = leftWindow + i
        local cell = self.stars:AddComponent(UIHeroSkillStar, item.name)
        cell:SetFilled(true)
        cell:SetStarIndex(viewStarIndex)
      end
    end
    self.curStar = curStar
  else
    self.stars:SetActive(false)
  end
  self.newTag:SetActive(false)
  SetSelected(self, false)
end
local OnItemClick = function(self)
  if self.callBack and self.skillData then
    self.callBack(self.skillData, self)
  elseif self.templateItemCallBack and self.skillId then
    self.templateItemCallBack(self.skillId, self.level, self.addMaxLv, self)
  end
end
local ShowNewTag = function(self)
  self.newTag:SetActive(true)
  if self.showRedPoint then
    self.redPoint:SetActive(false)
  end
end
local HideNewTag = function(self)
  self.newTag:SetActive(false)
  if self.showRedPoint then
    self.redPoint:SetActive(true)
  end
end
local GetNewTagShowState = function(self)
  return self.newTag.gameObject.activeSelf
end
local ShowNewStarEffect = function(self)
  if not self.newAddStar then
    local star = self.starTempalte:GameObjectSpawn(self.stars.transform)
    star.name = "NewAddStar"
    self.newAddStar = self.stars:AddComponent(UIHeroSkillStar, star.name)
    star.transform:SetAsFirstSibling()
  end
  self.newAddStar:SetFilled(true)
  self.newAddStar:SetStarIndex((self.curStar or 0) + 1)
  self.newAddStar:PlayBreathEffect()
end
local SetTemplateData = function(self, skillId, level, addMaxLv, param, callBack)
  if not skillId then
    self:SetActive(false)
    return
  else
    self:SetActive(true)
  end
  if param ~= nil then
    self.showSkillName = param.showSkillName
    self.showSkillLevel = param.showSkillLevel
    self.showLock = param.showLock
    self.showRedPoint = param.showRedPoint
    self.showStar = param.showStar
  end
  local heroSkillTemplate = DataCenter.HeroSkillTemplateManager:GetTemplate(skillId)
  if heroSkillTemplate == nil then
    self:SetActive(false)
    return
  end
  local _level = level or 1
  local _star = heroSkillTemplate.star
  local _addMaxLevel = addMaxLv or 0
  if callBack ~= nil then
    self.templateItemCallBack = callBack
  end
  self.callBack = nil
  self.skillId = skillId
  self.level = _level
  self.addMaxLv = _addMaxLevel
  if self.showSkillName then
    self.skillNameText:SetActive(true)
    self.skillNameText:SetLocalText(heroSkillTemplate.name)
  else
    self.skillNameText:SetActive(false)
  end
  self.skillIcon:LoadSprite(heroSkillTemplate.icon)
  local showSkillLevel = self.showSkillLevel
  if showSkillLevel == nil then
    showSkillLevel = true
  end
  if self.showLock then
    UIGray.SetGray(self.skillIcon.transform, true, true)
    self.lockContent:SetActive(true)
  else
    UIGray.SetGray(self.skillIcon.transform, false, true)
    self.lockContent:SetActive(false)
  end
  if showSkillLevel then
    self.levelBg:SetActive(true)
    local groupMaxLevel = DataCenter.HeroSkillTemplateManager:GetSkillGroupMaxLevel(heroSkillTemplate.group, _addMaxLevel)
    if groupMaxLevel == _level then
      self.levelText:SetText("max")
    else
      self.levelText:SetText(_level)
    end
  else
    self.levelBg:SetActive(false)
  end
  self.redPoint:SetActive(false)
  if self.showStar then
    self.stars:SetActive(true)
    ClearStars(self)
    local curStar = _star
    if 0 < curStar then
      local showStarCount = math.min(curStar, 5)
      local leftWindow = math.max(0, curStar - 5)
      for i = 1, showStarCount do
        local item = self.starTempalte:GameObjectSpawn(self.stars.transform)
        item.name = "star" .. i
        local viewStarIndex = leftWindow + i
        local cell = self.stars:AddComponent(UIHeroSkillStar, item.name)
        cell:SetFilled(true)
        cell:SetStarIndex(viewStarIndex)
      end
    end
    self.curStar = curStar
  else
    self.stars:SetActive(false)
  end
  self.newTag:SetActive(false)
  SetSelected(self, false)
end
local SetParamsForUniqueWeaponEffect = function(self, icon, locked)
  self.skillIcon:LoadSprite(icon)
  self.skillNameText:SetActive(false)
  self.levelBg:SetActive(false)
  self.redPoint:SetActive(false)
  self.stars:SetActive(false)
  self.newTag:SetActive(false)
  SetSelected(self, false)
  if locked then
    UIGray.SetGray(self.skillIcon.transform, true, true)
    self.lockContent:SetActive(true)
  else
    UIGray.SetGray(self.skillIcon.transform, false, true)
    self.lockContent:SetActive(false)
  end
end

function UIHeroSkillItem:ForceShowLevelNumText()
  if self.levelText and self.skillData then
    self.levelText:SetText(self.skillData:GetLevel())
  end
end

UIHeroSkillItem.OnCreate = OnCreate
UIHeroSkillItem.OnDestroy = OnDestroy
UIHeroSkillItem.OnEnable = OnEnable
UIHeroSkillItem.OnDisable = OnDisable
UIHeroSkillItem.DataDefine = DataDefine
UIHeroSkillItem.DataDestroy = DataDestroy
UIHeroSkillItem.ComponentDefine = ComponentDefine
UIHeroSkillItem.ComponentDestroy = ComponentDestroy
UIHeroSkillItem.SetData = SetData
UIHeroSkillItem.OnAddListener = OnAddListener
UIHeroSkillItem.OnRemoveListener = OnRemoveListener
UIHeroSkillItem.OnItemClick = OnItemClick
UIHeroSkillItem.ShowNewTag = ShowNewTag
UIHeroSkillItem.HideNewTag = HideNewTag
UIHeroSkillItem.GetNewTagShowState = GetNewTagShowState
UIHeroSkillItem.ShowNewStarEffect = ShowNewStarEffect
UIHeroSkillItem.SetSelected = SetSelected
UIHeroSkillItem.GetSelected = GetSelected
UIHeroSkillItem.SetTemplateData = SetTemplateData
UIHeroSkillItem.GetSkillData = GetSkillData
UIHeroSkillItem.SetTacticalWeaponSkillData = SetTacticalWeaponSkillData
return UIHeroSkillItem
