local ProgressBar = require "app/tools/progress_bar"
local BattleBossRushResultUI = class("BattleBossRushResultUI", BaseUI)

local UNIT_RESULT_RERPORT_CELL = "app/ui/battle/cell/unit_result_report_cell"
local MAX_SCROLL_SHOW_COUNT = 10
local SCROLL_LINE_HEIGHT = 130

function BattleBossRushResultUI:getPrefabPath()
    return "assets/prefabs/ui/battle/battle_boss_rush_result_ui.prefab"
end

function BattleBossRushResultUI:ctor(params)
    if EDITOR_MODE then
        Logger.printTable(params)
    end
    -- 通用
    self.battleType = params.battleType
    self.combatReport = params.combatReport
    self.taskProgress = params.taskProgress
    self.totalDmg = 0
    self.atkReport = self.combatReport.atkReport
    if self.atkReport then
        for _, info in ipairs(self.atkReport) do
            self.totalDmg = self.totalDmg + info.dmg
        end
    end
end

function BattleBossRushResultUI:onClose()
    if self.sliderSequence then
        self.sliderSequence:Kill()
        self.sliderSequence = nil
    end
    if self.animNode then
        self.animNode:Kill()
        self.animNode = nil
    end
end

function BattleBossRushResultUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()

    self.continue = uiMap["battle_boss_rush_result_ui.continue"]
    -- 胜利节点
    self.victoryNode = uiMap["battle_boss_rush_result_ui.victory_node"]
    self.victoryMask = uiMap["battle_boss_rush_result_ui.victory_node.mask_v"]
    self.victoryTxTitle = uiMap["battle_boss_rush_result_ui.victory_node.title_bg.desc"]
    self.victorySpine = uiMap["battle_boss_rush_result_ui.victory_node.ui_spine_obj"]
    self.victoryUnitNode = uiMap["battle_boss_rush_result_ui.victory_node.unit_node"]
    self.unitNode = uiMap["battle_boss_rush_result_ui.unit_node"]
    self.unitTxDesc3 = uiMap["battle_boss_rush_result_ui.unit_node.desc_3"]
    if not self.unitResultReportCells then
        self.unitResultReportCells = {}
        for index = 1, 5 do
            self.unitResultReportCells[index] = CellManager:addCellComp(uiMap["battle_boss_rush_result_ui.unit_node.unit_result_report_cell_" .. index], UNIT_RESULT_RERPORT_CELL)
        end
    end

    local iconWave = uiMap["battle_boss_rush_result_ui.unit_node.icon_wave"]
    local iconRound = uiMap["battle_boss_rush_result_ui.unit_node.icon_round"]
    local txWave = uiMap["battle_boss_rush_result_ui.unit_node.desc_wave"]
    local txRound = uiMap["battle_boss_rush_result_ui.unit_node.desc_round"]
    txWave:setText(self.taskProgress[GConst.BattleConst.BATTLE_TASK_FIELD.PASS_WAVE] or 0)
    txRound:setText(self.taskProgress[GConst.BattleConst.BATTLE_TASK_FIELD.TOTAL_TURN] or 0)
    GFunc.centerImgAndTx(iconWave, txWave, 10, -136)
    GFunc.centerImgAndTx(iconRound, txRound, 10, 136)

    local waveNew = uiMap["battle_boss_rush_result_ui.unit_node.desc_new_1"]
    local roundNew = uiMap["battle_boss_rush_result_ui.unit_node.desc_new_2"]
    waveNew:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_28))
    roundNew:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_28))
    local tag1, tag2 = DataManager.ActBossRushData:getWaveRoundNewTag()
    waveNew:setVisible(tag1 == true)
    roundNew:setVisible(tag2 == true)

    self.victoryMask:addClickListener(function()
        self:onClickMask()
    end)
end

function BattleBossRushResultUI:onClickMask()
    ModuleManager.BattleManager:endBattleAndExit()
end

function BattleBossRushResultUI:onRefresh()
    AudioManager:playEffect(AudioManager.EFFECT_ID.BATTLE_VICTORY)
    self:refreshVictoryNode()
    self:refreshUnitInfo()
    self:refreshRewards()
end

function BattleBossRushResultUI:refreshVictoryNode()
    self.victoryNode:setVisible(true)

    self.victoryTxTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_30))
    self.victorySpine:playAnimComplete("born", false, true, function()
        self.victorySpine:playAnim("idle", true, true)
    end)

    self:showNodeAnim(self.victoryUnitNode, self.unitNode)
end

-- 播放节点动画
function BattleBossRushResultUI:showNodeAnim(parent, node)
    if not parent then
        node:setVisible(false)
        return
    else
        node:setVisible(true)
    end
    node:setParent(parent, false)
    node:setAnchoredPosition(0, 0)
    local canvasNode = parent:getComponent(GConst.TYPEOF_UNITY_CLASS.CANVAS_GROUP)

    self.animNode = self.root:createBindTweenSequence()
    self.animNode:Insert(0, canvasNode:DOFade(0, 0))
    self.animNode:Insert(0.1, canvasNode:DOFade(1, 0))
    self.animNode:Insert(0, parent:getTransform():DOScale(0, 0))
    self.animNode:Insert(0.1, parent:getTransform():DOScale(0.35, 0))
    self.animNode:Insert(0.13, parent:getTransform():DOScale(1.1, 0.16))
    self.animNode:Insert(0.26, parent:getTransform():DOScale(1, 0.14))
    self.animNode:OnComplete(function()
        self.animNode = nil
    end)
end

function BattleBossRushResultUI:refreshRewards()
    local uiMap = self.root:genAllChildren()
    uiMap["battle_boss_rush_result_ui.reward_node.reward_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_29))
    local wave = self.taskProgress[GConst.BattleConst.BATTLE_TASK_FIELD.PASS_WAVE] or 0
    uiMap["battle_boss_rush_result_ui.reward_node.score.tx_score"]:setText("+" .. wave)
    local txLastWave = uiMap["battle_boss_rush_result_ui.reward_node.tx_last"]
    local txNextWave = uiMap["battle_boss_rush_result_ui.reward_node.tx_next"]
    local slider = uiMap["battle_boss_rush_result_ui.reward_node.slider"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_SLIDER)
    local sliderText = uiMap["battle_boss_rush_result_ui.reward_node.text"]
    local curWaveLevel = DataManager.ActBossRushData:getWaveLevel()
    local curWaveCount = DataManager.ActBossRushData:getWaveCount()
    local lastLevel, lastCount = DataManager.ActBossRushData:getLastWaveLevelInfo()

    local count = DataManager.ActBossRushData:getLevelWave(lastLevel)
    local lastNeedCount = math.max(DataManager.ActBossRushData:getLevelWave(lastLevel + 1), 0)
    local lastHaveCount = math.max(lastCount - count, 0)
    local oldSliderValue = lastHaveCount / lastNeedCount

    local count = DataManager.ActBossRushData:getLevelWave(curWaveLevel)
    local curNeedCount =  math.max(DataManager.ActBossRushData:getLevelWave(curWaveLevel + 1) - count, 0)
    local curHaveCount = math.max(curWaveCount - count, 0)
    local newSliderValue = curHaveCount / curNeedCount

    if DataManager.ActBossRushData:isMaxWaveLevel() then
        curWaveLevel = curWaveLevel - 1
        newSliderValue = 1
    end

    local params = {}
	params.ui = self
	params.oldLv = lastLevel
	params.newLv = curWaveLevel
	params.beginPer = oldSliderValue
	params.endPer = newSliderValue
	params.time = 0.01
	params.perStep = 0.01
	params.callback = function (curLv, curPer)
		slider.value = curPer
        local nextCount = DataManager.ActBossRushData:getLevelWave(curLv + 1)
        local minCount = DataManager.ActBossRushData:getLevelWave(curLv)
		local ss = minCount
		if nextCount then
			ss = math.floor(curPer * (nextCount - minCount) + 0.0000001)
		end
		sliderText:setText(ss)
        txLastWave:setText(minCount)
        txNextWave:setText(nextCount)
	end
	params.endCallback = function()
        local nextCount = DataManager.ActBossRushData:getLevelWave(curWaveLevel + 1)
        local minCount = DataManager.ActBossRushData:getLevelWave(curWaveLevel)

		local sliderStr = curHaveCount
        if sliderStr > nextCount - minCount then
            sliderStr = nextCount - minCount
        end
		sliderText:setText(sliderStr)


        txLastWave:setText(minCount)
        txNextWave:setText(nextCount)
	end
	local progressBar = ProgressBar:create()
	progressBar:runBar(params)
end

function BattleBossRushResultUI:refreshUnitInfo()
    if not self.atkReport then
        self.unitNode:setVisible(false)
        return
    end

    self.unitNode:setVisible(true)
    for index, cell in ipairs(self.unitResultReportCells) do
        local info = self.atkReport[index]
        cell:getBaseObject():setVisible(info ~= nil)
        if info then
            cell:refresh(info, self.totalDmg)
        end
    end

    self.unitTxDesc3:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_7, GFunc.num2Str(self.totalDmg)))
end

return BattleBossRushResultUI