local this = class("moduleSpiritStarUp", G_UIModuleBase)

function this.bind()
  return {
    beforeStarList = {
      moduleName = "pages/heroPanel/spirit/cellSpiritStar"
    },
    afterStarList = {
      moduleName = "pages/heroPanel/spirit/cellSpiritStar"
    },
    skillList = {
      moduleName = "pages/heroPanel/spirit/cellSpiritUniqueSkill"
    },
    costList = {
      moduleName = "pages/heroPanel/spirit/cellCostItemSquare"
    },
    filterTypeText = "",
    starUpAreaActive = true,
    maxAreaActive = false,
    maxStarList = {
      moduleName = "pages/heroPanel/spirit/cellSpiritStar"
    },
    maxSkill = {
      moduleName = "pages/heroPanel/spirit/cellSpiritUniqueSkill"
    }
  }
end

function this.methods()
  return {
    onClick_autoBtn = function(self)
      self:autoSelect()
    end,
    onClick_starUp = function(self)
      self:onClickStarUp()
    end
  }
end

function this:initModule(data)
  self.spiritGuid = data.spiritGuid
  self.heroGuid = data.heroGuid
  self.isPreview = data.isPreview
  self.selectItems = {}
  local currSpData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  self.targetStar = currSpData.star + 1
  self.targetStar = math.min(self.targetStar, L_Const.SpiritronFullStar)
  self:refreshStar()
end

function this:onClickStarUp()
  local items, spiritronData = L_SpiritronManager:getAdvancedSelectItem(self.selectItems)
  if #spiritronData == 0 and #items == 0 then
    return L_HeroManager:heroHintInfo(6)
  end
  local data = {
    guid = self.spiritGuid,
    costs = spiritronData,
    cost_items = items
  }
  local showSpiritTip = false
  local spiritronId
  for i = 1, #spiritronData do
    local spirit = L_SpiritronStore:getSpiritronItem(spiritronData[i])
    showSpiritTip = showSpiritTip or 1 < spirit.star
    showSpiritTip = showSpiritTip or 1 < spirit.spiritronLv
    showSpiritTip = showSpiritTip or 0 < spirit.exp
    if showSpiritTip then
      spiritronId = spirit.spiritronId
      break
    end
  end
  if not showSpiritTip then
    if self.starOverFlow then
      L_GameUtil.showCommonTip({
        txtContent = L_WordsTpl:getValue("ui_spirit_star_up_overflow_check"),
        txtTitle = L_WordsTpl:getTplById("notice_title_hint"),
        confirmCallback = function()
          self:spiritronStrength(data)
        end
      })
      return
    else
      self:spiritronStrength(data)
    end
  else
    local item = L_ItemTplManager:getSpiritronItem(spiritronId)
    local tips = L_WordsTpl:getValue("ui_level_up_spirit_tip", {
      [0] = item.name
    })
    local _, afterStar, overflow = L_SpiritronManager:getStarUpSelectItem(self.selectItems, self.spiritGuid)
    if overflow then
      tips = L_WordsTpl:getValue("ui_star_up_spirit_overflow", {
        [0] = item.name,
        [1] = afterStar - L_Const.SpiritronFullStar
      })
    end
    L_GameUtil.showCommonTip({
      txtContent = tips,
      txtTitle = L_WordsTpl:getTplById("notice_title_hint"),
      confirmCallback = function()
        self:spiritronStrength(data)
      end
    })
  end
end

function this:spiritronStrength(data)
  local num = 0
  if data and data.cost_items and data.cost_items[1] then
    num = data.cost_items[1].num
  end
  local params = {
    guid = self.spiritGuid,
    item_num = num,
    guids = data.costs
  }
  local spData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  local curStar = spData.star
  L_SpiritronStore:req_risingStarSpiritron(params, function()
    local currData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
    L_UI:open("pageSpiritStarUpSuccess", {
      spiritGuid = self.spiritGuid,
      curStar = curStar,
      afterStar = currData.star
    })
    self.selectItems = {}
    self.targetStar = currData.star + 1
    self:refreshCost(true)
  end)
end

function this:autoSelect()
  if self.onAuto then
    self.selectItems = {}
    local currSpData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
    self.targetStar = currSpData.star + 1
    self.onAuto = false
    self:refreshCost(true)
    return
  end
  self.onAuto = true
  self.selectItems = L_SpiritronManager:getItemsForAutoStarSelect(self.spiritGuid)
  if #self.selectItems == 0 then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_spirit_auto_empty"))
  end
  self:refreshCost()
end

function this:refreshCost(keepStar)
  local _, afterStar, overflow = L_SpiritronManager:getStarUpSelectItem(self.selectItems, self.spiritGuid)
  self.starOverFlow = overflow
  if not keepStar then
    self.targetStar = afterStar
    local currSpData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
    if self.targetStar == 1 or currSpData.star == self.targetStar then
      self.targetStar = self.targetStar + 1
    end
  end
  self:refreshStar()
end

function this:refreshStar()
  local currSpData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  self.bind.starUpAreaActive = currSpData.star < L_Const.SpiritronFullStar
  self.bind.maxAreaActive = currSpData.star >= L_Const.SpiritronFullStar
  if currSpData.star < L_Const.SpiritronFullStar then
    self:setStarList()
    self:setSkilList()
    self:setCostList()
    if #self.selectItems == 0 then
      self.onAuto = false
    elseif #self.selectItems == 4 then
      self.onAuto = true
    end
    if self.onAuto then
      self.bind.filterTypeText = L_WordsTpl:getValue("ui_spirit_auto_clear")
    else
      self.bind.filterTypeText = L_WordsTpl:getValue("ui_spirit_auto_put")
    end
  else
    self:setMaxUI()
  end
end

function this:setMaxUI()
  local currSpData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  local starData = {}
  for _ = 1, currSpData.star - 1 do
    table.insert(starData, {starActive = true, greyStarActive = false})
  end
  self.bind.maxStarList:clear()
  self.bind.maxStarList:insert_array(starData)
  local headPart = "\227\128\128\227\128\128\227\128\128\227\128\128\227\128\128"
  local currentSKillText = headPart .. L_SpiritronManager:getConfigSkillDesc(currSpData.spiritronId, currSpData.star)
  self.modules.maxSkill:setData({
    skillText = currentSKillText,
    currentText = "",
    starText = L_WordsTpl:getValue("ui_spirit_next_star", {
      [0] = currSpData.star - 1
    }),
    currentBgActive = false,
    nextBgActive = true
  })
end

function this:setStarList()
  local currSpData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  local beforeStarData = {}
  for _ = 1, currSpData.star - 1 do
    table.insert(beforeStarData, {starActive = true, greyStarActive = false})
  end
  if currSpData.star - 1 == 0 then
    table.insert(beforeStarData, {starActive = false, greyStarActive = true})
  end
  local afterStarData = {}
  self.targetStar = math.min(self.targetStar, L_Const.SpiritronFullStar)
  for _ = 1, self.targetStar - 1 do
    table.insert(afterStarData, {starActive = true, greyStarActive = false})
  end
  self.bind.beforeStarList:clear()
  self.bind.afterStarList:clear()
  self.bind.beforeStarList:insert_array(beforeStarData)
  self.bind.afterStarList:insert_array(afterStarData)
end

function this:setSkilList()
  local currSpData = L_SpiritronStore:getSpiritronItem(self.spiritGuid)
  local skillData = {}
  local headPart = "\227\128\128\227\128\128\227\128\128\227\128\128\227\128\128"
  local currentSKillText = headPart .. L_SpiritronManager:getConfigSkillDesc(currSpData.spiritronId, currSpData.star)
  local nextSkillText = headPart .. L_SpiritronManager:getConfigSkillDesc(currSpData.spiritronId, self.targetStar)
  table.insert(skillData, {
    skillText = currentSKillText,
    currentText = L_WordsTpl:getValue("ui_spirit_current_star"),
    currentBgActive = true,
    nextBgActive = false
  })
  table.insert(skillData, {
    skillText = nextSkillText,
    starText = L_WordsTpl:getValue("ui_spirit_next_star", {
      [0] = self.targetStar - 1
    }),
    currentBgActive = false,
    nextBgActive = true
  })
  self.bind.skillList:clear()
  self.bind.skillList:insert_array(skillData)
end

function this:setCostList()
  local data = {}
  for i = 1, 4 do
    if self.selectItems[i] then
      local selectItem = self.selectItems[i]
      local itemNum = 1
      if selectItem.itemType == L_Const.resType.commonItem then
        itemNum = L_BagStore:getItemNum(selectItem.itemId)
      end
      table.insert(data, {
        itemType = selectItem.itemType,
        itemId = selectItem.itemId,
        guid = selectItem.guid,
        selectNum = selectItem.itemSelectNum,
        addBtnActive = false,
        cellIconActive = true,
        bgActive = true,
        params = {
          noTip = true,
          selectNum = selectItem.itemSelectNum,
          selectMode = true,
          mutiSelect = true,
          validateSelectFunc = function(beforeNum, afterNum)
            if afterNum <= 0 then
              table.remove(self.selectItems, i)
              self:refreshCost()
              return false
            end
            if afterNum > itemNum then
              return false
            end
            local isReduce = afterNum < selectItem.itemSelectNum
            local _, afterStar, _ = L_SpiritronManager:getStarUpSelectItem(self.selectItems, self.spiritGuid)
            if afterStar >= L_Const.SpiritronFullStar and not isReduce then
              L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_spirit_star_limit"))
              return false
            end
            selectItem.itemSelectNum = afterNum
            if self.starOverFlow and not isReduce then
              L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_spirit_star_limit"))
              selectItem.itemSelectNum = afterNum - 1
              return false
            end
            if isReduce then
              self:refreshCost()
            end
            return true
          end,
          itemNum = itemNum,
          callback = function()
            self:refreshCost()
          end
        }
      })
    else
      table.insert(data, {
        addBtnActive = true,
        cellIconActive = false,
        bgActive = false,
        onAddClick = function()
          L_UI:open("pageSpiritMaterialBag", {
            bagType = L_Const.spiritMaterialBagType.star,
            pageCloseCallback = function()
              self:onBagClose()
            end,
            onSelectItem = function(passData)
              self:onSelectItem(passData)
            end,
            selectItems = self.selectItems,
            spiritGuid = self.spiritGuid
          })
          self.parent:setLeftAreaActive(false)
        end
      })
    end
  end
  if #self.bind.costList == #data then
    for i = 1, #self.bind.costList do
      self.bind.costList:change(i, data[i])
    end
  else
    self.bind.costList:clear()
    self.bind.costList:insert_array(data)
  end
end

function this:onBagClose()
  self.parent:setLeftAreaActive(true)
end

function this:onSelectItem(passData)
  self.selectItems = passData
  self:refreshCost()
end

return this
