local this = class("moduleHeroSpiritSwitch", G_UIModuleBase)

function this.bind()
  return {
    spiritList = {
      moduleName = "pages/heroPanel/spirit/cellReplaceSpirit"
    },
    equipedSpirit = {
      moduleName = "pages/bag/moduleBagTip"
    },
    camparedSpirit = {
      moduleName = "pages/bag/moduleBagTip"
    },
    equipedSpiritActive = false,
    camparedSpiritActive = false,
    equipEmptyActive = false,
    selectEmptyActive = false,
    module_sortFilter = L_Const.ModuleInfo.SortFilterModule,
    switchBtnTitle = ""
  }
end

function this.methods()
  return {
    spiritList = {
      onClick_item = function(self, bind)
        self.selectedSpiritGuid = bind.guid
        self:setSpiritListActive()
        self:refreshView()
      end,
      onClick_remove = function(self, bind)
        if not math.isEmpty(self.currentSpiritGuid) then
          L_SpiritronStore:req_equipSpiritron(self.heroGuid, 0, function()
            self.selectedSpiritGuid = self.currentSpiritGuid
            self.currentSpiritGuid = nil
            self:refreshView()
            L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_spirit_get_off"))
          end)
        else
          L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_equip_spirit_tip"))
        end
      end
    },
    onClick_equipedStrengthBtn = function(self)
      L_SpiritronManager:openSpiritUpgrade({
        isPreview = self.isPreview,
        spiritGuid = self.currentSpiritGuid,
        heroGuid = self.heroGuid,
        jumpMode = L_Const.spiritUpgradeModules.moduleSpiritLevelUp
      })
    end,
    onClick_strengthBtn = function(self)
      L_SpiritronManager:openSpiritUpgrade({
        isPreview = self.isPreview,
        spiritGuid = self.selectedSpiritGuid,
        heroGuid = self.heroGuid,
        jumpMode = L_Const.spiritUpgradeModules.moduleSpiritLevelUp
      })
    end,
    onClick_switchBtn = function(self)
      if self.selectedSpiritGuid == self.currentSpiritGuid then
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_spirit_equip_same"))
        return
      end
      if not math.isEmpty(self.selectedSpiritGuid) then
        local serverData = L_SpiritronStore:getSpiritronItem(self.selectedSpiritGuid)
        if not math.isEmpty(serverData.spiritronHero) then
          local item = L_ItemTplManager:getSpiritronItem(serverData.spiritronId)
          local spiritName = item.name
          local heroData = L_HeroStore:getHero(serverData.spiritronHero)
          local heroConfigId = L_HeroStore:getHeroConfigId(heroData)
          local heroItem = L_ItemTplManager:getHeroItem(heroConfigId)
          local tempName = heroItem.name
          if serverData.spiritronHero == L_HeroStore:getDefaultHeroGuid() then
            tempName = L_PlayerStore:getPlayerName()
          end
          local beforeItem
          if not math.isEmpty(self.currentSpiritGuid) then
            beforeItem = {
              itemType = L_Const.resType.spiritron,
              guid = self.currentSpiritGuid
            }
          end
          local afterItem = {
            itemType = L_Const.resType.spiritron,
            guid = self.selectedSpiritGuid
          }
          local tips = L_WordsTpl:getValue("ui_switch_check_tip", {
            [0] = spiritName,
            [1] = tempName
          })
          L_UI:open("pageCommonSwitchTip", {
            txtTitle = L_WordsTpl:getValue("ui_spirit_switch_tip_title"),
            tips = tips,
            confirmCallback = function()
              self:reqReplaceSpiritron()
            end,
            beforeItem = beforeItem,
            afterItem = afterItem,
            emptyTips = L_WordsTpl:getValue("ui_common_switch_empty_tips")
          })
        else
          self:reqReplaceSpiritron()
        end
      end
    end
  }
end

function this:open(options)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.spiritLockChange, self.setLockStatus, self)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.spiritLockStatusChange, self.refreshOneSpirit, self)
  L_SpiritronStore:listenCallFunc(L_SpiritronStore.event.refreshWeapon, self.refreshData, self)
end

function this:close(options)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.spiritLockChange, self.setLockStatus)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.spiritLockStatusChange, self.refreshOneSpirit)
  L_SpiritronStore:unListenCallFunc(L_SpiritronStore.event.refreshWeapon, self.refreshData)
end

function this:setLockStatus(data)
  if data and not math.isEmpty(data.spiritGuid) then
    self.changeLockStatus = true
  else
    self.changeLockStatus = false
  end
end

function this:refreshData()
  if self.changeLockStatus then
    return
  end
  local serverData = L_SpiritronStore:getSpiritronItem(self.selectedSpiritGuid)
  if not serverData then
    self.selectedSpiritGuid = nil
  end
  self:initSpiritList()
  self:refreshView()
end

function this:updateFilter()
  self:initSortFilterModule()
end

function this:initSortFilterModule()
  local callback = function()
    self:initSpiritList()
  end
  self._sortFilter = L_SortFilterManager:createSortFilter(L_SystemConst.enum.spiritSwitchBag, callback, callback)
  self._sortFilter:bindModule(self.modules.module_sortFilter)
end

function this:refreshOneSpirit(data)
  for _, module in ipairs(self.modules.spiritList) do
    if data.spiritGuid == module:getGuid() then
      module:initCell()
    end
  end
  self.changeLockStatus = false
end

function this:reqReplaceSpiritron()
  L_SpiritronStore:req_equipSpiritron(self.heroGuid, self.selectedSpiritGuid, function()
    self.currentSpiritGuid = self.selectedSpiritGuid
    self:refreshView()
  end)
end

function this:refreshView()
  self:refreshSpiritList()
  self:refreshSpiritInfo()
end

function this:initModule(options)
  if not L_UI:checkPageOpen("pageHeroSpiritUpgrade") and self.changeLockStatus then
    return
  end
  self.currentSpiritGuid = options.spiritGuid
  self.selectedSpiritGuid = self.currentSpiritGuid
  self.heroGuid = options.heroGuid
  self:updateFilter()
  self:initSpiritList()
  self:refreshSpiritInfo()
end

function this:refreshSpiritList()
  for i = 1, #self.bind.spiritList do
    self.bind.spiritList:getItemCls(i):refreshView()
  end
end

function this:initSelectSpiritInfo()
  local needShow = not math.isEmpty(self.selectedSpiritGuid) and self.selectedSpiritGuid ~= self.currentSpiritGuid
  self.bind.go_selectInfo = needShow
  self.bind.go_selectInfoEmpty = not needShow
  if needShow then
    local moduleSelectInfo = self.modules.camparedSpirit
    moduleSelectInfo:setData(L_BagConst.bagTag.spiritron, self.selectedSpiritGuid, {hideFuncBtn = true})
  end
end

function this:initSpiritList()
  local spiritronListData = {
    {go_btn_remove = true, go_item = false}
  }
  local spiritList = L_SpiritronStore:getSpiritronList()
  local tempData = {}
  for k, v in pairs(spiritList) do
    local item = L_ItemTplManager:getSpiritronItem(v.spiritronId)
    local data = {
      itemType = L_Const.resType.spiritron,
      guid = v.guid,
      active = v.guid == self.selectedSpiritGuid,
      star = v.star,
      id = v.spiritronId,
      itemId = v.spiritronId,
      quality = item.quality,
      spiritronId = v.spiritronId,
      level = v.spiritronLv
    }
    table.insert(tempData, data)
  end
  self._sortFilter:sortFilterList(tempData)
  table.fill(spiritronListData, tempData)
  if self.selectedSpiritGuid == nil or self.selectedSpiritGuid == 0 and spiritronListData[2] ~= nil then
    self.selectedSpiritGuid = spiritronListData[2].guid
  end
  self.bind.spiritList:clear()
  self.bind.spiritList:insert_array(spiritronListData)
end

function this:refreshSpiritInfo()
  self.bind.equipedSpiritActive = not math.isEmpty(self.currentSpiritGuid)
  self.bind.camparedSpiritActive = not math.isEmpty(self.selectedSpiritGuid) and self.currentSpiritGuid ~= self.selectedSpiritGuid
  self.bind.equipEmptyActive = math.isEmpty(self.currentSpiritGuid)
  self.bind.selectEmptyActive = math.isEmpty(self.selectedSpiritGuid) or self.currentSpiritGuid == self.selectedSpiritGuid
  if self.bind.camparedSpiritActive then
    self.modules.camparedSpirit:reset()
    self.modules.camparedSpirit:setData(L_BagConst.bagTag.spiritron, self.selectedSpiritGuid, {hideFuncBtn = true})
  end
  if self.bind.equipedSpiritActive then
    self.modules.equipedSpirit:reset()
    self.modules.equipedSpirit:setData(L_BagConst.bagTag.spiritron, self.currentSpiritGuid, {hideFuncBtn = true})
  end
  if math.isEmpty(self.currentSpiritGuid) then
    self.bind.switchBtnTitle = L_WordsTpl:getValue("ui_spirit_equip_btn")
  else
    self.bind.switchBtnTitle = L_WordsTpl:getValue("ui_spirit_switch_btn")
  end
  self:setSpiritListActive()
end

function this:setSpiritListActive()
  for _, cell in ipairs(self.modules.spiritList) do
    cell:setActveState(cell.bind.guid == self.selectedSpiritGuid)
  end
end

return this
