local DungeonRuneBossFightUI = class("DungeonRuneBossFightUI", BaseUI)

local RUNE_TASK_CELL = "app/ui/dungeon_rune/cell/rune_task_cell"

function DungeonRuneBossFightUI:onPressBackspace()
    self:closeUI()
end

function DungeonRuneBossFightUI:isFullScreen()
    return false
end

function DungeonRuneBossFightUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_rune/dungeon_rune_boss_fight_ui.prefab"
end

function DungeonRuneBossFightUI:ctor(params)
    self.runeData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.RUNES_OPEN)

    self.id = params.id
end

function DungeonRuneBossFightUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
    self:_bind()
end

function DungeonRuneBossFightUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_rune_boss_fight_ui.bg.title.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_6, self.id))
    uiMap["dungeon_rune_boss_fight_ui.bg.btn_again.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_2))
    local spineName, spineScale = self.runeData:getChapterMonsterSpine(self.id)
    local spineObject = uiMap["dungeon_rune_boss_fight_ui.bg.banner.ui_spine_obj"]
    spineObject:setVisible(false)
    spineObject:loadAssetAsync(spineName, function()
        spineObject:playAnim("idle", true, true, true)
        spineObject:setLocalScale(spineScale, spineScale, spineScale)
        spineObject:setVisible(true)
    end, true)

    local i18Config = self.runeData:getChapterMonsterI18N(self.id)
    uiMap["dungeon_rune_boss_fight_ui.bg.desc_1"]:setText(i18Config.name)
    uiMap["dungeon_rune_boss_fight_ui.bg.desc_2"]:setText(i18Config.desc)

    self:refreshTargetInfo()
    self:refreshRewardInfo()
    self:refreshFightBtn()
end

function DungeonRuneBossFightUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_rune_boss_fight_ui.bg.btn_close"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["dungeon_rune_boss_fight_ui.bg.btn"]:addClickListener(function()
        local passed = self.runeData:passedChapter(self.id)
        if passed then
            ModuleManager.DungeonRuneManager:reqSweep(self.id)
        else
            ModuleManager.DungeonRuneManager:reqFight(self.id)
        end
    end)

    uiMap["dungeon_rune_boss_fight_ui.bg.btn_again"]:addClickListener(function()
        ModuleManager.DungeonRuneManager:reqFight(self.id)
    end)
end

function DungeonRuneBossFightUI:_bind()
    self:bind(self.runeData, "isDirty", function()
        self:refreshFightBtn()
    end)
end

function DungeonRuneBossFightUI:refreshTargetInfo()
    local uiMap = self.root:genAllChildren()
    if not self.taskRuneCell then
        self.taskRuneCell = CellManager:addCellComp(uiMap["dungeon_rune_boss_fight_ui.bg.banner.rune_task_cell"], RUNE_TASK_CELL)
    end

    local chapterCondition = self.runeData:getChapterCondition(self.id)
    if chapterCondition[1] then
        self.taskRuneCell:refresh(chapterCondition[1])
        self.taskRuneCell:addClickListener(function()
            ModuleManager.TipsManager:showDescTips(self.runeData:getConditionDesc(chapterCondition[1]), self.taskRuneCell:getBaseObject())
        end)
    end
end

function DungeonRuneBossFightUI:refreshRewardInfo()
    local uiMap = self.root:genAllChildren()
    local passed = self.runeData:passedChapter(self.id)
    local title = uiMap["dungeon_rune_boss_fight_ui.bg.desc_3"]

    local descPassRound = uiMap["dungeon_rune_boss_fight_ui.bg.desc_4"]
    local imgPassIcon = uiMap["dungeon_rune_boss_fight_ui.bg.icon"]
    local rewardNode = uiMap["dungeon_rune_boss_fight_ui.bg.reward_node"]

    if not self.rewardCells then
        self.rewardCells = {}
        for i = 1, 4 do
            self.rewardCells[i] = CellManager:addCellComp(uiMap["dungeon_rune_boss_fight_ui.bg.reward_node.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
        end
    end

    local rewards = self.runeData:getChapterRewards(self.id)
    if passed then
        title:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_4))
        -- local round = self.runeData:getChapterPassRound(self.id)
        imgPassIcon:setActive(false)
        descPassRound:setText(GConst.EMPTY_STRING)
        descPassRound:setAnchoredPositionX(0)
        -- descPassRound:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_MIN, round))
        -- imgPassIcon:setActive(true)
        GFunc.centerImgAndTx(imgPassIcon, descPassRound, 5)
        rewards = self.runeData:getChapterSweepRewards(self.id)
    else
        imgPassIcon:setActive(false)
        descPassRound:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_5))
        descPassRound:setAnchoredPositionX(0)
        title:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_3))
    end

    for index, cell in ipairs(self.rewardCells) do
        local reward = rewards[index]
        if reward then
            cell:refreshByConfig(reward)
            cell:getBaseObject():setActive(true)
        else
            cell:getBaseObject():setActive(false)
        end
    end

    rewardNode:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
end

function DungeonRuneBossFightUI:refreshFightBtn()
    local uiMap = self.root:genAllChildren()
    local btn = uiMap["dungeon_rune_boss_fight_ui.bg.btn"]
    local btnAgain = uiMap["dungeon_rune_boss_fight_ui.bg.btn_again"]

    local icon = uiMap["dungeon_rune_boss_fight_ui.bg.btn.icon"]
    local num = uiMap["dungeon_rune_boss_fight_ui.bg.btn.num"]
    local desc = uiMap["dungeon_rune_boss_fight_ui.bg.btn.desc"]

    local passed = self.runeData:passedChapter(self.id)
    if passed then
        desc:setText(I18N:getGlobalText(I18N.GlobalConst.SMASH))
        num:setText(1)
        icon:setSprite(ModuleManager.ItemManager:getItemIcon(GConst.ItemConst.ITEM_ID_SLIVER_WING))
        GFunc.centerImgAndTx(icon, num, 5)
        btn:setAnchoredPositionX(130)
        btnAgain:setActive(true)
        btn:setLocalScale(0.9, 0.9, 0.9)
    else
        desc:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_CHALLENGE))
        num:setText(1)
        icon:setSprite(ModuleManager.ItemManager:getItemIcon(GConst.ItemConst.ITEM_ID_GLOD_WING))
        GFunc.centerImgAndTx(icon, num, 5)
        btn:setAnchoredPositionX(0)
        btnAgain:setActive(false)
        btn:setLocalScale(1, 1, 1)
    end
end

return DungeonRuneBossFightUI