local ActPvpMatchResultUI = class("ActPvpMatchResultUI", BaseUI)

local SIMPLE_HERO_CELL = "app/ui/activity/act_pvp/cell/simple_hero_cell"

function ActPvpMatchResultUI:getPrefabPath()
    return "assets/prefabs/ui/activity/act_pvp/act_pvp_match_result_ui.prefab"
end

function ActPvpMatchResultUI:onLoadRootComplete()
    self:_display()
    self:_addListeners()

    self:refreshTime()
    if self.actCountdownSid then
        self:unscheduleGlobal(self.actCountdownSid)
    end
    self.actCountdownSid = self:scheduleGlobal(function()
        self:refreshTime()
	end, 1)
end

function ActPvpMatchResultUI:_display()
    local uiMap = self.root:genAllChildren()
    local vsSpineObj = uiMap["act_pvp_match_result_ui.match_result.spine_vs"]
	vsSpineObj:playAnimComplete("born", false, true, function()
        vsSpineObj:playAnim("idle", true, true)
    end)
    uiMap["act_pvp_match_result_ui.match_result.btn_start.tx_start"]:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_CHALLENGE))
    uiMap["act_pvp_match_result_ui.match_result.btn_rematch.tx_rematch"]:setText(I18N:getGlobalText(I18N.GlobalConst.ARENA_DESC_23))

    local matchAnimator = uiMap["act_pvp_match_result_ui.match_result"]:getComponent(GConst.TYPEOF_UNITY_CLASS.ANIMATOR)
    matchAnimator:Play(1396761479, -1, 0)

    self:refreshPlayerInfos()
end

function ActPvpMatchResultUI:_addListeners()
    local uiMap = self.root:genAllChildren()
    uiMap["act_pvp_match_result_ui.match_result.btn_close"]:addClickListener(function()
        self:closeUI()
    end)

    uiMap["act_pvp_match_result_ui.match_result.btn_rematch"]:addClickListener(function()
        self:onClickRefresh()
    end)

    uiMap["act_pvp_match_result_ui.match_result.btn_start"]:addClickListener(function()
        ModuleManager.ActPvpManager:reqFight()
    end)

    self:addEventListener(EventManager.CUSTOM_EVENT.ACT_PVP_EVENT, function(params)
        if params and params.reqCDOver then
            self:closeUI()
        end
    end)
end

function ActPvpMatchResultUI:onClickRefresh()
    local remainTime = DataManager.ActPvpData:getMatchCdEndTime()
    if remainTime <= 0 then
        self:closeUI()
        ModuleManager.ActPvpManager:showMatchUI(true)
    else
        if DataManager.ActPvpData:getCanAdRefreshHero() then
            SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.ACT_PVP_AD_REMATCH, function()
                ModuleManager.ActPvpManager:reqOverCD(true)
            end)
        else
            ModuleManager.ActPvpManager:reqOverCD(false)
        end
    end
end

function ActPvpMatchResultUI:refreshTime()
    local uiMap = self.root:genAllChildren()
    local remainTime = DataManager.ActPvpData:getMatchCdEndTime()
    local adImg = uiMap["act_pvp_match_result_ui.match_result.btn_rematch.img_ad"]
    local str
    if remainTime <= 0 then
        str = GConst.EMPTY_STRING
        adImg:setVisible(false)
    else
        str = Time:formatNumTime(remainTime)
        if DataManager.ActPvpData:getCanAdRefreshHero() then
            adImg:setVisible(true)
        else
            adImg:setVisible(false)
        end
    end
    uiMap["act_pvp_match_result_ui.match_result.btn_rematch.tx_free_countdown"]:setText(str)
end

function ActPvpMatchResultUI:refreshPlayerInfos()
    local uiMap = self.root:genAllChildren()
    local rivalInfo = DataManager.ActPvpData:getRivalInfo()
    if rivalInfo then
        if not self.otherPlayerHeadCell then
            self.otherPlayerHeadCell = CellManager:addCellComp(uiMap["act_pvp_match_result_ui.match_result.match.info.player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
        end
        self.otherPlayerHeadCell:refresh(rivalInfo.avatar, rivalInfo.avatar_frame)
        uiMap["act_pvp_match_result_ui.match_result.match.info.tx_name"]:setText(rivalInfo.name)
        uiMap["act_pvp_match_result_ui.match_result.match.info.tx_level"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, rivalInfo.level))
        uiMap["act_pvp_match_result_ui.match_result.match.info.tx_win_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_14))
        uiMap["act_pvp_match_result_ui.match_result.match.info.tx_win_count"]:setText(rivalInfo.total_win)

        if not self.otherSimpleHeroCells then
            self.otherSimpleHeroCells = {}
            for i = 1, 5 do
                self.otherSimpleHeroCells[i] = CellManager:addCellComp(uiMap["act_pvp_match_result_ui.match_result.match.formation.simple_hero_cell_" .. i], SIMPLE_HERO_CELL)
            end
        end

        local heroes = DataManager.ActPvpData:getRivalInfoHeroes()
        if heroes then
            for matchType, cell in ipairs(self.otherSimpleHeroCells) do
                local entity = heroes[matchType]
                if entity then
                    cell:getBaseObject():setVisible(true)
                    cell:refresh(entity)
                else
                    cell:getBaseObject():setVisible(false)
                end
            end
        end
    end

    if not self.playerHeadCell then
        self.playerHeadCell = CellManager:addCellComp(uiMap["act_pvp_match_result_ui.match_result.self.info.player_head_cell"], GConst.TYPEOF_LUA_CLASS.PLAYER_HEAD_CELL)
    end
    self.playerHeadCell:refresh(DataManager.PlayerData:getUsingAvatarId(), DataManager.PlayerData:getUsingFrameId())
    uiMap["act_pvp_match_result_ui.match_result.self.info.tx_name"]:setText(DataManager.PlayerData:getNickname())
    uiMap["act_pvp_match_result_ui.match_result.self.info.tx_level"]:setText(I18N:getGlobalText(I18N.GlobalConst.HERO_DESC_1, DataManager.PlayerData:getLv()))
    uiMap["act_pvp_match_result_ui.match_result.self.info.tx_win_desc"]:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_PVP_DESC_14))
    uiMap["act_pvp_match_result_ui.match_result.self.info.tx_win_count"]:setText(DataManager.ActPvpData:getTotalWinCount())

    if not self.simpleHeroCells then
        self.simpleHeroCells = {}
        for i = 1, 5 do
            self.simpleHeroCells[i] = CellManager:addCellComp(uiMap["act_pvp_match_result_ui.match_result.self.formation.simple_hero_cell_" .. i], SIMPLE_HERO_CELL)
        end
    end

    local heroes = DataManager.ActPvpData:getHeros()
    if heroes then
        for matchType, cell in ipairs(self.simpleHeroCells) do
            local entity = heroes[matchType]
            if entity then
                cell:getBaseObject():setVisible(true)
                cell:refresh(entity)
            else
                cell:getBaseObject():setVisible(false)
            end
        end
    end
end

return ActPvpMatchResultUI