local ActivityUI = class("ActivityUI", BaseUI)
local COMP_TASK = "app/ui/activity/activity_task_comp"
local COMP_BOUNTY = "app/ui/activity/activity_bounty_comp"
local COMP_SKIN = "app/ui/activity/activity_skin_comp"
local COMP_HERO = "app/ui/activity/activity_hero_comp"

function ActivityUI:isFullScreen()
    return false
end

function ActivityUI:getPrefabPath()
    return "assets/prefabs/ui/activity/activity_ui.prefab"
end

function ActivityUI:onPressBackspace()
	self:closeUI()
end

function ActivityUI:ctor(param)
    if param then
        self.panelType = param
    elseif DataManager.ActivityData:hasTaskRedPoint() then
        self.panelType = GConst.ActivityConst.PANEL_TYPE.TASK
    elseif DataManager.ActivityData:hasBountyRedPoint() then
        self.panelType = GConst.ActivityConst.PANEL_TYPE.BOUNTY
    elseif DataManager.ActivityData:hasSkinRedPoint() then
        self.panelType = GConst.ActivityConst.PANEL_TYPE.SKIN
    elseif DataManager.ActivityData:hasHeroRedPoint() then
        self.panelType = GConst.ActivityConst.PANEL_TYPE.HERO
    else
        self.panelType = GConst.ActivityConst.PANEL_TYPE.TASK -- 默认进入任务页签
    end
end

function ActivityUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    -- 标题
    self.imgTitle = uiMap["activity_ui.img_title"]
    self.txTitle = uiMap["activity_ui.img_title.tx_title"]
    -- 子界面
    self.taskPanel = uiMap["activity_ui.task_panel"]
    self.bountyPanel = uiMap["activity_ui.bounty_panel"]
    self.skinPanel = uiMap["activity_ui.skin_panel"]
    self.heroPanel = uiMap["activity_ui.hero_panel"]
    -- 任务标签
    self.btnTask = uiMap["activity_ui.common.btns.btn_task"]
    self.txTask1 = uiMap["activity_ui.common.btns.btn_task.tx_btn"]
    self.txTask2 = uiMap["activity_ui.common.btns.btn_task.select.tx_select"]
    self.selectTask = uiMap["activity_ui.common.btns.btn_task.select"]
    -- 战令标签
    self.btnBounty = uiMap["activity_ui.common.btns.btn_bounty"]
    self.txBounty1 = uiMap["activity_ui.common.btns.btn_bounty.tx_btn"]
    self.txBounty2 = uiMap["activity_ui.common.btns.btn_bounty.select.tx_select"]
    self.selectBounty = uiMap["activity_ui.common.btns.btn_bounty.select"]
    -- 皮肤标签
    self.btnSkin = uiMap["activity_ui.common.btns.btn_skin"]
    self.txSkin1 = uiMap["activity_ui.common.btns.btn_skin.tx_btn"]
    self.txSkin2 = uiMap["activity_ui.common.btns.btn_skin.select.tx_select"]
    self.selectSkin = uiMap["activity_ui.common.btns.btn_skin.select"]
    -- 英雄标签
    self.btnHero = uiMap["activity_ui.common.btns.btn_hero"]
    self.txHero1 = uiMap["activity_ui.common.btns.btn_hero.tx_btn"]
    self.txHero2 = uiMap["activity_ui.common.btns.btn_hero.select.tx_select"]
    self.selectHero = uiMap["activity_ui.common.btns.btn_hero.select"]
    -- 通用
    self.btnClose = uiMap["activity_ui.common.btn_close"]
    self.btnHelp = uiMap["activity_ui.countdown.btn_help"]
    self.txCountdown = uiMap["activity_ui.countdown.content.tx_countdown"]

    self.txTask1:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_NAME))
    self.txTask2:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_NAME))
    self.txBounty1:setText(I18N:getGlobalText(I18N.GlobalConst.REWARD_DESC))
    self.txBounty2:setText(I18N:getGlobalText(I18N.GlobalConst.REWARD_DESC))
    self.txSkin1:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_3))
    self.txSkin2:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_3))
    self.txHero1:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_4))
    self.txHero2:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_4))

    self.btnTask:addClickListener(function()
        self.panelType = GConst.ActivityConst.PANEL_TYPE.TASK
        self:onRefresh()
    end)
    self.btnBounty:addClickListener(function()
        self.panelType = GConst.ActivityConst.PANEL_TYPE.BOUNTY
        self:onRefresh()
    end)
    self.btnSkin:addClickListener(function()
        self.panelType = GConst.ActivityConst.PANEL_TYPE.SKIN
        self:onRefresh()
    end)
    self.btnHero:addClickListener(function()
        self.panelType = GConst.ActivityConst.PANEL_TYPE.HERO
        self:onRefresh()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self.btnHelp:addClickListener(function()
        local params = {
            type = GConst.TipsConst.HELP_TIPS_TYPE.ARENA,
            title = I18N:getGlobalText(I18N.GlobalConst.COLLECTION_DESC_11),
            desc = I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_10),
        }
        ModuleManager.TipsManager:showHelpTips(params)
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.ACTIVITY_SUMMER_END, function()
        self:closeUI()
    end)
    self:bind(DataManager.ActivityData, "isDirty", function()
        self:onRefresh()
	end)
end

function ActivityUI:onRefresh()
    self.txTitle:setText(DataManager.ActivityData:getActTitle())
    self.imgTitle:setSprite(DataManager.ActivityData:getActTitleBg())

    -- 倒计时
	if self.actCountdownSid then
		self.txCountdown:unscheduleGlobal(self.actCountdownSid)
	end
    self.txCountdown:setText(GFunc.getTimeStrWithHMS2(DataManager.ActivityData:getEndRemainTime()))
	self.actCountdownSid = self.txCountdown:scheduleGlobal(function()
        self.txCountdown:setText(GFunc.getTimeStrWithHMS2(DataManager.ActivityData:getEndRemainTime()))
	end, 1)

    if self.panelType == GConst.ActivityConst.PANEL_TYPE.TASK then
        self:showTaskInfo()
    elseif self.panelType == GConst.ActivityConst.PANEL_TYPE.BOUNTY then
        self:showBountyInfo()
    elseif self.panelType == GConst.ActivityConst.PANEL_TYPE.SKIN then
        self:showSkinInfo()
    elseif self.panelType == GConst.ActivityConst.PANEL_TYPE.HERO then
        self:showHeroInfo()
    end
    self:refreshRedPoint()
end

-- 刷新标签红点
function ActivityUI:refreshRedPoint()
    if DataManager.ActivityData:hasTaskRedPoint() then
        self.btnTask:addRedPoint(-65, -5, 0.6)
    else
        self.btnTask:removeRedPoint()
    end
    if DataManager.ActivityData:hasBountyRedPoint() then
        self.btnBounty:addRedPoint(-65, -5, 0.6)
    else
        self.btnBounty:removeRedPoint()
    end
    if DataManager.ActivityData:hasSkinRedPoint() then
        self.btnSkin:addRedPoint(-65, -5, 0.6)
    else
        self.btnSkin:removeRedPoint()
    end
    if DataManager.ActivityData:hasHeroRedPoint() then
        self.btnHero:addRedPoint(-65, -5, 0.6)
    else
        self.btnHero:removeRedPoint()
    end
end

function ActivityUI:showTaskInfo()
    self.taskPanel:setActive(true)
    self.selectTask:setActive(true)
    self.bountyPanel:setActive(false)
    self.selectBounty:setActive(false)
    self.skinPanel:setActive(false)
    self.selectSkin:setActive(false)
    self.heroPanel:setActive(false)
    self.selectHero:setActive(false)
    self.btnHelp:setActive(true)

    if not self.compTask then
        self.taskPanel:initPrefabHelper()
        self.taskPanel:genAllChildren()
        self.compTask = self.taskPanel:addLuaComponent(COMP_TASK)
    end

    self.compTask:refresh()
end

function ActivityUI:showBountyInfo()
    self.taskPanel:setActive(false)
    self.selectTask:setActive(false)
    self.bountyPanel:setActive(true)
    self.selectBounty:setActive(true)
    self.skinPanel:setActive(false)
    self.selectSkin:setActive(false)
    self.heroPanel:setActive(false)
    self.selectHero:setActive(false)
    self.btnHelp:setActive(true)

    if not self.compBounty then
        self.bountyPanel:initPrefabHelper()
        self.bountyPanel:genAllChildren()
        self.compBounty = self.bountyPanel:addLuaComponent(COMP_BOUNTY)
    end

    self.compBounty:refresh()
end

function ActivityUI:showSkinInfo()
    self.taskPanel:setActive(false)
    self.selectTask:setActive(false)
    self.bountyPanel:setActive(false)
    self.selectBounty:setActive(false)
    self.skinPanel:setActive(true)
    self.selectSkin:setActive(true)
    self.heroPanel:setActive(false)
    self.selectHero:setActive(false)
    self.btnHelp:setActive(false)

    if not self.compSkin then
        self.skinPanel:initPrefabHelper()
        self.skinPanel:genAllChildren()
        self.compSkin = self.skinPanel:addLuaComponent(COMP_SKIN)
    end

    self.compSkin:refresh()
end

function ActivityUI:showHeroInfo()
    self.taskPanel:setActive(false)
    self.selectTask:setActive(false)
    self.bountyPanel:setActive(false)
    self.selectBounty:setActive(false)
    self.skinPanel:setActive(false)
    self.selectSkin:setActive(false)
    self.heroPanel:setActive(true)
    self.selectHero:setActive(true)
    self.btnHelp:setActive(false)

    if not self.compHero then
        self.heroPanel:initPrefabHelper()
        self.heroPanel:genAllChildren()
        self.compHero = self.heroPanel:addLuaComponent(COMP_HERO)
    end

    self.compHero:refresh()
end

return ActivityUI