local ShopManager = class("ShopManager", BaseModule)

-- 展示商城宝箱
function ShopManager:showBoxHeroUI(type)
	UIManager:showUI("app/ui/shop/box_hero_ui", {type = type})
end

-- 展示竞技场ad宝箱
function ShopManager:showArenaAdBoxUI(isWin)
    local params = {
        isWin = isWin
    }
	UIManager:showUI("app/ui/shop/box_hero_ui", {arena = params})
end

function ShopManager:showBoxLevelUI()
    UIManager:showUI("app/ui/shop/box_level_ui")
end

-- 宝箱奖励开箱界面 type,params,rewards
function ShopManager:showBoxOpenUI(params)
    UIManager:showUI("app/ui/shop/box_open_ui", params)
end

-- 宝箱奖励展示界面
function ShopManager:showBoxRewardUI(params)
    UIManager:showUI("app/ui/shop/box_reward_ui", params)
end

function ShopManager:showBoxLevelUpUI(params)
    UIManager:showUI("app/ui/shop/box_level_up_ui", params)
end

-- 触发弹窗礼包
function ShopManager:triggerGiftPopUI(actType, actId, showType)
    -- 入门礼包不在通用触发条件内
    if (actType == PayManager.PURCHARSE_TYPE.ACT_GIFT and actId == GConst.ShopConst.INTRODUCT_GIFT_ID) then
        self:showGiftPopUI(actType, actId, false, showType)
        DataManager.ShopData:removePopUpGift(actType, actId)
    else
        if ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.ACT_GIFT_SHOW_OPEN, true) then
            self:showGiftPopUI(actType, actId, false, showType)
            DataManager.ShopData:removePopUpGift(actType, actId)
        end
    end
end

function ShopManager:showGiftPopUI(actType, actId, onlySelf, showType)
    if GFunc.isShenhe() then
        return
    end
    if actType == PayManager.PURCHARSE_TYPE.ACT_GIFT and actId == GConst.ShopConst.FIRST_RECHARGE_ID then
        UIManager:showUI("app/ui/shop/first_recharge_pop_ui", {showType = showType})
    elseif actType == PayManager.PURCHARSE_TYPE.ACT_GIFT and actId == GConst.ShopConst.INTRODUCT_GIFT_ID then
        UIManager:showUI("app/ui/shop/introduct_pop_ui", {showType = showType})
    elseif actType == PayManager.PURCHARSE_TYPE.ACT_GIFT and (actId == GConst.ActivityConst.SKIN_GIFT_ID_1 or actId == GConst.ActivityConst.SKIN_GIFT_ID_2) then
        ModuleManager.ActivityManager:showActivityUI(GConst.ActivityConst.PANEL_TYPE.SKIN)
    elseif actType == PayManager.PURCHARSE_TYPE.ACT_GIFT and actId == GConst.ActivityConst.HERO_GIFT_ID_1 then
        ModuleManager.ActivityManager:showActivityUI(GConst.ActivityConst.PANEL_TYPE.HERO)
    elseif actType == PayManager.PURCHARSE_TYPE.CHAPTER_GIFT then
        UIManager:showUI("app/ui/shop/gift_pop_chapter_ui", {type = actType, id = actId, onlySelf = onlySelf, showType = showType})
    elseif actType == PayManager.PURCHARSE_TYPE.GROW_UP_GIFT_NEW then
        UIManager:showUI("app/ui/shop/gift_pop_grow_up_ui", {type = actType, id = actId, onlySelf = onlySelf, showType = showType})
    else
        UIManager:showUI("app/ui/shop/gift_pop_ui", {type = actType, id = actId, onlySelf = onlySelf, showType = showType})
    end
end

-- 触发金币弹窗礼包
function ShopManager:triggerCoinGiftPopUI(actId)
    self:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.ACT_GIFT, actId, BIReport.PAY_UI_SHOW_TYPE.TRIGGER_POP)
end

-- 触发成长礼包
function ShopManager:triggerGrowUpGiftPopUI(actId)
    self:triggerGiftPopUI(PayManager.PURCHARSE_TYPE.GROW_UP_GIFT_NEW, actId, BIReport.PAY_UI_SHOW_TYPE.TRIGGER_POP)
end

-- 购买每日特惠商品
function ShopManager:buyMallDailyGift(id, isAd)
    if not DataManager.ShopData:getMallDailyIsOpen() then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_OVER_DESC))
        return
    end

    local params = {id = id}
    local rewards, costs = DataManager.ShopData:getMallDailyRewardAndCost(id)

    if costs then
        if not GFunc.checkCost(costs.id, costs.num, true) then
            -- 尝试触发金币礼包
            if costs.id == GConst.ItemConst.ITEM_ID_GOLD then
                self:tryTriggerCoinGift()
            end
            return
        end
    end

    -- 二次弹窗确认
    if costs then
        local isCoin = costs.id == GConst.ItemConst.ITEM_ID_GOLD
        local params ={
            titleTx = I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_31),
            content = isCoin and I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_32) or I18N:getGlobalText(I18N.GlobalConst.SHOP_DESC_33),
            boxType = GConst.MESSAGE_BOX_TYPE.MB_OK_CANCEL,
            showToday = GConst.MESSAGE_BOX_SHOW_TODAY.HOT_SELL_BUY,
            costId = costs.id,
            costNum = costs.num,
            okFunc = function()
                local responseData = {
                    rewards = rewards,
                    costs = costs
                }
                self:sendMessage(ProtoMsgType.FromMsgEnum.BuyMallDailyReq, params, responseData, self.buyMallDailyGiftFinish, BIReport.ITEM_GET_TYPE.MALL_DAILY)
            end,
        }
	    GFunc.showMessageBox(params)
    else
        local responseData = {
            rewards = rewards,
            costs = costs
        }
        self:sendMessage(ProtoMsgType.FromMsgEnum.BuyMallDailyReq, params, responseData, self.buyMallDailyGiftFinish, BIReport.ITEM_GET_TYPE.MALL_DAILY)
    end
end

function ShopManager:buyMallDailyGiftFinish(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        GFunc.showRewardBox(result.rewards)
        DataManager.ShopData:updateMallDailyGoods(result.good_info)
    end
end

-- 重置每日特惠商品(type 1广告 2钻石)
function ShopManager:resetMallDailyGift(type)
    if not DataManager.ShopData:getMallDailyIsOpen() then
        GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.ACTIVITY_OVER_DESC))
        return
    end

    if type == 2 then -- 钻石刷新
        if not GFunc.checkCost(GConst.ItemConst.ITEM_ID_GEM, DataManager.ShopData:getMallDailyDiamondResetCost(), true) then
            return
        end
    end

    local params = {reset_type = type}
    local responseData = {}
    if type == 2 then 
        local costs = {type = GConst.REWARD_TYPE.ITEM, id = GConst.ITEM_ID_GEM, num = DataManager.ShopData:getMallDailyDiamondResetCost()}
        responseData.costs = costs
    end
    self:sendMessage(ProtoMsgType.FromMsgEnum.MallDailyResetReq, params, responseData, self.resetMallDailyGiftFinish, BIReport.ITEM_GET_TYPE.MALL_DAILY_RESET)
end

function ShopManager:resetMallDailyGiftFinish(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        local resetType = result.reqData and result.reqData.reset_type
        if resetType == 1 then -- 免费的
            DataManager.ShopData:addMallDailyAdResetCount()
        else
            DataManager.ShopData:addMallDailyDiamondResetCount()
        end
        DataManager.ShopData:initMallDaily(result.mall_daily_info)
    end
end

-- 跨天刷新每日商店数据
function ShopManager:mallDailyGiftOverDay()
    local params = {}
    local responseData = {}
    self:sendMessage(ProtoMsgType.FromMsgEnum.MallDailyOverDayReq, params, responseData, self.mallDailyGiftOverDayFinish)
end

function ShopManager:mallDailyGiftOverDayFinish(result)
    -- 消息无用 仅为了触发推送
end

function ShopManager:onMallDailyReset(result)
    if result.status == 0 then
        DataManager.ShopData:initMallDaily(result.mall_daily_info)
    end
end

-- 常规金币
function ShopManager:buyMallIdleCoin(id, isAd)
    local params = {id = id, ad = isAd}
    local rewardNum, costs = DataManager.ShopData:getCommonDailyCoinDataById(id)

    local responseData = {
        rewards = {type = GConst.REWARD_TYPE.ITEM, id = GConst.ITEM_ID_GOLD, num = rewardNum},
        costs = costs
    }
    self:sendMessage(ProtoMsgType.FromMsgEnum.BuyMallIdleReq, params, responseData, self.buyMallIdleCoinFinish, BIReport.ITEM_GET_TYPE.MALL_IDLE)
end

function ShopManager:buyMallIdleCoinFinish(result)
    if result.err_code == GConst.ERROR_STR.SUCCESS then
        GFunc.showRewardBox(result.rewards)
        DataManager.ShopData:initCommonDailyGoldGift(result.ad_count)
    end
end

-- 推送助力礼包
function ShopManager:onTriggerLevelUpGift(result)
    DataManager.ShopData:onTriggerLevelUpGift(result.current_level_up_gift)
end

-- 尝试触发金币礼包
function ShopManager:tryTriggerCoinGift()
    local triggerId = DataManager.ShopData:checkAndGetCoinGiftTriggerId()
    if triggerId then
        local params = {gold_gift_id = triggerId}
        local responseData = {}
        self:sendMessage(ProtoMsgType.FromMsgEnum.TriggerGoldGiftReq, params, responseData, self.tryTriggerCoinGiftFinish)
        return true
    else
        -- 不满足触发条件
        if EDITOR_MODE then
            Logger.log("不满足金币礼包触发条件")
        end
    end
end

function ShopManager:tryTriggerCoinGiftFinish(result)
    if result.status == 0 then
        DataManager.ShopData:onTriggerCoinGift(result.reqData.gold_gift_id)
        -- 立即触发金币弹窗
        self:triggerCoinGiftPopUI(result.reqData.gold_gift_id)
    end
end

-- 推送成长礼包
function ShopManager:onTriggerGrowUpGift(result)
    DataManager.ShopData:initGrowUpGift(result.group_up_gift)

    -- 有2种情况 1.处于英雄升级/解锁界面 立即触发弹窗 2.购买了成长礼包,此时仅标记,在奖励弹窗结束后再弹
    if UIManager:getTopUIObj() == UIManager.UI_PATH.HERO_DETAIL_UI then
        self:triggerGrowUpGiftPopUI(result.group_up_gift.current_grow_up_id)
    end
end

return ShopManager