local ActivitySkinComp = class("ActivitySkinComp", LuaComponent)

function ActivitySkinComp:init()
    local uiMap = self:getUIMap()
    self.txTitle = uiMap["skin_panel.banner.title.tx_title"]
    self.txBannerDesc1 = uiMap["skin_panel.banner.content.desc1.tx_desc1"]
    self.txBannerDesc2 = uiMap["skin_panel.banner.content.desc2.tx_desc2"]
    -- 礼包1
    self.spineSkin1 = uiMap["skin_panel.skin1.spine_skin1"]
    self.txDesc1 = uiMap["skin_panel.skin1.bg_title.tx_title"]
    self.btnBuy1 = uiMap["skin_panel.skin1.btn_buy"]
    self.txBuy1 = uiMap["skin_panel.skin1.btn_buy.tx_buy"]
    self.rewardCells1 = {}
    for i = 1, 3 do
        table.insert(self.rewardCells1, uiMap["skin_panel.skin1.rewards.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
    end
    -- 礼包2
    self.spineSkin2 = uiMap["skin_panel.skin2.spine_skin2"]
    self.txDesc2 = uiMap["skin_panel.skin2.bg_title.tx_title"]
    self.btnBuy2 = uiMap["skin_panel.skin2.btn_buy"]
    self.txBuy2 = uiMap["skin_panel.skin2.btn_buy.tx_buy"]
    self.rewardCells2 = {}
    for i = 1, 3 do
        table.insert(self.rewardCells2, uiMap["skin_panel.skin2.rewards.reward_cell_" .. i]:addLuaComponent(GConst.TYPEOF_LUA_CLASS.REWARD_CELL))
    end

    self.btnBuy1:addClickListener(function()
        PayManager:purchasePackage(GConst.ActivityConst.SKIN_GIFT_ID_1, PayManager.PURCHARSE_TYPE.ACT_GIFT)
    end)
    self.btnBuy2:addClickListener(function()
        PayManager:purchasePackage(GConst.ActivityConst.SKIN_GIFT_ID_2, PayManager.PURCHARSE_TYPE.ACT_GIFT)
    end)
    LocalData:recordTodayActSummerWatchedSkin()
end

function ActivitySkinComp:refresh()
    self.txTitle:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_5))

    -- 礼包1
    local gift1 = DataManager.ShopData:getActGiftConfig()[GConst.ActivityConst.SKIN_GIFT_ID_1]
    local skinId1,heroId1
    for idx, reward in ipairs(gift1.reward) do
        if ConfigManager:getConfig("item")[reward.id].type == GConst.ItemConst.ITEM_TYPE.SKIN then
            skinId1 = DataManager.SkinData:getSkinIdByItemId(reward.id)
            heroId1 = DataManager.SkinData:getHeroIdBySkinId(skinId1)
        end
    end
    if skinId1 and heroId1 then
        self.spineSkin1:getSkeletonGraphic().enabled = false
        self.spineSkin1:loadAssetAsync(DataManager.SkinData:getModelId(skinId1), function()
            self.spineSkin1:getSkeletonGraphic().enabled = true
            self.spineSkin1:playAnim("idle", true, true, true)
        end, true)
        self.txBannerDesc1:setText(DataManager.HeroData:getHeroById(heroId1):getName() .. "-" .. DataManager.SkinData:getName(skinId1) .. "(" .. I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_9) .. ")")
    end
    self.txDesc1:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_6))
    for index, cell in ipairs(self.rewardCells1) do
        if gift1.reward[index] then
            cell:setVisible(true)
            cell:refreshByConfig(gift1.reward[index])
        else
            cell:setVisible(false)
        end
    end
    if DataManager.ShopData:getGiftRemainBuyNum(GConst.ActivityConst.SKIN_GIFT_ID_1) > 0 then
        self.btnBuy1:setTouchEnable(true)
        self.btnBuy1:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_2")
        self.txBuy1:setText(GFunc.getFormatPrice(gift1.recharge_id))
    else
        self.btnBuy1:setTouchEnable(false)
        self.btnBuy1:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_2")
        self.txBuy1:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20))
    end

    -- 礼包2
    local gift2 = DataManager.ShopData:getActGiftConfig()[GConst.ActivityConst.SKIN_GIFT_ID_2]
    local skinId2,heroId2
    for idx, reward in ipairs(gift2.reward) do
        if ConfigManager:getConfig("item")[reward.id].type == GConst.ItemConst.ITEM_TYPE.SKIN then
            skinId2 = DataManager.SkinData:getSkinIdByItemId(reward.id) 
            heroId2 = DataManager.SkinData:getHeroIdBySkinId(skinId2) 
        end
    end
    if skinId2 and heroId2 then
        self.spineSkin2:getSkeletonGraphic().enabled = false
        self.spineSkin2:loadAssetAsync(DataManager.SkinData:getModelId(skinId2), function()
            self.spineSkin2:getSkeletonGraphic().enabled = true
            self.spineSkin2:playAnim("idle", true, true, true)
        end, true)
        self.txBannerDesc2:setText(DataManager.HeroData:getHeroById(heroId2):getName() .. "-" .. DataManager.SkinData:getName(skinId2) .. "(" .. I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_9) .. ")")
    end
    self.txDesc2:setText(I18N:getGlobalText(I18N.GlobalConst.ACT_DESC_7))
    for index, cell in ipairs(self.rewardCells2) do
        if gift2.reward[index] then
            cell:setVisible(true)
            cell:refreshByConfig(gift2.reward[index])
        else
            cell:setVisible(false)
        end
    end
    if DataManager.ShopData:getGiftRemainBuyNum(GConst.ActivityConst.SKIN_GIFT_ID_2) > 0 then
        self.btnBuy2:setTouchEnable(true)
        self.btnBuy2:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_green_2")
        self.txBuy2:setText(GFunc.getFormatPrice(gift2.recharge_id))
    else
        self.btnBuy2:setTouchEnable(false)
        self.btnBuy2:setSprite(GConst.ATLAS_PATH.COMMON, "common_btn_grey_2")
        self.txBuy2:setText(I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_20))
    end
end

return ActivitySkinComp