local this = class("moduleHeroSpiritUpgrade", G_UIModuleBase)
local moduleNames = L_Const.spiritUpgradeModules

function this.bind()
  return {
    breakBtnTitle = "",
    breakBtnActiveTitle = "",
    spiritronCardItem = {
      moduleName = "pages/Spiritron/cellSpiritronCardItem"
    },
    moduleSpiritInfo = {
      moduleName = "pages/heroPanel/spirit/moduleSpiritInfo"
    },
    moduleSpiritBreak = {
      moduleName = "pages/heroPanel/spirit/moduleSpiritBreak"
    },
    moduleSpiritLevelUp = {
      moduleName = "pages/heroPanel/spirit/moduleSpiritLevelUp"
    },
    moduleSpiritStarUp = {
      moduleName = "pages/heroPanel/spirit/moduleSpiritStarUp"
    },
    moduleSpiritInfoActive = true,
    moduleSpiritBreakActive = false,
    moduleSpiritLevelUpActive = false,
    moduleSpiritStarUpActive = false,
    leftAreaActive = true,
    detailToggleIsOn = true,
    breakToogleIsOn = false,
    starToggleIsOn = false,
    levelUpIconActive = false,
    shadowLevelUpIconActive = false,
    breakIconActive = true,
    shadowBreakIconActive = true,
    currencyActive = false,
    storyRedActive = false
  }
end

function this.methods()
  return {
    onClick_detail = function(self, isToggle)
      if isToggle then
        self:onFunctionSwicth(moduleNames.moduleSpiritInfo)
      end
    end,
    onClick_break = function(self, isToggle)
      if isToggle then
        self:checkCurrentUpgradeStatus()
        self:onFunctionSwicth(self.upgradeStatus)
      end
    end,
    onClick_Star = function(self, isToggle)
      if isToggle then
        self:onFunctionSwicth(moduleNames.moduleSpiritStarUp)
      end
    end
  }
end

function this:open(options)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.refreshWeapon, self.checkUpgradeStatus, self)
end

function this:close(options)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.refreshWeapon, self.checkUpgradeStatus)
end

function this:checkUpgradeStatus()
  self:checkCurrentUpgradeStatus()
  local needRefresh = false
  if self.upgradeModuleName == moduleNames.moduleSpiritLevelUp or self.upgradeModuleName == moduleNames.moduleSpiritBreak then
    needRefresh = self.upgradeModuleName ~= self.upgradeStatus
    self.upgradeModuleName = self.upgradeStatus
  end
  if needRefresh then
    self:refreshCurrentModule()
  end
  self.bind.moduleSpiritInfoActive = self.upgradeModuleName == moduleNames.moduleSpiritInfo
  self.bind.moduleSpiritBreakActive = self.upgradeModuleName == moduleNames.moduleSpiritBreak
  self.bind.moduleSpiritLevelUpActive = self.upgradeModuleName == moduleNames.moduleSpiritLevelUp
  self.bind.moduleSpiritStarUpActive = self.upgradeModuleName == moduleNames.moduleSpiritStarUp
  self:setBtnText()
end

function this:refreshCurrentModule()
  local curModule = self.modules[self.upgradeModuleName]
  if curModule ~= nil then
    curModule:initModule({
      heroGuid = self.heroGuid,
      spiritGuid = self.spiritGuid,
      isPreview = self.isPreview
    })
    if self.upgradeModuleName == moduleNames.moduleSpiritInfo then
      curModule:setUpBtnActive(false)
    end
    self.bind.currencyActive = self.upgradeModuleName == moduleNames.moduleSpiritLevelUp or self.upgradeModuleName == moduleNames.moduleSpiritBreak
    self:initCurrencyModule()
  end
end

function this:onTopBarRefresh()
  self:initCurrencyModule()
end

function this:initCurrencyModule()
  local systemId
  if self.upgradeModuleName == moduleNames.moduleSpiritLevelUp then
    systemId = L_SystemConst.enum.spiritronStrength
  end
  if self.upgradeModuleName == moduleNames.moduleSpiritBreak then
    systemId = L_SystemConst.enum.spiritronAdvance
  end
  L_TopBarManager:setResourcesBySystemId(systemId)
end

function this:checkCurrentUpgradeStatus()
  local currSpData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  local maxLevel = L_SpiritronManager:getMaxLevel(self.spiritGuid)
  if maxLevel <= currSpData.spiritronLv then
    local spiritronRankTpl = L_GameTpl:getSpiritronRankTpl()
    local maxRank = spiritronRankTpl:getMaxRank(currSpData.spiritronId)
    if maxRank <= currSpData.rank then
      self.upgradeStatus = moduleNames.moduleSpiritLevelUp
    else
      self.upgradeStatus = moduleNames.moduleSpiritBreak
    end
  else
    self.upgradeStatus = moduleNames.moduleSpiritLevelUp
  end
end

function this:refreshStatus()
  self:checkCurrentUpgradeStatus()
  if self.upgradeModuleName == moduleNames.moduleSpiritLevelUp or self.upgradeModuleName == moduleNames.moduleSpiritBreak then
    self.upgradeModuleName = self.upgradeStatus
  end
  self:onFunctionSwicth(self.upgradeModuleName)
  self:setBtnText()
end

function this:initModule(data)
  self.heroGuid = data.heroGuid
  self.spiritGuid = data.spiritGuid
  self.isPreview = data.isPreview
  local serverData = L_SpiritronManager:getSpiritronItem(self.spiritGuid, self.isPreview)
  self.modules.spiritronCardItem:setId(serverData.spiritronId)
  if string.isEmpty(self.upgradeModuleName) then
    if not string.isEmpty(data.jumpMode) then
      self.upgradeModuleName = data.jumpMode
    else
      self.upgradeModuleName = moduleNames.moduleSpiritInfo
    end
  end
  self.bind.detailToggleIsOn = self.upgradeModuleName == moduleNames.moduleSpiritInfo
  self.bind.breakToogleIsOn = self.upgradeModuleName == moduleNames.moduleSpiritBreak or self.upgradeModuleName == moduleNames.moduleSpiritLevelUp
  self.bind.starToggleIsOn = self.upgradeModuleName == moduleNames.moduleSpiritStarUp
  self:refreshStatus()
  self.bind.storyRedActive = not self.isPreview
  if not self.isPreview then
    L_ReddotManager:registerReddot(self.bindComponents.storyRedDot, string.format(L_ReddotManager.DotDef.SpiritSory, serverData.spiritronId))
  end
end

function this:onFunctionSwicth(moduleName)
  if not string.isEmpty(moduleName) then
    self.upgradeModuleName = moduleName
  end
  self:refreshCurrentModule()
  self.bind.moduleSpiritInfoActive = self.upgradeModuleName == moduleNames.moduleSpiritInfo
  self.bind.moduleSpiritBreakActive = self.upgradeModuleName == moduleNames.moduleSpiritBreak
  self.bind.moduleSpiritLevelUpActive = self.upgradeModuleName == moduleNames.moduleSpiritLevelUp
  self.bind.moduleSpiritStarUpActive = self.upgradeModuleName == moduleNames.moduleSpiritStarUp
end

function this:setBtnText()
  self.bind.levelUpIconActive = self.upgradeStatus == moduleNames.moduleSpiritLevelUp
  self.bind.shadowLevelUpIconActive = self.upgradeStatus == moduleNames.moduleSpiritLevelUp
  self.bind.breakIconActive = self.upgradeStatus == moduleNames.moduleSpiritBreak
  self.bind.shadowBreakIconActive = self.upgradeStatus == moduleNames.moduleSpiritBreak
  if self.upgradeStatus == moduleNames.moduleSpiritLevelUp then
    self.bind.breakBtnTitle = L_WordsTpl:getValue("ui_spirit_level_up")
    self.bind.breakBtnActiveTitle = L_WordsTpl:getValue("ui_spirit_level_up")
  end
  if self.upgradeStatus == moduleNames.moduleSpiritBreak then
    self.bind.breakBtnTitle = L_WordsTpl:getValue("ui_spirit_break")
    self.bind.breakBtnActiveTitle = L_WordsTpl:getValue("ui_spirit_break")
  end
end

function this:setLeftAreaActive(active)
  self.bind.leftAreaActive = active
end

return this
