local SummonOddsUI = class("SummonOddsUI", BaseUI)

local UIPrefabObject = require "app/bf/unity/uiprefab_object"
local SummonHeroCell = "app/ui/summon/cell/summon_hero_cell"

function SummonOddsUI:isFullScreen()
    return false
end

function SummonOddsUI:ctor(params)
	self.page = params.summonId or 1
end

function SummonOddsUI:getPrefabPath()
    return "assets/prefabs/ui/summon/summon_odds_ui.prefab"
end

function SummonOddsUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    uiMap["summon_odds_ui.bg.tx_title"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_4))
    if self.page == 1 then
        uiMap["summon_odds_ui.bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_5))
    else
        uiMap["summon_odds_ui.bg.desc_tx"]:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_3))
    end

    self.content = uiMap["summon_odds_ui.bg.target_node.bg.scrollrect.viewport.content"]
    self.layout_content = self.content:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
    self.title = uiMap["summon_odds_ui.bg.target_node.bg.scrollrect.viewport.content.title"]
    self.summonHeroCell = uiMap["summon_odds_ui.bg.scrollrect.viewport.content.summon_hero_cell"]
    self.layout = uiMap["summon_odds_ui.bg.target_node.bg.scrollrect.viewport.content.layout"]

    uiMap["summon_odds_ui.bg.target_node.bottom_bg.btn_close"]:addClickListener(function()
        self:closeUI()
    end)
end

--@region 主界面刷新
function SummonOddsUI:onRefresh()
    local tempGrouped = {}
    local weights = DataManager.SummonData:getSummonWeight(self.page)
    for _, entry in pairs(weights) do
        local qlt = DataManager.HeroData:getHeroQlt(entry.id)
        if not tempGrouped[qlt] then
            tempGrouped[qlt] = {}
        end
        table.insert(tempGrouped[qlt], entry.id)
    end
    local shows = DataManager.SummonData:getSummonShow(self.page)
    self.shadowObjs = {}
    self.layoutObjs = {}
    for i = #shows, 1, -1 do
        local data = shows[i]
        local shadowObj = CS.UnityEngine.Object.Instantiate(self.title:getGameObject())
        local title_item = UIPrefabObject:create()
        title_item:initWithPrefab(GConst.EMPTY_STRING, shadowObj)
        title_item:initPrefabHelper()
        title_item:setActive(true)
        title_item:setParent(self.content, false)
        title_item:setLocalScale(0.9, 0.9, 0.9)
        local uiMap = title_item:genAllChildren()
        table.insert(self.shadowObjs, title_item)

        local descTx = uiMap["title.desc_tx"]
        -- local bg = uiMap["title.bg"]
        -- local txName = uiMap["title.bg.tx_name"]
        -- bg:setSprite(GConst.ATLAS_PATH.UI_SUMMON, "summon_bg_" .. data[1])
        local str
        if i == 1 then
            str = I18N:getGlobalText(I18N.GlobalConst.HERO_CARD_DESC_QLT_3) .. data[2] / 100 .. "%"
        elseif i == 2 then
            str = I18N:getGlobalText(I18N.GlobalConst.HERO_CARD_DESC_QLT_4) .. data[2] / 100 .. "%"
        else
            str = I18N:getGlobalText(I18N.GlobalConst.HERO_CARD_DESC_QLT_5) .. data[2] / 100 .. "%"
        end
        local str = string.format("<color=%s>%s</color>", GConst.QUALITY_TYPE[i + 2], str)
        descTx:setText(str)
        -- descTx:setText(I18N:getGlobalText(I18N.GlobalConst.BATTLE_GET_DESC_3, data[2] / 100))
        local layoutObj = CS.UnityEngine.Object.Instantiate(self.layout:getGameObject())
        local layout_item = UIPrefabObject:create()
        layout_item:initWithPrefab(GConst.EMPTY_STRING, layoutObj)
        layout_item:initPrefabHelper()
        layout_item:setActive(true)
        layout_item:setParent(self.content, false)
        local gridLayout = layout_item:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_GRID_LAYOUT)
        gridLayout.CellSize = BF.Vector2(150, 214)
        table.insert(self.layoutObjs, layout_item)
        local temp_id = tempGrouped[data[1]]
        for _, id in pairs(temp_id) do
            local forceObj = CS.UnityEngine.Object.Instantiate(self.summonHeroCell:getGameObject())
            local force_item = UIPrefabObject:create()
            force_item:initWithPrefab(GConst.EMPTY_STRING, forceObj)
            force_item:initPrefabHelper()
            force_item:setActive(true)
            force_item:setParent(layout_item, false)
            local cell = force_item:addLuaComponent(SummonHeroCell)
            cell:refreshInfo(id)
        end
        gridLayout:RefreshLayout()
        layout_item:setSizeDeltaY(gridLayout:GetVerticalSize())
    end
    self.layout_content:RefreshLayout()
    self.content:setSizeDeltaY(self.layout_content:GetVerticalSize())
end

function SummonOddsUI:onClose()
    for _, obj in pairs(self.shadowObjs) do
        obj:destroy()
    end
    for _, obj in pairs(self.layoutObjs) do
        obj:destroy()
    end
end

return SummonOddsUI
