local Msg = require("core.Msg")
local Util = require("common.Util")
local Lang = require("common.Lang")
local OpenServerRankDB = require("present.OpenServerRankDB")
local Broadcast = require("broadcast.Broadcast")
local OpenAct = require("present.OpenAct")
local YunYingLogic = require("yunying.YunYingLogic")
local DrawCardLogic = require("drawCard.DrawCardLogic")
local Log = require("common.Log")

--[[
	开服排名活动 充值排名、招募排名
    local:
        getOpenActParam()       -- 通过排名类型获得开服活动参数
        getDayLeftTime()        -- 获得当天剩余时间秒
        getActLeftTime()        -- 得到活动需要用到的时间秒
	public:
		isActive()				-- 激活状态
		isBaseOpen()			-- 开启活动基本条件	
		isOpen()                -- 活动开启	
		getLeftTime()           -- 得到活动剩余时间	
		query()					-- 发送活动信息
        onCharge()			    -- 充值回调
		onDrawCard()			-- 招募回调
        onHuangjingTower()      -- 爬塔回调
		onZeroAll()             -- 零点处理回调
--]]

local function OpenServerRank_WriteLog(human, sztext)
    if human then
        sztext = sztext.." _id = "..human.db._id.." name = "..human.db.name
    end
    
    Log.write(Log.LOGID_OSS_COMMON, sztext)
end

local function getOpenActParam(rankType)
    if rankType == OpenServerRankDB.RANK_TYPE_DAY_TOPUP or 
       rankType == OpenServerRankDB.RANK_TYPE_TOTAL_TOPUP  then return OpenAct.OPEN_ACT_TOPUP_RANK end

    if rankType == OpenServerRankDB.RANK_TYPE_DAY_DRAW_CARD or 
       rankType == OpenServerRankDB.RANK_TYPE_TOTAL_DRAW_CARD then return OpenAct.OPEN_ACT_DRAW_CARD_RANK end

    if rankType == OpenServerRankDB.RANK_TYPE_TOTAL_TOWER then return OpenAct.OPEN_ACT_TOWER_RANK end
end

local function getDayLeftTime()
	local now = os.time()
 	local endTime = Util.getDayStartTime(now) + 86400
    
    return endTime - now
end

local function getActLeftTime(rankType)
    if rankType == OpenServerRankDB.RANK_TYPE_DAY_TOPUP or 
        rankType == OpenServerRankDB.RANK_TYPE_DAY_DRAW_CARD then 
        return getDayLeftTime()
    end

    if rankType == OpenServerRankDB.RANK_TYPE_TOTAL_TOPUP or 
        rankType == OpenServerRankDB.RANK_TYPE_TOTAL_DRAW_CARD or 
        rankType == OpenServerRankDB.RANK_TYPE_TOTAL_TOWER then 
        local openActParam = getOpenActParam(rankType)
        local _,leftTime = OpenAct.getOpenActTime(openActParam)
        return leftTime
    end

    return 0
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isBaseOpen(human, YYInfo, funcConfig, noSend)
    if human.db.lv < funcConfig.openLv then
        if not noSend then
            local str = Util.format(Lang.ROLE_LEV_ERROR, funcConfig.openLv)
            return Broadcast.sendErr(human, str)
        end
    end

    local flag = OpenAct.getOpenActTime(funcConfig.param)
    if not flag then return end

    return true
end

function isOpen(human, YYInfo, funcConfig)
	if isBaseOpen(human, YYInfo, funcConfig, true) then return true end
end

function getLeftTime(human, YYInfo, funcConfig)
	local _,leftTime = OpenAct.getOpenActTime(funcConfig.param)
 	return leftTime
end

function query(human, rankType)
	local actLeftTime = getActLeftTime(rankType)
	local maxSendRank = OpenServerRankDB.getMaxSendRank(rankType)

	local msgRet = Msg.gc.GC_OPEN_SERVER_RANK_QUERY
	msgRet.type = rankType
	msgRet.leftTime = actLeftTime
	OpenServerRankDB.wrapOwnerData(rankType, msgRet.ownerData, human.db._id)
	
    for rank = 1,maxSendRank do
		OpenServerRankDB.wrapOpenServerRankList(rankType, msgRet.list[rank], rank)
	end
	msgRet.list[0] = maxSendRank

	Msg.send(msgRet, human.fd)
end

function rewardQuery(human, rankType)
    OpenServerRankDB.rewardQuery(human, rankType)
end

function onCharge(human, price, funcID, buyID)
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
	if not funcConfig then return end

    if not isOpen(human, nil, funcConfig) then return end

    if funcConfig.param == OpenAct.OPEN_ACT_TOPUP_RANK then
        OpenServerRankDB.onValueAdd(human, OpenServerRankDB.RANK_TYPE_DAY_TOPUP, price)
        OpenServerRankDB.onValueAdd(human, OpenServerRankDB.RANK_TYPE_TOTAL_TOPUP, price)
        OpenServerRank_WriteLog(human, "[OpenServerRankDB_onCharge] 玩家充值进行上榜操作 price = "..price.."buyID = "..buyID)
    end
end

function onDrawCard(human, funcID, value, drawType)
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
	if not funcConfig then return end

    if not isOpen(human, nil, funcConfig) then return end

    if funcConfig.param == OpenAct.OPEN_ACT_DRAW_CARD_RANK then
        if drawType == DrawCardLogic.DRAWCARD_ID2 or 
            drawType == DrawCardLogic.DRAWCARD_ID5 or 
            drawType == DrawCardLogic.DRAWCARD_ID6 then
           OpenServerRankDB.onValueAdd(human, OpenServerRankDB.RANK_TYPE_DAY_DRAW_CARD, value)
           OpenServerRankDB.onValueAdd(human, OpenServerRankDB.RANK_TYPE_TOTAL_DRAW_CARD, value)
            OpenServerRank_WriteLog(human, "[OpenServerRankDB_onDrawCard] 玩家抽卡进行上榜操作 value = "..value.."drawType = "..drawType)
        end
    end
end

function onHuangjingTower(human,funcID, parameter)
    local funcConfig = YunYingLogic.getFuncConfig(funcID)
	if not funcConfig then return end

    if not isOpen(human, nil, funcConfig) then return end

    if funcConfig.param == OpenAct.OPEN_ACT_TOWER_RANK then
        local towerLv = human.db.tower and human.db.tower.lv or 0
        OpenServerRankDB.onValueSet(human, OpenServerRankDB.RANK_TYPE_TOTAL_TOWER, towerLv)
        OpenServerRank_WriteLog(human, "[OpenServerRankDB_onHuangjingTower] 玩家进行上榜操作 towerLv = "..towerLv)
    end 
end

function onZeroAll(funcID)
    OpenServerRank_WriteLog(nil, "[OpenServerRank_onZeroAll] 整点进行发奖励操作 funcID = "..funcID)
    OpenServerRankDB.onRewardSend(funcID)
end