local FullMoonChooseHeroComp = class("FullMoonChooseHeroComp", LuaComponent)

function FullMoonChooseHeroComp:init()
    local uiMap = self:getUIMap()
    -- self.spineBanner = uiMap["choose_hero_panel.spine_banner"]
    self.txTitle = uiMap["choose_hero_panel.tx_title"]
    self.txTips = uiMap["choose_hero_panel.tx_tips"]
    self.btnBuy = uiMap["choose_hero_panel.btn_buy"]
    self.txBuy = uiMap["choose_hero_panel.btn_buy.tx_buy"]
    self.rewards = uiMap["choose_hero_panel.rewards"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
    self.rewardCells = {}
    for i = 1, 3 do
        table.insert(self.rewardCells, uiMap["choose_hero_panel.rewards.pop_reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.POP_REWARD_CELL))
    end
    self.gifts = uiMap["choose_hero_panel.gifts"]:getComponent(GConst.TYPEOF_UNITY_CLASS.BF_HORIZONTAL_OR_VERTICAL_LAYOUT)
    self.giftItems = {}
    for i = 1, 5 do
        table.insert(self.giftItems, uiMap["choose_hero_panel.gifts.gift_" .. i])
    end

    -- self.spineBanner:playAnim("animation", true, true)
    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_4))
    self.txTips:setText(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_6))

    self.btnBuy:addClickListener(function()
        if not DataManager.FullMoonData:getChooseHeroIndex() then
            self:showChoose()
            return
        end
        PayManager:purchasePackage(self.selectGiftId, PayManager.PURCHARSE_TYPE.ACT_GIFT)
    end)
end

function FullMoonChooseHeroComp:refresh()
    self.curGiftId = DataManager.FullMoonData:getCurChooseHeroGiftId()
    self.selectGiftId = self.curGiftId
    self:refreshGifts()
end

-- 刷新礼包显示
function FullMoonChooseHeroComp:refreshGifts()
    local chooseFunc = function()
        self:showChoose()
    end
    local gift = DataManager.ShopData:getActGiftConfig()[self.selectGiftId]

    self.txTips:setActive(self.selectGiftId ~= self.curGiftId)
    self.btnBuy:setActive(self.selectGiftId == self.curGiftId)
    if not DataManager.FullMoonData:isGiftBought(self.selectGiftId) then
        self.btnBuy:setTouchEnable(true)
        self.btnBuy:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_1")
        self.txBuy:setText(GFunc.getFormatPrice(gift.recharge_id))
    else
        self.btnBuy:setTouchEnable(false)
        self.btnBuy:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_1")
        self.txBuy:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20))
    end

    for index, cell in ipairs(self.rewardCells) do
        if gift.reward[index] then
            cell:setVisible(true)
            cell:refresh(gift.reward[index].id, gift.reward[index].num, true)
        elseif index == #gift.reward + 1 then
            cell:setVisible(true)
            if DataManager.FullMoonData:getChooseHeroIndex() then
                local choose = DataManager.FullMoonData:getChooseRewards(self.selectGiftId, true)[DataManager.FullMoonData:getChooseHeroIndex()]
                -- Logger.logHighlight(DataManager.FullMoonData:getChooseHeroIndex())
                cell:refresh(choose.id, choose.num, true)
                cell:refreshChoose(chooseFunc)
            else
                cell:refreshOnlyChoose(chooseFunc)
            end
        else
            cell:setVisible(false)
        end
    end
    self.rewards:RefreshLayout()

    local ids = DataManager.FullMoonData:getCurStepChooseHeroGiftIds()
    for index, item in ipairs(self.giftItems) do
        if ids[index] then
            item:setActive(true)
            local map = item:genAllChildren()
            local imgGift = map["img_gift"]
            local imgBught = map["img_bught"]
            local imgSelect = map["img_select"]

            if ids[index] == self.curGiftId then
                imgGift:setSprite(GConst.ATLAS_PATH.ACT_COMMON, "act_common_chest_2")
            elseif ids[index] > self.curGiftId then
                imgGift:setSprite(GConst.ATLAS_PATH.ACT_COMMON, "act_common_chest_3")
            else
                imgGift:setSprite(GConst.ATLAS_PATH.ACT_COMMON, "act_common_chest_1")
            end
            imgSelect:setActive(ids[index] == self.selectGiftId)
            imgBught:setActive(DataManager.FullMoonData:isGiftBought(ids[index]))
            item:addClickListener(function()
                self:onSelectGift(ids[index])
            end)
        else
            item:setActive(false)
        end
    end
    self.gifts:RefreshLayout()
end

-- 选择礼包
function FullMoonChooseHeroComp:onSelectGift(id)
    if id < self.curGiftId then
        -- 已购礼包不允许点击
        return
    end
    self.selectGiftId = id
    self:refreshGifts()
end

-- 自选弹窗
function FullMoonChooseHeroComp:showChoose()
    ModuleManager.TipsManager:showChooseRewardTips(
        I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_7),
        I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_MOON_DESC_8),
        DataManager.FullMoonData:getChooseRewards(self.selectGiftId),
        function(index)
            index = DataManager.FullMoonData:getChooseRewardsIndexByCanChooseIndex(index)
            ModuleManager.FullMoonManager:reqGiftChooseHero(index)
        end
    )
end

return FullMoonChooseHeroComp