local DungeonBoardCell = class("DungeonBoardCell", BaseCell)

local HIDE_REWARD_MODULE = {
    [ModuleManager.MODULE_KEY.DUNGEON_WEAPON] = true,
    [ModuleManager.MODULE_KEY.DUNGEON_ARMOR] = true,
    [ModuleManager.MODULE_KEY.RUNES_OPEN] = true,
}

function DungeonBoardCell:init()
    self.uiMap = self:getUIMap()

    self.txTitle = self.uiMap["dungeon_board_cell.tx_title"]
    self.countdown = self.uiMap["dungeon_board_cell.countdown"]
    self.txCountdown = self.uiMap["dungeon_board_cell.countdown.tx_countdown"]
    self.txOpen = self.uiMap["dungeon_board_cell.tx_open"]
    self.lock = self.uiMap["dungeon_board_cell.lock"]
    self.lockIcon = self.uiMap["dungeon_board_cell.lock.desc.icon"]
    self.lockTxLock = self.uiMap["dungeon_board_cell.lock.desc.tx_lock"]
    self.lockIconCountdown = self.uiMap["dungeon_board_cell.lock.countdown.icon"]
    self.lockTxCountdown = self.uiMap["dungeon_board_cell.lock.countdown.tx_countdown"]
    self.btnStart = self.uiMap["dungeon_board_cell.btn_start"]
    self.txStart = self.uiMap["dungeon_board_cell.btn_start.tx_btn"]
    self.txTimes = self.uiMap["dungeon_board_cell.btn_start.tx_times"]
    self.btnHelp = self.uiMap["dungeon_board_cell.btn_help"]
    self.itemReward = self.uiMap["dungeon_board_cell.reward_cell"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL)

    self.btnStart:addClickListener(function()
        DataManager.DungeonData:onClickFight(self.moduleKey)
	end)
    self.btnHelp:addClickListener(function()
        -- 展示提示
        ModuleManager.TipsManager:showDescTips(DataManager.DungeonData:getRule(self.moduleKey), self.btnHelp)
	end)
end

function DungeonBoardCell:refresh(moduleKey)
    self.moduleKey = moduleKey

    self:getGameObject().name = self.moduleKey
    self:refreshInfo()
    self:refreshRewards()
end

function DungeonBoardCell:refreshInfo()
    self.txTitle:setText(DataManager.DungeonData:getTitle(self.moduleKey))
    self.txOpen:setText("<color=" .. DataManager.DungeonData:getOpenTextColor(self.moduleKey) .. ">" .. DataManager.DungeonData:getOpenTimeDesc(self.moduleKey) .. "</color>")
    self.baseObject:setTexture(DataManager.DungeonData:getBanner(self.moduleKey))    

    if DataManager.DungeonData:isActive(self.moduleKey) then
        self.btnStart:setVisible(true)
        self.countdown:setVisible(true)
        self.lock:setVisible(false)

        -- 红点
        if DataManager.DungeonData:isCanChallenge(self.moduleKey) then
            self.btnStart:addRedPoint(110, 40, 0.6)
        else
            self.btnStart:removeRedPoint()
        end

        self.txOpen:setAnchoredPositionY(-110)
        self.txStart:setText(I18N:getGlobalText(I18N.GlobalConst.START_DESC))
        if self.moduleKey == ModuleManager.MODULE_KEY.DUNGEON_WEAPON then
            local weaponData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_WEAPON)
            local time = weaponData:getRemianFarmCount()
            local timeStr = nil
            if time <= 0 then
                timeStr = "<color=#FF6464>" .. time .. "</color>"
            else
                timeStr = time
            end
            self.txTimes:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_WEAPON_DESC_5, timeStr))
        else
            local time = DataManager.DungeonData:getRemainTimes(self.moduleKey)
            local timeStr = nil
            if time <= 0 then
                timeStr = "<color=#FF6464>" .. time .. "</color>"
            else
                timeStr = time
            end
            self.txTimes:setText(I18N:getGlobalText(I18N.GlobalConst.TODAY_REMAIN_TIMES, timeStr))
        end

        if DataManager.DungeonData:isNotShowLimitCount(self.moduleKey) then
            self.txTimes:setText(GConst.EMPTY_STRING)
        end

        self:refreshCountdown(self.txCountdown)
    else
        self.btnStart:setVisible(false)
        self.countdown:setVisible(false)
        self.lock:setVisible(true)

        self.txOpen:setAnchoredPositionY(-75)
        self.lockTxLock:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_OPEN))
        self:refreshCountdown(self.lockTxCountdown)
    end

    GFunc.centerImgAndTx(self.lockIcon, self.lockTxLock, 10)
    GFunc.centerImgAndTx(self.lockIconCountdown, self.lockTxCountdown, 10)
end

function DungeonBoardCell:refreshRewards()
    if HIDE_REWARD_MODULE[self.moduleKey] then
        self.itemReward:getBaseObject():setActive(false)
    else
        self.itemReward:getBaseObject():setActive(true)
        local id = DataManager.DungeonData:getBoardShowRewardId(self.moduleKey)
        self.itemReward:refreshItemById(id, 0)
    end
end

function DungeonBoardCell:refreshCountdown(txCountdown)
    if self.countdownSid then
        self:getBaseObject():unscheduleGlobal(self.countdownSid)
        self.countdownSid = nil
    end
    if DataManager.DungeonData:getIsAllTimeOpen(self.moduleKey) then
        self.countdown:setVisible(false)
    else
        self.countdownSid = self:getBaseObject():scheduleGlobal(function()
            self:updateTime(txCountdown)
        end, 1)
        self:updateTime(txCountdown)
    end
end

function DungeonBoardCell:updateTime(txCountdown)
    if self.remainTime == nil or self.remainTime < 0 then
        self.remainTime = DataManager.DungeonData:geNextTime(self.moduleKey)
    else
        self.remainTime = self.remainTime - 1
    end

	txCountdown:setText(GFunc.getTimeStrWithHMS(self.remainTime))
end

return DungeonBoardCell