local FullMoonUI = class("FullMoonUI", BaseUI)
local COMP_TASK = "app/ui/activity/full_moon/full_moon_task_comp"
local COMP_CHOOSE_HERO = "app/ui/activity/full_moon/full_moon_choose_hero_comp"
local COMP_NEW_HERO = "app/ui/activity/full_moon/full_moon_new_hero_comp"
local COMP_SKIN = "app/ui/activity/full_moon/full_moon_skin_comp"

function FullMoonUI:isFullScreen()
    return false
end

function FullMoonUI:getPrefabPath()
    return "assets/prefabs/ui/activity/full_moon/full_moon_ui.prefab"
end

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

function FullMoonUI:onClose()
    if self.actCountdownSid then
		self.txCountdown:unscheduleGlobal(self.actCountdownSid)
	end
end

function FullMoonUI:ctor(param)
    if param then
        self.panelType = param
    else
        self.panelType = GConst.FullMoonConst.PANEL_TYPE.TASK -- 默认进入任务页签
    end
end

function FullMoonUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    -- 通用
    self.txTitle = uiMap["full_moon_ui.img_title.tx_title"]
    self.btnClose = uiMap["full_moon_ui.common.btn_close"]
    self.txCountdown = uiMap["full_moon_ui.countdown.tx_countdown"]
    self.bg_task = uiMap["full_moon_ui.common.bg_task"]
    self.bg_gift = uiMap["full_moon_ui.common.bg_gift"]
    -- 子界面
    self.taskPanel = uiMap["full_moon_ui.task_panel"]
    self.chooseHeroPanel = uiMap["full_moon_ui.choose_hero_panel"]
    self.newHeroPanel = uiMap["full_moon_ui.new_hero_panel"]
    self.skinPanel = uiMap["full_moon_ui.skin_panel"]
    -- 任务标签
    self.btnTask = uiMap["full_moon_ui.common.btns.btn_task"]
    self.txTask1 = uiMap["full_moon_ui.common.btns.btn_task.tx_btn"]
    self.txTask2 = uiMap["full_moon_ui.common.btns.btn_task.select.tx_select"]
    self.selectTask = uiMap["full_moon_ui.common.btns.btn_task.select"]
    -- 自选英雄礼包标签
    self.btnChooseHero = uiMap["full_moon_ui.common.btns.btn_choose_hero"]
    self.txChooseHero1 = uiMap["full_moon_ui.common.btns.btn_choose_hero.tx_btn"]
    self.txChooseHero2 = uiMap["full_moon_ui.common.btns.btn_choose_hero.select.tx_select"]
    self.selectChooseHero = uiMap["full_moon_ui.common.btns.btn_choose_hero.select"]
    -- 新英雄礼包标签
    self.btnNewHero = uiMap["full_moon_ui.common.btns.btn_new_hero"]
    self.txNewHero1 = uiMap["full_moon_ui.common.btns.btn_new_hero.tx_btn"]
    self.txNewHero2 = uiMap["full_moon_ui.common.btns.btn_new_hero.select.tx_select"]
    self.selectNewHero = uiMap["full_moon_ui.common.btns.btn_new_hero.select"]
    -- 皮肤标签
    self.btnSkin = uiMap["full_moon_ui.common.btns.btn_skin"]
    self.txSkin1 = uiMap["full_moon_ui.common.btns.btn_skin.tx_btn"]
    self.txSkin2 = uiMap["full_moon_ui.common.btns.btn_skin.select.tx_select"]
    self.selectSkin = uiMap["full_moon_ui.common.btns.btn_skin.select"]

    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_1))
    self.txTask1:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_NAME))
    self.txTask2:setText(I18N:getGlobalText(I18N.GlobalConst.TASK_NAME))
    self.txChooseHero1:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_2))
    self.txChooseHero2:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_2))
    self.txNewHero1:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_3))
    self.txNewHero2:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_3))
    self.txSkin1:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_3))
    self.txSkin2:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_3))
    DataManager.FullMoonData:showActPanel()

    self.btnTask:addClickListener(function()
        self.panelType = GConst.FullMoonConst.PANEL_TYPE.TASK
        self:onRefresh()
    end)
    self.btnChooseHero:addClickListener(function()
        self.panelType = GConst.FullMoonConst.PANEL_TYPE.CHOOSE_HERO
        self:onRefresh()
    end)
    self.btnNewHero:addClickListener(function()
        self.panelType = GConst.FullMoonConst.PANEL_TYPE.NEW_HERO
        self:onRefresh()
    end)
    self.btnSkin:addClickListener(function()
        self.panelType = GConst.FullMoonConst.PANEL_TYPE.SKIN
        self:onRefresh()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:addEventListener(EventManager.CUSTOM_EVENT.ACTIVITY_FULL_MOON_END, function()
        self:closeUI()
    end)
    self:bind(DataManager.FullMoonData, "isDirty", function()
        -- 检查更新数据
        if DataManager.FullMoonData:getIsInReset() and self.panelType == GConst.FullMoonConst.PANEL_TYPE.TASK then
            ModuleManager.FullMoonManager:reqActData()
        end

        self:onRefresh()
	end)
    self:bind(DataManager.ShopData, "isDirty", function()
        self:onRefresh()
	end)
end

function FullMoonUI:onRefresh()
    -- 倒计时
	if self.actCountdownSid then
		self.txCountdown:unscheduleGlobal(self.actCountdownSid)
	end
    self.txCountdown:setText(Time:formatNumTimeStr(DataManager.FullMoonData:getEndRemainTime()))
	self.actCountdownSid = self.txCountdown:scheduleGlobal(function()
        self.txCountdown:setText(Time:formatNumTimeStr(DataManager.FullMoonData:getEndRemainTime()))
	end, 1)

    if self.panelType == GConst.FullMoonConst.PANEL_TYPE.TASK then
        self:showTaskInfo()
    elseif self.panelType == GConst.FullMoonConst.PANEL_TYPE.CHOOSE_HERO then
        self:showChooseHeroInfo()
    elseif self.panelType == GConst.FullMoonConst.PANEL_TYPE.NEW_HERO then
        self:showNewHeroInfo()
    elseif self.panelType == GConst.FullMoonConst.PANEL_TYPE.SKIN then
        self:showSkinInfo()
    end
    self:refreshRedPoint()
end

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

function FullMoonUI:showTaskInfo()
    self.taskPanel:setActive(true)
    self.selectTask:setActive(true)
    self.chooseHeroPanel:setActive(false)
    self.selectChooseHero:setActive(false)
    self.newHeroPanel:setActive(false)
    self.selectNewHero:setActive(false)
    self.skinPanel:setActive(false)
    self.selectSkin:setActive(false)
    self.bg_task:setActive(true)
    self.bg_gift:setActive(false)

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

    self.compTask:refresh()
end

function FullMoonUI:showChooseHeroInfo()
    self.taskPanel:setActive(false)
    self.selectTask:setActive(false)
    self.chooseHeroPanel:setActive(true)
    self.selectChooseHero:setActive(true)
    self.newHeroPanel:setActive(false)
    self.selectNewHero:setActive(false)
    self.skinPanel:setActive(false)
    self.selectSkin:setActive(false)
    self.bg_task:setActive(false)
    self.bg_gift:setActive(true)

    if not self.chooseHeroComp then
        self.chooseHeroPanel:initPrefabHelper()
        self.chooseHeroPanel:genAllChildren()
        self.chooseHeroComp = self.chooseHeroPanel:addLuaComponent(COMP_CHOOSE_HERO)
    end

    self.chooseHeroComp:refresh()
end

function FullMoonUI:showNewHeroInfo()
    self.taskPanel:setActive(false)
    self.selectTask:setActive(false)
    self.chooseHeroPanel:setActive(false)
    self.selectChooseHero:setActive(false)
    self.newHeroPanel:setActive(true)
    self.selectNewHero:setActive(true)
    self.skinPanel:setActive(false)
    self.selectSkin:setActive(false)
    self.bg_task:setActive(false)
    self.bg_gift:setActive(true)

    if not self.newHeroComp then
        self.newHeroPanel:initPrefabHelper()
        self.newHeroPanel:genAllChildren()
        self.newHeroComp = self.newHeroPanel:addLuaComponent(COMP_NEW_HERO)
    end

    self.newHeroComp:refresh()
end

function FullMoonUI:showSkinInfo()
    self.taskPanel:setActive(false)
    self.selectTask:setActive(false)
    self.chooseHeroPanel:setActive(false)
    self.selectChooseHero:setActive(false)
    self.newHeroPanel:setActive(false)
    self.selectNewHero:setActive(false)
    self.skinPanel:setActive(true)
    self.selectSkin:setActive(true)
    self.bg_task:setActive(false)
    self.bg_gift: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

return FullMoonUI