local this = class("moduleAccessoryShowSpirit", G_UIModuleBase)
local _spiritronTpl = L_GameTpl:getSpiritronTpl()
local quality_color = L_Const.accessoryQualityColor

function this.bind()
  return {
    spiritNullActive = false,
    spiritPossessActive = false,
    txt_currLv = "",
    color_split = C_Color(1, 1, 1, 1),
    txt_maxLv = "",
    txt_dec_e = "",
    go_star = false,
    go_selected = false,
    go_tipNode = false,
    module_spiritInfo = {
      moduleName = "pages/bag/moduleBagTip"
    },
    go_model = ""
  }
end

function this.methods()
  return {
    onClick_addSpirit = function(self)
      L_SpiritronManager:openSpiritSwitch({
        spiritGuid = self.spiritGuid,
        heroGuid = self.heroGuid
      })
    end,
    onClick_maxBtn = function(self)
      if self.bind.spiritPossessActive then
        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
    end,
    onClick_viewSpiritBg = function(self)
      self:showSpiritTip(true)
    end,
    onClick_spiritStrength = function(self)
      self:showSpiritTip(false)
      L_SpiritronManager:openSpiritUpgrade({
        isPreview = self.isPreview,
        spiritGuid = self.spiritGuid,
        heroGuid = self.heroGuid,
        jumpMode = L_Const.spiritUpgradeModules.moduleSpiritLevelUp
      })
    end,
    onClick_spiritReplace = function(self)
      self:showSpiritTip(false)
      L_SpiritronManager:openSpiritSwitch({
        spiritGuid = self.spiritGuid,
        heroGuid = self.heroGuid
      })
    end,
    onClick_lock = function(self)
      self.isEnterLock = true
    end
  }
end

function this:initModule(data)
  data = data or {
    spiritGuid = self.spiritGuid,
    heroGuid = self.heroGuid
  }
  L_ReddotManager:registerReddot(self.bindComponents.reddotNormal, string.format(L_ReddotManager.DotDef.HeroSpirit, data.heroGuid))
  if data.spiritGuid == nil then
    return
  end
  self.spiritGuid = data.spiritGuid
  self.heroGuid = data.heroGuid
  self.bind.spiritNullActive = math.isEmpty(self.spiritGuid)
  self.bind.spiritPossessActive = not math.isEmpty(self.spiritGuid)
  if not self.bind.spiritPossessActive then
    return
  end
  self:initSpiritPossess()
end

function this:initSpiritPossess()
  local spiritItem = L_SpiritronStore:getSpiritronItem(self.spiritGuid, true)
  self.bind.go_star = true
  local path = string.format("UI/Texture/AccessoryRank/tex_equipment_bg_lzstar%d.png", spiritItem.star - 1)
  self.bindComponents.img_star:LoadSprite(path, false)
  local spiritronCfg = _spiritronTpl:getTplById(spiritItem.spiritronId)
  local rarity = _spiritronTpl:getRarity(spiritronCfg)
  self.bind.txt_currLv = L_GameUtil.fillColor(tostring(spiritItem.spiritronLv), quality_color[rarity])
  local _, color = C_ColorUtility.TryParseHtmlString(quality_color[rarity])
  self.bind.color_split = color
  self.bind.txt_maxLv = L_GameUtil.fillColor(tostring(L_SpiritronManager:getMaxLevel(self.spiritGuid, false)), quality_color[rarity])
  self.bind.txt_dec_e = L_GameUtil.fillColor("LEVEL", quality_color[rarity])
  if spiritronCfg then
    local path = _spiritronTpl:getUIPath(spiritronCfg)
    if self.curPath ~= path then
      self.curPath = path
      self.bind.go_model = path
    end
  end
end

function this:initStarList(starNum)
  if not self.isBind then
    return
  end
  local stars = {}
  for i = 1, starNum do
    table.insert(stars, {})
  end
  self.bind.list_star:clear()
  self.bind.list_star:insert_array(stars)
end

function this:showSpiritTip(bShow)
  if self.bind.go_tipNode == bShow then
    return
  end
  self.bind.go_selected = bShow
  self.bind.go_tipNode = bShow
  if self.bind.go_tipNode and self.bind.spiritPossessActive then
    self.modules.module_spiritInfo:reset()
    self.modules.module_spiritInfo:setData(L_BagConst.bagTag.spiritron, self.spiritGuid, {hideFuncBtn = true})
  end
end

function this:changeLockStatus()
  self.isEnterLock = false
end

function this:open()
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.refreshWeapon, self.initModule, self)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.switchSpirit, self.initModule, self)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.spiritLockStatusChange, self.changeLockStatus, self)
end

function this:close()
  self.curPath = nil
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.refreshWeapon, self.initModule)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.switchSpirit, self.initModule)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.spiritLockStatusChange, self.changeLockStatus)
end

return this
