local BuyVitUI = class("BuyVitUI", BaseUI)

function BuyVitUI:isFullScreen()
    return false
end

function BuyVitUI:showCommonBG()
    return false
end

function BuyVitUI:ctor(params)
    self.dataKey = params.dataKey
end

function BuyVitUI:getPrefabPath()
    return "assets/prefabs/ui/shop/buy_vit_ui.prefab"
end

function BuyVitUI:onLoadRootComplete()
    local uiMap = self.root:genAllChildren()
    self.mask = uiMap["buy_vit_ui.mask"]
    self.txTitle = uiMap["buy_vit_ui.content.tx_title"]
    self.btnClose = uiMap["buy_vit_ui.content.btn_close"]
    self.buyNodes = {}
    self.txRewards = {}
    self.txCosts = {}
    self.imgCosts = {}
    self.txLimits = {}
    self.imgChecks = {}
    for k, buyType in pairs(GConst.VIT_BUY_TYPE) do
        self.buyNodes[buyType] = uiMap["buy_vit_ui.content.content.cell_" .. buyType]
        self.txRewards[buyType] = uiMap["buy_vit_ui.content.content.cell_" .. buyType .. ".tx_num"]
        self.txCosts[buyType] = uiMap["buy_vit_ui.content.content.cell_" .. buyType .. ".tx_cost"]
        self.imgCosts[buyType] = uiMap["buy_vit_ui.content.content.cell_" .. buyType .. ".img_cost"]
        self.txLimits[buyType] = uiMap["buy_vit_ui.content.content.cell_" .. buyType .. ".tx_limit"]
        self.imgChecks[buyType] = uiMap["buy_vit_ui.content.content.cell_" .. buyType .. ".img_check"]
    end

    self.txTitle:setText(self:getBuyTitle())
    if self.buyNodes[GConst.VIT_BUY_TYPE.AD] then
        self.buyNodes[GConst.VIT_BUY_TYPE.AD]:addClickListener(function()
            -- if not GFunc.IsGotServerTime() then
            --     GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.NEED_NET))
            --     return
            -- end
            SDKManager:showFullScreenAds(self:getBuyAdType(), function()
                self:getBuyFunc(GConst.VIT_BUY_TYPE.AD)
            end)
        end)
    end
    self.buyNodes[GConst.VIT_BUY_TYPE.GEM]:addClickListener(function()
        local cost = self:getBuyCost(GConst.VIT_BUY_TYPE.GEM)
        if not GFunc.checkCost(GFunc.getRewardId(cost), GFunc.getRewardNum(cost), true, self:getBuyBI()) then
            return
        end
        local params = {}
        params.content = I18N:getGlobalText(I18N.GlobalConst.MALL_DESC_13)
        params.boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL
        params.showToday = GConst.MESSAGE_BOX_SHOW_TODAY.BUY_VIT_COUNT
        params.okFunc = function()
            self:getBuyFunc(GConst.VIT_BUY_TYPE.GEM)
        end
        GFunc.showMessageBox(params)
    end)
    self.mask:addClickListener(function()
        self:closeUI()
    end)
    self.btnClose:addClickListener(function()
        self:closeUI()
    end)

    self:bind(DataManager[self.dataKey], "isDirty", function()
        self:onRefresh()
    end)
end

function BuyVitUI:onRefresh()
    for buyType, node in pairs(self.buyNodes) do
        local reward = self:getBuyReward(buyType)
        local cost = self:getBuyCost(buyType)
        local remain = self:getBuyRemainCount(buyType)

        node:setSprite(GConst.ATLAS_PATH.UI_SHOP, self:getBuyBg(buyType))
        self.txRewards[buyType]:setText("x" .. GFunc.getRewardNum(reward))
        if remain < 9999 then
            self.txLimits[buyType]:setText(I18N:getGlobalText(I18N.GlobalConst.TODAY_REMAIN_TIMES, remain))
        else
            self.txLimits[buyType]:setText("")
        end

        if buyType == GConst.VIT_BUY_TYPE.AD then
            GFunc.setAdsSprite(self.imgCosts[buyType])
            self.txCosts[buyType]:setText(I18N:getGlobalText(I18N.GlobalConst.FREE_DESC))
        elseif buyType == GConst.VIT_BUY_TYPE.GEM then
            self.imgCosts[buyType]:setSprite(GFunc.getIconResByReward(cost))
            self.txCosts[buyType]:setText(GFunc.getRewardNum(cost))
        end
        GFunc.centerImgAndTx(self.imgCosts[buyType], self.txCosts[buyType], 3)

        if remain > 0 then
            node:setTouchEnable(true)
            self.imgChecks[buyType]:setActive(false)
            self.imgCosts[buyType]:setActive(true)
            self.txCosts[buyType]:setActive(true)
        else
            node:setTouchEnable(false)
            self.imgChecks[buyType]:setActive(false)
            self.imgCosts[buyType]:setActive(true)
            self.txCosts[buyType]:setActive(true)
        end

        -- 红点
        if buyType == GConst.VIT_BUY_TYPE.AD and remain > 0 then
            node:addRedPoint(105, 160, 1)
        else
            node:removeRedPoint()
        end
    end
end

-- 以下接口，如果要加新的道具，需要补充对应的逻辑

function BuyVitUI:getBuyTitle()
    if self.dataKey == "ShopData" then
        return I18N:getGlobalText(I18N.GlobalConst.BUY_VIT_DESC_1)
    end
    if self.dataKey == "ChallengeTeamData" then
        return I18N:getGlobalText(I18N.GlobalConst.BUY_VIT_DESC_3)
    end
    if self.dataKey == "ChapterTeamData" then
        return I18N:getGlobalText(I18N.GlobalConst.BUY_VIT_DESC_4)
    end
end

function BuyVitUI:getBuyBg(buyType)
    if self.dataKey == "ShopData" then
        if buyType == GConst.VIT_BUY_TYPE.AD then
            return "shop_bg_14"
        end
        if buyType == GConst.VIT_BUY_TYPE.GEM then
            return "shop_bg_13"
        end
    end
    if self.dataKey == "ChallengeTeamData" then
        if buyType == GConst.VIT_BUY_TYPE.AD then
            return "shop_bg_8"
        end
        if buyType == GConst.VIT_BUY_TYPE.GEM then
            return "shop_bg_11"
        end
    end
    if self.dataKey == "ChapterTeamData" then
        if buyType == GConst.VIT_BUY_TYPE.AD then
            return "shop_bg_10"
        end
        if buyType == GConst.VIT_BUY_TYPE.GEM then
            return "shop_bg_9"
        end
    end
end

function BuyVitUI:getBuyReward(buyType)
    if self.dataKey == "ShopData" then
        return DataManager.ShopData:getVitBuyReward(buyType)
    end
    if self.dataKey == "ChallengeTeamData" then
        return DataManager.ChallengeTeamData:getVitBuyReward(buyType)
    end
    if self.dataKey == "ChapterTeamData" then
        return DataManager.ChapterTeamData:getVitBuyReward(buyType)
    end
end

function BuyVitUI:getBuyCost(buyType)
    if self.dataKey == "ShopData" then
        return DataManager.ShopData:getVitBuyCost(buyType)
    end
    if self.dataKey == "ChallengeTeamData" then
        return DataManager.ChallengeTeamData:getVitBuyCost(buyType)
    end
    if self.dataKey == "ChapterTeamData" then
        return DataManager.ChapterTeamData:getVitBuyCost(buyType)
    end
end

function BuyVitUI:getBuyRemainCount(buyType)
    if self.dataKey == "ShopData" then
        return DataManager.ShopData:getVitBuyRemainCount(buyType)
    end
    if self.dataKey == "ChallengeTeamData" then
        return DataManager.ChallengeTeamData:getVitBuyRemainCount(buyType)
    end
    if self.dataKey == "ChapterTeamData" then
        return DataManager.ChapterTeamData:getVitBuyRemainCount(buyType)
    end
end

function BuyVitUI:getBuyFunc(buyType)
    if self.dataKey == "ShopData" then
        return ModuleManager.ShopManager:reqBuyVit(buyType)
    end
    if self.dataKey == "ChallengeTeamData" then
        return ModuleManager.ChallengeTeamManager:reqChallengeBuyVitality(buyType == GConst.VIT_BUY_TYPE.AD)
    end
    if self.dataKey == "ChapterTeamData" then
        return ModuleManager.ChapterTeamManager:reqBuyVitality(buyType)
    end
end

function BuyVitUI:getBuyBI()
    if self.dataKey == "ShopData" then
        return BIReport.ITEM_GET_TYPE.BUY_VIT
    end
    if self.dataKey == "ChallengeTeamData" then
        return BIReport.ITEM_GET_TYPE.CHALLENGE_VITALITY_BUY_COUNT
    end
    if self.dataKey == "ChapterTeamData" then
        return BIReport.ITEM_GET_TYPE.CHAPTER_TEAM_BUY_COUNT
    end
end

function BuyVitUI:getBuyAdType()
    if self.dataKey == "ShopData" then
        return BIReport.ADS_CLICK_TYPE.AD_ENERGY
    end
    if self.dataKey == "ChallengeTeamData" then
        return BIReport.ADS_CLICK_TYPE.AD_CHALLENGE_ENERGY
    end
    if self.dataKey == "ChapterTeamData" then
        return BIReport.ADS_CLICK_TYPE.AD_CHAPTER_TEAM_ENERGY
    end
end

return BuyVitUI
