local FourteenDayEntranceUI = class("FourteenDayEntranceUI", BaseUI)

function FourteenDayEntranceUI:isFullScreen()
    return false
end

function FourteenDayEntranceUI:getPrefabPath()
    return "assets/prefabs/ui/activity/fourteen_day/fourteen_day_entrance_ui.prefab"
end

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

function FourteenDayEntranceUI:onClose()
    if self.countdownSid then
		self:unscheduleGlobal(self.countdownSid)
	end
    if self.animsAppear then
        for idx, anim in pairs(self.animsAppear) do
            if anim then
                anim:Kill()
            end
        end
    end
end

function FourteenDayEntranceUI:ctor()
end

function FourteenDayEntranceUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.btnClose = uiMap["fourteen_day_entrance_ui.btn_close"]
    self.flyPos = uiMap["fourteen_day_entrance_ui.fly_posx"]
    -- 战令
    self.itemBounty = uiMap["fourteen_day_entrance_ui.item_bounty"]
    self.txBountyTitle = uiMap["fourteen_day_entrance_ui.item_bounty.tx_title"]
    self.txBountyDesc = uiMap["fourteen_day_entrance_ui.item_bounty.tx_desc"]
    self.txBountyCountdown = uiMap["fourteen_day_entrance_ui.item_bounty.countdown.tx_countdown"]
    -- 任务
    self.itemTask = uiMap["fourteen_day_entrance_ui.item_task"]
    self.txTaskTitle = uiMap["fourteen_day_entrance_ui.item_task.tx_title"]
    self.txTaskDesc = uiMap["fourteen_day_entrance_ui.item_task.tx_desc"]
    self.txTaskCountdown = uiMap["fourteen_day_entrance_ui.item_task.countdown.tx_countdown"]
    -- 兑换
    self.itemExchange = uiMap["fourteen_day_entrance_ui.item_exchange"]
    self.txExchangeTitle = uiMap["fourteen_day_entrance_ui.item_exchange.tx_title"]
    self.txExchangeDesc = uiMap["fourteen_day_entrance_ui.item_exchange.tx_desc"]
    self.txExchangeCountdown = uiMap["fourteen_day_entrance_ui.item_exchange.countdown.tx_countdown"]
    -- 礼包
    self.itemGift = uiMap["fourteen_day_entrance_ui.item_gift"]
    self.txGiftTitle = uiMap["fourteen_day_entrance_ui.item_gift.tx_title"]
    self.txGiftDesc = uiMap["fourteen_day_entrance_ui.item_gift.tx_desc"]
    self.txGiftCountdown = uiMap["fourteen_day_entrance_ui.item_gift.countdown.tx_countdown"]

    self.txBountyTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_TITLE_1))
    self.txBountyDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_DESC_1))
    self.txTaskTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_TITLE_2))
    self.txTaskDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_DESC_2))
    self.txExchangeTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_TITLE_3))
    self.txExchangeDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_DESC_3))
    self.txGiftTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_FOURTEEN_TITLE_4))
    self.txGiftDesc:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_BOSS_RUSH_DESC_32))
    if self.animsAppear then
        for idx, anim in pairs(self.animsAppear) do
            if anim then
                anim:Kill()
            end
        end
    end
    self.animsAppear = {}
    table.insert(self.animsAppear, self:showAppearAnim(1, self.itemBounty))
    table.insert(self.animsAppear, self:showAppearAnim(2, self.itemTask))
    table.insert(self.animsAppear, self:showAppearAnim(3, self.itemExchange))
    table.insert(self.animsAppear, self:showAppearAnim(4, self.itemGift))

    self.itemBounty:addClickListener(function()
        UIManager:showUI("app/ui/activity/fourteen_day/fourteen_day_bounty_ui")
    end)
    self.itemTask:addClickListener(function()
        UIManager:showUI("app/ui/activity/fourteen_day/fourteen_day_task_ui")
    end)
    self.itemExchange:addClickListener(function()
        UIManager:showUI("app/ui/activity/fourteen_day/fourteen_day_exchange_ui")
    end)
    self.itemGift:addClickListener(function()
        UIManager:showUI("app/ui/activity/fourteen_day/fourteen_day_gift_ui")
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)
    self:bind(DataManager.FourteenDayData, "isDirty", function()
        self:onRefresh()
	end)
    self:bind(DataManager.ShopData, "isDirty", function()
        self:refreshRedPoint()
	end)
end

function FourteenDayEntranceUI:onRefresh()
    -- 倒计时
	if self.countdownSid then
		self:unscheduleGlobal(self.countdownSid)
	end
	self.countdownSid = self:scheduleGlobal(function()
        local time = DataManager.FourteenDayData:getEndRemainTime()
        if time < 0 then
            self:closeUI()
            return
        end
        self.txBountyCountdown:setText(Time:formatNumTimeStr(time))
        self.txTaskCountdown:setText(Time:formatNumTimeStr(time))
        self.txExchangeCountdown:setText(Time:formatNumTimeStr(time))
        self.txGiftCountdown:setText(Time:formatNumTimeStr(time))
	end, 1)
    local time = DataManager.FourteenDayData:getEndRemainTime()
    self.txBountyCountdown:setText(Time:formatNumTimeStr(time))
    self.txTaskCountdown:setText(Time:formatNumTimeStr(time))
    self.txExchangeCountdown:setText(Time:formatNumTimeStr(time))
    self.txGiftCountdown:setText(Time:formatNumTimeStr(time))

    self:refreshRedPoint()
end

-- 刷新红点
function FourteenDayEntranceUI:refreshRedPoint()
    if DataManager.FourteenDayData:hasBountyRedPoint() then
        self.itemBounty:addRedPoint(300, 75, 0.6)
    else
        self.itemBounty:removeRedPoint()
    end
    if DataManager.FourteenDayData:hasTaskRedPoint() then
        self.itemTask:addRedPoint(300, 75, 0.6)
    else
        self.itemTask:removeRedPoint()
    end
    if DataManager.FourteenDayData:hasExchangeRedPoint() then
        self.itemExchange:addRedPoint(300, 75, 0.6)
    else
        self.itemExchange:removeRedPoint()
    end
    if DataManager.FourteenDayData:hasGiftRedPoint() then
        self.itemGift:addRedPoint(300, 75, 0.6)
    else
        self.itemGift:removeRedPoint()
    end
end

-- 展示出现动画
function FourteenDayEntranceUI:showAppearAnim(idx, item)
    item:setTouchEnable(false)
    item:setPosition(self.flyPos:getPosition().x, item:getPosition().y, item:getPosition().z)

    local delay = (idx - 1) * 0.1
	local animAppear = item:createBindTweenSequence()
    animAppear:Insert(delay, item:getTransform():DOMove(CS.UnityEngine.Vector3(0, item:getPosition().y, item:getPosition().z), 0.5))
    animAppear:OnComplete(function()
        item:setTouchEnable(true)
    end)
    return animAppear
end

return FourteenDayEntranceUI