local this = class("moduleSpiritInfo", G_UIModuleBase)
local MaxColor = C_Color(0.43, 0.57, 0.87)

function this.bind()
  return {
    spiritName = "",
    moduleStar = {
      moduleName = "pages/HeroPanel/moduleHeroShowRank"
    },
    starActive = false,
    expsliderValue = 0,
    sliderColor = C_Color.white,
    levelText = "",
    levelLimitText = "",
    expText = "",
    romanNum = "",
    romanNumActive = false,
    moduleProperty = {
      moduleName = "pages/heroPanel/spirit/moduleSpiritProperty"
    },
    skillText = "",
    skillTextActive = false,
    spiritEmptyTipsActive = false,
    strengthBtnActive = false,
    switchBtnActive = false,
    equipBtnActive = false,
    viewBtnActibe = false,
    lockActive = false,
    lockIconActive = false,
    spiritronCardItem = {
      moduleName = "pages/Spiritron/cellSpiritronCardItem"
    },
    cellSpiritronCardItemActive = false,
    upBtnActive = true,
    desAreaActive = false,
    desText = "",
    viewBtnActive = true,
    storyRedActive = false
  }
end

function this.methods()
  return {
    onClick_strengthBtn = function(self)
      L_SpiritronManager:openSpiritUpgrade({
        isPreview = self.isPreview,
        spiritGuid = self.spiritGuid,
        heroGuid = self.heroGuid,
        jumpMode = L_Const.spiritUpgradeModules.moduleSpiritLevelUp
      })
    end,
    onClick_switchBtn = function(self)
      L_SpiritronManager:openSpiritSwitch({
        spiritGuid = self.spiritGuid,
        heroGuid = self.heroGuid
      })
    end,
    onClick_equipBtn = function(self)
      L_SpiritronManager:openSpiritSwitch({
        spiritGuid = self.spiritGuid,
        heroGuid = self.heroGuid
      })
    end,
    onClick_viewBtn = function(self)
      local spTpl = L_GameTpl:getSpiritronTpl()
      local serverData = L_SpiritronManager:getSpiritronItem(self.spiritGuid, self.isPreview)
      local tpl = spTpl:getTplById(serverData.spiritronId)
      local cgPath = spTpl:getCgPath(tpl)
      if string.isEmpty(cgPath) then
        L_UI:open("pageSpiritronInfoLow", {
          id = serverData.spiritronId,
          isPreview = self.isPreview
        })
      else
        L_UI:open("pageSpiritronInfo", {
          id = serverData.spiritronId,
          isPreview = self.isPreview
        })
      end
    end,
    onClick_lock = function(self)
      if math.isEmpty(self.spiritGuid) then
        return
      end
      L_SpiritronStore:req_lockSpiritron(self.spiritGuid, function()
        local serverData = L_SpiritronManager:getSpiritronItem(self.spiritGuid, self.isPreview)
        self.bind.lockIconActive = serverData.lock
      end)
    end
  }
end

function this:initModule(data)
  self.spiritGuid = data.spiritGuid
  self.heroGuid = data.heroGuid
  self.isPreview = data.isPreview
  self:refreshView()
  self.bind.storyRedActive = not self.isPreview
  if self.isPreview then
    self.bindComponents.animation:Play("anim_heroes_spiritron_info_switch_star_init")
  else
    local serverData = L_SpiritronManager:getSpiritronItem(data.spiritGuid)
    L_ReddotManager:registerReddot(self.bindComponents.storyRedDot, string.format(L_ReddotManager.DotDef.SpiritSory, serverData.spiritronId))
  end
end

function this:open(options)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.refreshWeapon, self.refreshView, self)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.spiritLockStatusChange, self.setInfo, self)
end

function this:close(options)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.refreshWeapon, self.refreshView)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.spiritLockStatusChange, self.setInfo)
end

function this:refreshView()
  local isEmpty = math.isEmpty(self.spiritGuid)
  self.bind.strengthBtnActive = not isEmpty
  self.bind.switchBtnActive = not isEmpty
  self.bind.equipBtnActive = isEmpty
  self.bind.viewBtnActibe = not isEmpty
  self.bind.romanNumActive = not isEmpty
  self.bind.skillTextActive = not isEmpty
  self.bind.spiritEmptyTipsActive = isEmpty
  self.bind.lockActive = not isEmpty
  self.bind.starActive = not isEmpty
  self.bind.nameTextActive = not isEmpty
  self.bind.nameNoStar = isEmpty
  self.bind.cellSpiritronCardItemActive = false
  self.modules.moduleProperty:initModule(self.spiritGuid, self.heroGuid, self.isPreview)
  if isEmpty then
    self:setEmpty()
  else
    self:setInfo()
  end
  if self.isPreview then
    self.bind.lockActive = false
    local spiritConfigId = self.parent:getModeController()._spiritronId
    if L_SpiritronStore:getHasSpiritron(spiritConfigId) then
      self.bind.viewBtnActive = true
    else
      self.bind.viewBtnActive = false
    end
    self.bind.expText = ""
  end
  self.bind.cellSpiritronCardItemActive = not isEmpty
end

function this:setEmpty()
  self.bind.levelText = "0"
  self.bind.levelLimitText = "0"
  self.bind.expText = "--/--"
  self.bind.spiritName = L_WordsTpl:getValue("ui_spirit_not_equip")
  self.bind.expsliderValue = 0
end

function this:setInfo()
  if math.isEmpty(self.spiritGuid) then
    return
  end
  local serverData = L_SpiritronManager:getSpiritronItem(self.spiritGuid, self.isPreview)
  self.bind.skillText = L_SpiritronManager:getSkillDescByGuidByitem(serverData)
  local spiritronRankTpl = L_GameTpl:getSpiritronRankTpl()
  local item = L_ItemTplManager:getSpiritronItem(serverData.spiritronId)
  local rankConfs = spiritronRankTpl:getChildren(serverData.spiritronId)
  local lvMax = rankConfs[serverData.rank].rankLevelLimit
  local currLv = serverData.spiritronLv
  local isMax = currLv == lvMax
  self.bind.expsliderValue = L_HeroManager:sliderChange(serverData.exp / L_SpiritronManager:getExpMax(serverData.spiritronId, serverData.spiritronLv))
  self.bind.sliderColor = isMax and MaxColor or C_Color.white
  self.bind.levelText = tostring(currLv)
  self.bind.levelLimitText = tostring(lvMax)
  self.bind.expText = string.format("%s<color=#aaaaaa>/%s</color>", serverData.exp, L_SpiritronManager:getExpMax(serverData.spiritronId, serverData.spiritronLv))
  self.modules.moduleStar:refreshStar(serverData.star - 1)
  self.bind.spiritName = item.name
  self.modules.spiritronCardItem:setId(serverData.spiritronId)
  self.bind.romanNumActive = serverData.rank - 1 > 0
  if serverData.rank - 1 > 0 then
    self.bind.romanNum = "BSHero/tex_herodetail_dec_jinjie" .. serverData.rank - 1
  end
  self.bind.lockIconActive = serverData.lock
  local spiritronTpl = L_GameTpl:getSpiritronTpl()
  local tpl = spiritronTpl:getTplById(serverData.spiritronId)
  self.bind.desText = spiritronTpl:getDesc(tpl)
  self.bind.starActive = serverData.star > 1
  if serverData.spiritronLv == L_SpiritronManager:getMaxLevel(self.spiritGuid, self.isPreview) then
    self.bind.expText = "MAX"
    self.bind.expsliderValue = 1
  end
end

function this:refreshLevel(option)
  local serverData = L_SpiritronManager:getSpiritronItem(option.spiritGuid, option.isPreview)
  local spiritronRankTpl = L_GameTpl:getSpiritronRankTpl()
  local item = L_ItemTplManager:getSpiritronItem(serverData.spiritronId)
  local rankConfs = spiritronRankTpl:getChildren(serverData.spiritronId)
  local lvMax = rankConfs[serverData.rank].rankLevelLimit
  local currLv = serverData.spiritronLv
  local isMax = currLv == lvMax
  self.bind.expsliderValue = L_HeroManager:sliderChange(serverData.exp / L_SpiritronManager:getExpMax(serverData.spiritronId, serverData.spiritronLv))
  self.bind.sliderColor = isMax and MaxColor or C_Color.white
  self.bind.levelText = tostring(currLv)
  self.bind.levelLimitText = tostring(lvMax)
  self.bind.expText = string.format("%s<color=#aaaaaa>/%s</color>", serverData.exp, L_SpiritronManager:getExpMax(serverData.spiritronId, serverData.spiritronLv))
  self.modules.moduleStar:refreshStar(serverData.star - 1)
  self.bind.romanNumActive = serverData.rank - 1 > 0
  if serverData.rank - 1 > 0 then
    self.bind.romanNum = "BSHero/tex_herodetail_dec_jinjie" .. serverData.rank - 1
  end
  self.bind.starActive = serverData.star > 1
  if serverData.spiritronLv == L_SpiritronManager:getMaxLevel(option.spiritGuid, option.isPreview) then
    self.bind.expText = "MAX"
    self.bind.expsliderValue = 1
  end
  self.modules.moduleProperty:initModule(option.spiritGuid, option.heroGuid, option.isPreview)
end

function this:playPreviewMaxAnimation(isMax)
  if isMax then
    self.bindComponents.animation:Play("anim_heroes_spiritron_info_switch_star1")
  else
    self.bindComponents.animation:Play("anim_heroes_spiritron_info_switch_star_hide")
  end
end

function this:setUpBtnActive(active)
  self.bind.upBtnActive = active and not self.isPreview
  self.bind.desAreaActive = not active or self.isPreview
end

return this
