local DungeonRuneFightUI = class("DungeonRuneFightUI", BaseUI)

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

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

function DungeonRuneFightUI:isFullScreen()
    return false
end

function DungeonRuneFightUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_rune/dungeon_rune_fight_ui.prefab"
end

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

    self.id = params.id
end

function DungeonRuneFightUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()
end

function DungeonRuneFightUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_rune_fight_ui.bg.title.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_6, self.id))
    uiMap["dungeon_rune_fight_ui.bg.desc_1"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_1))
    self:refreshTargetInfo()
    self:refreshRewardInfo()
    self:refreshFightBtn()
end

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

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

function DungeonRuneFightUI:refreshTargetInfo()
    local uiMap = self.root:genAllChildren()
    if not self.taskRuneCells then
        self.taskRuneCells = {}
        for i = 1, 4 do
            self.taskRuneCells[i] = CellManager:addCellComp(uiMap["dungeon_rune_fight_ui.bg.rune_task_cell_" .. i], RUNE_TASK_CELL)
        end
    end

    local chapterCondition = self.runeData:getChapterCondition(self.id)
    for index, cell in ipairs(self.taskRuneCells) do
        local info = chapterCondition[index]
        if info then
            cell:refresh(info)
            cell:getBaseObject():setActive(true)
            cell:addClickListener(function()
                ModuleManager.TipsManager:showDescTips(self.runeData:getConditionDesc(info), cell:getBaseObject())
            end)
        else
            cell:getBaseObject():setActive(false)
        end
    end
    uiMap["dungeon_rune_fight_ui.bg.task_node"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT):RefreshLayout()
end

function DungeonRuneFightUI:refreshRewardInfo()
    local uiMap = self.root:genAllChildren()
    local passed = self.runeData:passedChapter(self.id)
    local descPassRound = uiMap["dungeon_rune_fight_ui.bg.desc_3"]
    local imgPassIcon = uiMap["dungeon_rune_fight_ui.bg.icon"]
    local rewardNode = uiMap["dungeon_rune_fight_ui.bg.reward_node"]
    local lineR = uiMap["dungeon_rune_fight_ui.bg.line_r"]
    local lineL = uiMap["dungeon_rune_fight_ui.bg.line_l"]
    rewardNode:setActive(not passed)
    descPassRound:setVisible(passed)
    imgPassIcon:setVisible(passed)
    if passed then
        descPassRound:setVisible(false)
        imgPassIcon:setVisible(false)
        -- local round = self.runeData:getChapterPassRound(self.id)
        -- descPassRound:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_MIN, round))
        -- GFunc.centerImgAndTx(imgPassIcon, descPassRound, 5)
        lineR:setVisible(false)
        lineL:setVisible(false)
    else
        lineR:setVisible(true)
        lineL:setVisible(true)
        if not self.rewardCells then
            self.rewardCells = {}
            for i = 1, 4 do
                self.rewardCells[i] = CellManager:addCellComp(uiMap["dungeon_rune_fight_ui.bg.reward_node.reward_cell_" .. i], GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
            end
        end

        local rewards = self.runeData:getChapterRewards(self.id)
        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
end

function DungeonRuneFightUI:refreshFightBtn()
    local uiMap = self.root:genAllChildren()
    local title = uiMap["dungeon_rune_fight_ui.bg.desc_2"]

    local iconBg = uiMap["dungeon_rune_fight_ui.bg.btn.icon_bg"]
    local icon = uiMap["dungeon_rune_fight_ui.bg.btn.icon"]
    local num = uiMap["dungeon_rune_fight_ui.bg.btn.num"]
    local desc = uiMap["dungeon_rune_fight_ui.bg.btn.desc"]

    local descAgain = uiMap["dungeon_rune_fight_ui.bg.btn.desc_again"]
    local passed = self.runeData:passedChapter(self.id)
    local notPass = not passed
    iconBg:setVisible(notPass)
    icon:setVisible(notPass)
    num:setVisible(notPass)
    desc:setVisible(notPass)
    descAgain:setVisible(passed)
    if passed then
        descAgain:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_2))
        -- title:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_6))
        title:setText(GConst.EMPTY_STRING)
    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)
        title:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_RUNE_DESC_3))
    end
end

return DungeonRuneFightUI