local DungeonDifficultyUI = class("DungeonDifficultyUI", BaseUI)

function DungeonDifficultyUI:isFullScreen()
    return  false
end

function DungeonDifficultyUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon/dungeon_difficulty_ui.prefab"
end

function DungeonDifficultyUI:ctor(params)
    self.module = params.module
    self.curId = DataManager.DungeonData:getUnlockMaxId(self.module)
end

function DungeonDifficultyUI:onCover()
end

function DungeonDifficultyUI:onReshow()
end

function DungeonDifficultyUI:onClose()
end

function DungeonDifficultyUI:onLoadRootComplete()
    self.uiMap = self.root:genAllChildren()

    self.icon = self.uiMap["dungeon_difficulty_ui.bg.title.icon_title"]
    self.btnClose = self.uiMap["dungeon_difficulty_ui.bg.close_btn"]
    self.btnStart = self.uiMap["dungeon_difficulty_ui.bg.btns.btn_start"]
    self.txStart = self.uiMap["dungeon_difficulty_ui.bg.btns.btn_start.tx_start"]
    self.txStartCost = self.uiMap["dungeon_difficulty_ui.bg.btns.btn_start.cost.tx_cost"]
    self.btnSweep = self.uiMap["dungeon_difficulty_ui.bg.btns.btn_sweep"]
    self.txSweep = self.uiMap["dungeon_difficulty_ui.bg.btns.btn_sweep.tx_sweep"]
    self.txSweepCost = self.uiMap["dungeon_difficulty_ui.bg.btns.btn_sweep.cost.tx_cost"]
    self.txTime = self.uiMap["dungeon_difficulty_ui.bg.btns.tx_time"]
    self.txDifficulty = self.uiMap["dungeon_difficulty_ui.bg.select.chapter.tx_difficulty"]
    self.txLevel = self.uiMap["dungeon_difficulty_ui.bg.select.chapter.tx_level"]
    self.arrowLeft = self.uiMap["dungeon_difficulty_ui.bg.select.chapter.arrow_left"]
    self.arrowRight = self.uiMap["dungeon_difficulty_ui.bg.select.chapter.arrow_right"]
    self.txTitle = self.uiMap["dungeon_difficulty_ui.bg.title.title_text"]
    self.buffObj = self.uiMap["dungeon_difficulty_ui.bg.boss_buff"]
    self.txbuff = self.uiMap["dungeon_difficulty_ui.bg.boss_buff.tx_buff"]
    self.iconBuff = self.uiMap["dungeon_difficulty_ui.bg.boss_buff.icon_buff"]
    self.rewardTx = self.uiMap["dungeon_difficulty_ui.bg.reward_desc"]
    self.rewardFirst = self.uiMap["dungeon_difficulty_ui.bg.rewards.reward_first"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    self.rewardPass = self.uiMap["dungeon_difficulty_ui.bg.rewards.reward_pass"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)
    self.txFrist = self.uiMap["dungeon_difficulty_ui.bg.rewards.reward_first.first.tx_first"]
    self.Frist = self.uiMap["dungeon_difficulty_ui.bg.rewards.reward_first.first"]
    self.txLock = self.uiMap["dungeon_difficulty_ui.bg.tx_lock"]
    self.btnGroup = self.uiMap["dungeon_difficulty_ui.bg.btns"]

    local iconPath  = DataManager.DungeonData:getIcon(self.module)
    self.icon:setSprite(iconPath[1], iconPath[2], function()
        self.icon:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_IMAGE):SetNativeSize()
    end)
    self.txTitle:setText(DataManager.DungeonData:getTitle(self.module))
    self.txDifficulty:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_STAGE))
    self.txStart:setText(I18N:getGlobalText(I18N.GlobalConst.START_DESC))
    self.txSweep:setText(I18N:getGlobalText(I18N.GlobalConst.SMASH))
    local cost = DataManager.DungeonData:getChallengeHpCost(self.module)
    self.txStartCost:setText(GFunc.getRewardNum(cost))
    self.txSweepCost:setText(GFunc.getRewardNum(cost))
    self.txFrist:setText(I18N:getGlobalText(I18N.GlobalConst.FIRST_PASS))
    self.rewardTx:setText(I18N:getGlobalText(I18N.GlobalConst.REWARD_DESC))
    self.txLock:setText(I18N:getGlobalText(I18N.GlobalConst.PASS_REQUIRE))

    -- 背景自适应文本
    self.txFrist:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO):ForceMeshUpdate()
    local txW = self.txFrist:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).renderedWidth
    self.Frist:setSizeDeltaX(txW + 20)

    self:refreshDifficulty()

    self.btnClose:addClickListener(function()
        self:closeUI()
	end)
    self.btnStart:addClickListener(function()
        -- 开始挑战
        ModuleManager.DungeonManager:reqChallenge(self.module, self.curId)
	end)
    self.btnSweep:addClickListener(function()
        -- 开始扫荡
        ModuleManager.DungeonManager:reqSweep(self.module, self.curId)
	end)
    self.arrowLeft:addClickListener(function()
        if self:isMinId() then
            return
        end

        self.curId = self.curId - 1
        self:refreshDifficulty()
	end)
    self.arrowRight:addClickListener(function()
        if self:isMaxId() then
            return
        end

        self.curId = self.curId + 1
        self:refreshDifficulty()
	end)

    self:addEventListener(EventManager.CUSTOM_EVENT.DUNGEON_DAY_CHANGE, function()
        self:closeUI()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.DUNGEON_CHALLENGE, function()
        self:closeUI()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.DUNGEON_SWEEP, function()
        self:refreshDifficulty()
    end)
end

function DungeonDifficultyUI:refreshDifficulty()
    self.btnSweep:setActive(self:isCanSweepId())
    self.txLevel:setText(tostring(self.curId))

    -- buff
    local buff = DataManager.DungeonData:getBossBuff(self.module, self.curId)
    if buff then
        local cfg = ConfigManager:getConfigWithOtherKey("buff", "name")[buff.type]
        self.buffObj:setActive(true)
        self.txbuff:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_DESC))
        self.iconBuff:setSprite(GConst.ATLAS_PATH.ICON_BUFF, cfg.icon)
        self.iconBuff:addClickListener(function()
            ModuleManager.TipsManager:showDescTips(GFunc.getBuffDesc(buff.type, buff.num), self.iconBuff)
        end)

        GFunc.centerTxAndImg(self.txbuff, self.iconBuff, 5)
    else
        self.buffObj:setActive(false)
    end

    -- 箭头
    self.txLock:setActive(not self:isCanChallengeId())
    self.btnGroup:setActive(self:isCanChallengeId())
    self.arrowRight:setActive(not self:isMaxId())
    self.arrowLeft:setActive(not self:isMinId())

    -- 奖励
    local id = DataManager.DungeonData:getBoardShowRewardId(self.module)
    local firstNum = DataManager.DungeonData:getFirstRewardNum(self.module, self.curId)
    firstNum = firstNum or 0
    local passNum = DataManager.DungeonData:getPassRewardNum(self.module, self.curId)
    passNum = passNum or 0

    self.rewardFirst:refreshItemById(id, firstNum)
    self.rewardPass:refreshItemById(id, passNum)
    self.rewardFirst:getBaseObject():setActive((not self:isCanSweepId()) and firstNum)

    -- 次数
    local time = DataManager.DungeonData:getRemainTimes(self.module)
    local timeStr = nil
    if time <= 0 then
        timeStr = "<color=#FF8181>" .. time .. "</color>"
    else
        timeStr = time
    end
    self.txTime:setText(I18N:getGlobalText(I18N.GlobalConst.TODAY_REMAIN_TIMES, timeStr))
end

-- 是否是可扫荡关卡
function DungeonDifficultyUI:isCanSweepId()
    return self.curId <= DataManager.DungeonData:getPassedMaxId(self.module)
end

--是否是可挑战关卡
function DungeonDifficultyUI:isCanChallengeId()
    return self.curId <= DataManager.DungeonData:getUnlockMaxId(self.module)
end

-- 是最大副本id
function DungeonDifficultyUI:isMaxId()
    return self.curId == DataManager.DungeonData:getConfigMaxId(self.module)
end

--是最小关卡
function DungeonDifficultyUI:isMinId()
    return self.curId == 1
end

return DungeonDifficultyUI