local this = class("heroVoiceManager")
local charVoiceHeroTabTpl = L_GameTpl:getCharVoiceHeroTabTpl()
local voiceMap = {
  [L_Const.heroModuleType.Property] = "detailsVoice",
  [L_Const.heroModuleType.Skill] = "skillVoice",
  [L_Const.heroModuleType.Star] = "breakVoice",
  [L_Const.heroModuleType.Spiritron] = "spiritronVoice",
  [L_Const.heroModuleType.Accessory] = "equipVoice",
  [L_Const.heroModuleType.Data] = "dataVoice",
  [L_Const.heroModuleType.Talent] = "talentVoice"
}
local delayMap = {
  [L_Const.heroModuleType.Property] = "detailsDelay",
  [L_Const.heroModuleType.Skill] = "skillDelay",
  [L_Const.heroModuleType.Star] = "breakDelay",
  [L_Const.heroModuleType.Spiritron] = "spiritronDelay",
  [L_Const.heroModuleType.Accessory] = "equipDelay",
  [L_Const.heroModuleType.Data] = "dataDelay",
  [L_Const.heroModuleType.Talent] = "talentDelay"
}

function this:playHeroVoiceByConfigId(heroConifgId, moduleType)
  if not voiceMap[moduleType] or not delayMap[moduleType] then
    errorf(string.format("\233\161\181\231\173\190\231\177\187\229\158\139:%s \232\191\152\230\178\161\230\156\137\233\133\141\231\189\174", moduleType), 2)
    return
  end
  local voiceTpl = charVoiceHeroTabTpl:getTplById(heroConifgId)
  if not voiceTpl then
    errorf(string.format("\232\139\177\233\155\132:%s char_voice_hero_tab\232\161\168\233\133\141\231\189\174\228\184\141\229\173\152\229\156\168", heroConifgId), 2)
    return
  end
  local audioEvent = voiceTpl[voiceMap[moduleType]]
  local delayTime = voiceTpl[delayMap[moduleType]]
  self:stopHeroVoice()
  if not math.isEmpty(delayTime) then
    self.voiceTimer = Timer.once(delayTime / 1000, function()
      self:playHeroVoice(audioEvent)
      printf("\232\167\146\232\137\178\232\175\173\233\159\179\230\146\173\230\148\190", string.format("\232\167\146\232\137\178: %s    \232\175\173\233\159\179: %s", heroConifgId, audioEvent))
      if self.voiceTimer then
        Timer.remove(self.voiceTimer)
        self.voiceTimer = nil
      end
    end, self)
  else
    self:playHeroVoice(audioEvent)
    printf("\232\167\146\232\137\178\232\175\173\233\159\179\230\146\173\230\148\190", string.format("\232\167\146\232\137\178: %s    \232\175\173\233\159\179: %s", heroConifgId, audioEvent))
  end
end

function this:playHeroVoice(audioEvent)
  self:stopHeroVoice()
  if not string.isEmpty(audioEvent) then
    self.curVoice = C_AudioManager.Play(audioEvent)
    self.curVoice:SetStopTransitionDuration(0)
    if L_CommonUtil.isValid(self.curVoice) then
      self.curVoice:SetAutoRelease(false)
    end
  end
end

function this:stopHeroVoice()
  if self.voiceTimer then
    Timer.remove(self.voiceTimer)
    self.voiceTimer = nil
  end
  if L_CommonUtil.isValid(self.curVoice) then
    C_AudioManager.FreeSource(self.curVoice)
  end
  self.curVoice = nil
end

return this
