local DungeonArmorStarRewardUI = class("DungeonArmorStarRewardUI", BaseUI)

local STAR_REWARD_CELL = "app/ui/dungeon_armor/cell/star_reward_cell"

function DungeonArmorStarRewardUI:ctor()
    self.armorData = DataManager.DungeonData:getDungeonDataByType(ModuleManager.MODULE_KEY.DUNGEON_ARMOR)
    self.starList = {}
    for id, info in ipairs(ConfigManager:getConfig("chapter_dungeon_armor_reward")) do
        table.insert(self.starList, info)
    end

    local id, progress, totalProgress = self.armorData:getMinStarTarget()
    self.targetId = id
end

function DungeonArmorStarRewardUI:isFullScreen()
    return false
end

function DungeonArmorStarRewardUI:getPrefabPath()
    return "assets/prefabs/ui/dungeon_armor/dungeon_armor_star_reward_ui.prefab"
end

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

function DungeonArmorStarRewardUI:_display()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_armor_star_reward_ui.bg.title.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_8))

    self.allStarNum = self.armorData:getAllStarNum()
    uiMap["dungeon_armor_star_reward_ui.bg.bg.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_9, self.allStarNum))
    uiMap["dungeon_armor_star_reward_ui.bg.bg.btn.desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.DUNGEON_ARMOR_DESC_11))

    self:refreshScrollrect()
end

function DungeonArmorStarRewardUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["dungeon_armor_star_reward_ui.bg.bg.btn"]:addClickListener(function()
        EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.DUNGEON_ARMOR_TO_TARGET_ID, {id = self.armorData:getMinLowThreeStarId()})
        self:closeUI()
    end)

    uiMap["dungeon_armor_star_reward_ui.bg.btn_close"]:addClickListener(function()
        self:closeUI()
    end)
end

function DungeonArmorStarRewardUI:_bind()
    self:bind(self.armorData, "isDirty", function()
        self:_display()
    end)
end

function DungeonArmorStarRewardUI:refreshScrollrect()
    if self.scrollRect then
        self.scrollRect:updateAllCell()
        return
    end
    local uiMap = self.root:genAllChildren()
	self.scrollRect = uiMap["dungeon_armor_star_reward_ui.bg.bg.scrollrect"]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.SCROLL_RECT_BASE)
    self.scrollRect:addInitCallback(function()
        return STAR_REWARD_CELL
    end)
    self.scrollRect:addRefreshCallback(function(index, cell)
        local got = self.armorData:getStarRewardGot(index)
        cell:refresh(index, self.starList[index], got, self.allStarNum)
    end)
    self.scrollRect:clearCells()
    self.scrollRect:refillCells(#self.starList, nil, self.targetId)
end

return DungeonArmorStarRewardUI