local CoreSoulCell = class("CoreSoulCell", BaseCell)

local TITLE_TX = {
    [1] = I18N.GlobalConst.CORE_SUMMON_DESC_1,
    [2] = I18N.GlobalConst.CORE_SUMMON_DESC_2,
}
local INFO_TX = {
    [1] = I18N.GlobalConst.CORE_SUMMON_DESC_3,
    [2] = I18N.GlobalConst.CORE_SUMMON_DESC_4,
}
local BIG_REWARD_TX = {
    [1] = I18N.GlobalConst.CORE_SUMMON_DESC_9,
    [2] = I18N.GlobalConst.CORE_SUMMON_DESC_10,
}
local LAST_BIG_REWARD_TX = {
    [1] = I18N.GlobalConst.CORE_SUMMON_DESC_11,
    [2] = I18N.GlobalConst.CORE_SUMMON_DESC_12,
}

function CoreSoulCell:init()
	local uiMap = self:getUIMap()
    self.content = uiMap["core_soul_cell.content"]
    self.txTitle = uiMap["core_soul_cell.content.tx_title"]
    self.helpBtn = uiMap["core_soul_cell.content.help_btn"]
    self.txInfo = uiMap["core_soul_cell.content.tx_info"]
    self.icon = uiMap["core_soul_cell.content.icon"]
    self.txTimes = uiMap["core_soul_cell.content.tx_times"]
    self.itemIcon = uiMap["core_soul_cell.content.item_icon"]
    self.adBtn = uiMap["core_soul_cell.content.ad_btn"]
    self.adIcon = uiMap["core_soul_cell.content.ad_btn.ad_icon"]
    self.adTx = uiMap["core_soul_cell.content.ad_btn.tx"]
    self.singleBtn = uiMap["core_soul_cell.content.single_btn"]
    self.singleTitleTx = uiMap["core_soul_cell.content.single_btn.tx_title"]
    self.singleIcon = uiMap["core_soul_cell.content.single_btn.icon"]
    self.singleTx = uiMap["core_soul_cell.content.single_btn.tx"]
    self.multiBtn = uiMap["core_soul_cell.content.multi_btn"]
    self.multiTitleTx = uiMap["core_soul_cell.content.multi_btn.tx_title"]
    self.multiIcon = uiMap["core_soul_cell.content.multi_btn.icon"]
    self.multiTx = uiMap["core_soul_cell.content.multi_btn.tx"]
    self.multiIcon2 = uiMap["core_soul_cell.content.multi_btn.icon_2"]
    self.multiTx2 = uiMap["core_soul_cell.content.multi_btn.tx_2"]
end
function CoreSoulCell:refresh(id)
	self.id = id
    if self.id == GConst.ShopConst.CORE_SOUL_TYPE.NORMAL then -- 普通宝箱
        self.adBtn:addClickListener(function()
            if DataManager.ShopData:getCoreSoulAdRemainTimes() > 0 then
                SDKManager:showFullScreenAds(BIReport.ADS_CLICK_TYPE.AD_CORE_SOUL, function ()
                    ModuleManager.ShopManager:reqBuyCoreSoulBox(self.id, true, 1)
                end)
            end
        end)
        self.multiBtn:addClickListener(function()
            local costId = DataManager.ShopData:getCoreSoulCostItemId(self.id)
            local ownItemCount = DataManager.BagData.ItemData:getItemNumById(costId)
            local maxItemCount = GConst.ShopConst.CORE_SOUL_NORMAL_MAX_COUNT

            local times = math.min(ownItemCount, maxItemCount)
            if times > 0 then
                ModuleManager.ShopManager:reqBuyCoreSoulBox(self.id, false, times)
            else
                GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ITEM_NOT_ENOUGH, I18N:getConfig("item")[costId].name))
            end
        end)
    else -- 史诗宝箱
        self.singleBtn:addClickListener(function()
            local costId = DataManager.ShopData:getCoreSoulCostItemId(self.id)
            local ownItemCount = DataManager.BagData.ItemData:getItemNumById(costId)
            local ownGem = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_GEM) 
            local costGem = DataManager.ShopData:getCoreSoulSingleCostGemNum(self.id)
            if ownItemCount >= 1 then
                ModuleManager.ShopManager:reqBuyCoreSoulBox(self.id, false, 1)
            elseif ownGem >= costGem then
                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.SHOP_SOUL_SUMMON
                params.okFunc = function()
                    ModuleManager.ShopManager:reqBuyCoreSoulBox(self.id, false, 1)
                end
                GFunc.showMessageBox(params)
            else
                GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ITEM_NOT_ENOUGH, I18N:getConfig("item")[GConst.ItemConst.ITEM_ID_GEM].name))
            end
        end)
        self.multiBtn:addClickListener(function()
            local costId = DataManager.ShopData:getCoreSoulCostItemId(self.id)
            local ownItemCount = DataManager.BagData.ItemData:getItemNumById(costId)
            local ownGem = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_GEM) 
            local costGem = DataManager.ShopData:getCoreSoulSingleCostGemNum(self.id)
            local totalCount = GConst.ShopConst.CORE_SOUL_ELITE_MULTI_COUNT
            if ownItemCount >= totalCount then
                ModuleManager.ShopManager:reqBuyCoreSoulBox(self.id, false, totalCount)
            elseif ownGem >= costGem * (totalCount - ownItemCount) then
                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.SHOP_SOUL_SUMMON
                params.okFunc = function()
                    ModuleManager.ShopManager:reqBuyCoreSoulBox(self.id, false, totalCount)
                end
                GFunc.showMessageBox(params)
            else
                GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ITEM_NOT_ENOUGH, I18N:getConfig("item")[GConst.ItemConst.ITEM_ID_GEM].name))
            end
        end)
    end

    -- self.content:setSprite(GConst.ATLAS_PATH.UI_SHOP, CONTENT_BG[self.id])
    self.txTitle:setText(I18N:getGlobalText(TITLE_TX[self.id]))
    self.helpBtn:addClickListener(function()
        self:onClickHelpBtn(self.id)
    end)
    self.txInfo:setText(I18N:getGlobalText(INFO_TX[self.id]))
    -- self.icon:setSprite(GConst.ATLAS_PATH.UI_SHOP, CONTENT_BG[self.id])
    if self.id == 1 then
        self.itemIcon:setActive(false)
        local count = DataManager.ShopData:getCoreSoulBigRewardTimes(self.id)
        self.txTimes:setAnchoredPositionX(0)
        if count == 1 then
            self.txTimes:setText(I18N:getGlobalText(LAST_BIG_REWARD_TX[self.id]))
        else
            self.txTimes:setText(I18N:getGlobalText(BIG_REWARD_TX[self.id], count))
        end
    else
        self.itemIcon:setActive(true)
        self.txTimes:setText(I18N:getGlobalText(I18N.GlobalConst.SUMMON_DESC_15))
        local txW = self.txTimes:getComponent(GConst.TYPEOF_UNITY_CLASS.UI_TEXT_MESH_PRO).preferredWidth
        self.txTimes:setAnchoredPositionX(-15)
	    self.txTimes:setSizeDeltaX(txW)
    end
    if self.id == GConst.ShopConst.CORE_SOUL_TYPE.NORMAL then
        self.adBtn:setActive(true)
        self.singleBtn:setActive(false)
        self.multiBtn:setActive(true)
        -- 普通广告
        GFunc.setAdsSprite(self.adIcon)
        -- self.adBtn:setSprite("act_common_btn_blue_1" or "")
        self.adTx:setText(DataManager.ShopData:getCoreSoulAdRemainTimes() .. "/" .. DataManager.ShopData:getCoreSoulAdMaxTimes())
        if DataManager.ShopData:getCoreSoulAdRemainTimes() > 0 then
            self.adBtn:addRedPoint(40, 32, 0.9)
        else
            self.adBtn:removeRedPoint()
        end
        -- 普通多抽
        self.multiIcon2:setVisible(false)
        self.multiTx2:setVisible(false)
        local costId = DataManager.ShopData:getCoreSoulCostItemId(self.id)
        local ownItemCount = DataManager.BagData.ItemData:getItemNumById(costId)
        local maxItemCount = GConst.ShopConst.CORE_SOUL_NORMAL_MAX_COUNT
        self.multiTitleTx:setText(I18N:getGlobalText(I18N.GlobalConst.CORE_SUMMON_DESC_5, math.min(ownItemCount, maxItemCount)))
        self.multiIcon:setSprite(GFunc.getIconRes(costId))
        if ownItemCount <= 0 then -- 没有道具
            self.multiTx:setText("<color=red>0</color>/1")
            self.multiBtn:removeRedPoint()
        elseif ownItemCount <= maxItemCount then -- 有道具且在范围内,直接按当前拥有值消耗
            self.multiTx:setText(ownItemCount .. "/" .. ownItemCount)
            self.multiBtn:addRedPoint(60, 32, 0.9)
        else -- 按最大允许值消耗
            self.multiTx:setText(ownItemCount .. "/" .. maxItemCount)
            self.multiBtn:addRedPoint(60, 32, 0.9)
        end
        GFunc.centerImgAndTx(self.multiIcon, self.multiTx, 0)
    else
        self.adBtn:setActive(false)
        self.singleBtn:setActive(true)
        self.multiBtn:setActive(true)

        self.singleTitleTx:setText(I18N:getGlobalText(I18N.GlobalConst.CORE_SUMMON_DESC_7))
        self.multiTitleTx:setText(I18N:getGlobalText(I18N.GlobalConst.CORE_SUMMON_DESC_8))
        
        local costId = DataManager.ShopData:getCoreSoulCostItemId(self.id)
        local ownItemCount = DataManager.BagData.ItemData:getItemNumById(costId)
        local ownGem = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_GEM) 
        -- 史诗单抽
        if ownItemCount <= 0 then
            self.singleIcon:setSprite(GFunc.getIconRes(GConst.ItemConst.ITEM_ID_GEM))
            local costGem = DataManager.ShopData:getCoreSoulSingleCostGemNum(self.id)
            if ownGem < costGem then
                self.singleTx:setText("<color=red>" .. costGem .. "</color>")
            else
                self.singleTx:setText(costGem)
            end
            self.singleBtn:removeRedPoint() 
        else
            self.singleIcon:setSprite(GFunc.getIconRes(costId))
            self.singleTx:setText(ownItemCount .. "/1")
            self.singleBtn:addRedPoint(60, 32, 0.9)
        end
        GFunc.centerImgAndTx(self.singleIcon, self.singleTx, 0)
        -- 史诗多抽
        if ownItemCount <= 0 then -- 没有道具,全钻石
            self.multiIcon2:setVisible(false)
            self.multiTx2:setVisible(false)
            -- 显示钻石
            self.multiIcon:setSprite(GFunc.getIconRes(GConst.ItemConst.ITEM_ID_GEM))
            local costGem = DataManager.ShopData:getCoreSoulMultiCostGemNum(self.id)
            if ownGem < costGem then
                self.multiTx:setText("<color=red>" .. costGem .. "</color>")
            else
                self.multiTx:setText(costGem)
            end
            self.multiBtn:removeRedPoint()
            GFunc.centerImgAndTx(self.multiIcon, self.multiTx, 0)
        elseif ownItemCount < GConst.ShopConst.CORE_SOUL_ELITE_MULTI_COUNT then -- 需要和钻石混搭
            self.multiIcon2:setVisible(true)
            self.multiTx2:setVisible(true)
            -- 显示道具
            self.multiIcon:setSprite(GFunc.getIconRes(costId))
            self.multiTx:setText(ownItemCount)
            -- 显示钻石
            self.multiIcon2:setSprite(GFunc.getIconRes(GConst.ItemConst.ITEM_ID_GEM))
            local otherCount = GConst.ShopConst.CORE_SOUL_ELITE_MULTI_COUNT - ownItemCount
            local costGem = DataManager.ShopData:getCoreSoulSingleCostGemNum(self.id) * otherCount
            if ownGem < costGem then
                self.multiTx2:setText("<color=red>" .. costGem .. "</color>")
            else
                self.multiTx2:setText(costGem)
            end
            self.multiBtn:removeRedPoint()
            GFunc.centerImgAndTxAndImgAndTx(self.multiIcon, self.multiTx, self.multiIcon2, self.multiTx2, 0)
        else -- 可以道具全抽
            self.multiIcon2:setVisible(false)
            self.multiTx2:setVisible(false)

            self.multiIcon:setSprite(GFunc.getIconRes(costId))
            self.multiTx:setText(ownItemCount .. "/" .. GConst.ShopConst.CORE_SOUL_ELITE_MULTI_COUNT)
            self.multiBtn:addRedPoint(60, 32, 0.9)
            GFunc.centerImgAndTx(self.multiIcon, self.multiTx, 0)
        end
    end
end

function CoreSoulCell:setVisible(visible)
	self.baseObject:setVisible(visible)
end

function CoreSoulCell:onClickHelpBtn(idx)
    ModuleManager.TipsManager:showSummonProbabilityTips({
		cfgName = "summon_core",
		id = idx
	})
end

return CoreSoulCell