-- 战区争霸(DB)

local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local Log = require("common.Log")
local AreaBattleDefine = require("areaBattle.AreaBattleDefine")
local CombatVideo = require("combat.CombatVideo")
local CombatDefine = require("combat.CombatDefine")

local dBUpdate = {_id=nil}
local dBUpdateField = {}

-- 用于展示的简单录像数据
VideoShowCache = {}

-- DB
AreaBattleData = nil
-- {
--     state = 1,
--     joinSrvArr = {serverId1, serverId2, serverId3}, -- 参加活动的服务器列表
--     matchSrvArr = { {serverId1, serverId3}, {serverId2, serverId4}  }, -- 匹配成功的服务器列表
--     nowRoundStartTi = nil, -- 本轮活动开启时间
--     upLoadStartTi = nil,   -- 玩家战斗数据上传时间,

--     serverList = {    -- 服务器信息列表
--         [serverId] = {
--             winTimes = 0,
--             defeatTimes = 0,
--             winPlayerArr = {playerUuid1, playerUuid2,},
--             playerInfoArr = {
--                 {
--                     uuid = uuid,
--                     power = power,
--                     name = name,
--                     head = head,
--                     headFrame = headFrame,
--                 },
--             },
--         },
--     }
-- }

local function initData()
    AreaBattleData = {}
    AreaBattleData.state = AreaBattleDefine.STATE_END
    AreaBattleData.joinSrvArr = {}
    AreaBattleData.matchSrvArr = {}
    AreaBattleData.serverList = {}

    LuaMongo.insert(DB.db_area_battle, AreaBattleData)
end

local function resetData()
    AreaBattleData.state = AreaBattleDefine.STATE_END
    AreaBattleData.joinSrvArr = {}
    AreaBattleData.matchSrvArr = {}
    AreaBattleData.serverList = {}
    AreaBattleData.nowRoundStartTi = os.time()
    AreaBattleData.upLoadStartTi = nil

    dBUpdate._id = AreaBattleData._id
    LuaMongo.update(DB.db_area_battle, dBUpdate, AreaBattleData)
end

local function loadData()
    LuaMongo.find(DB.db_area_battle)

    local data = {}
    if LuaMongo.next(data) then
        AreaBattleData = data
    else
        initData()
    end
end

-- 修改db单个字段
local function updateValue(key, value)
	if not key then return end

    if value then
		dBUpdateField["$set"] = {[key]=value}
		dBUpdateField["$unset"] = nil
	else
		dBUpdateField["$set"] = nil
		dBUpdateField["$unset"] = {[key]=1}
	end

	dBUpdate._id = AreaBattleData._id
	LuaMongo.update(DB.db_area_battle, dBUpdate, dBUpdateField)
end

-- 生成录像数据中的英雄展示数据
local function genObjShowInfo(net, playerData, posArr, objList)
    net.name = playerData.name
    net.power = playerData.zhandouli
    net.head = playerData.head
    net.headFrame = playerData.headFrame
    net.playerLv = playerData.lv
    net.uuid = playerData.uuid
    net.heroList = {}

    local len = 0
    for _,pos in ipairs(posArr) do
        local grid = objList and objList[pos]
        if grid and grid.id ~= 0 then
            len = len + 1
            net.heroList[len] = {
                heroId = grid.id,
                heroColor = grid.quality or 0,
                heroStar = grid.star or 0,
                heroLv = grid.lv or 0,
                heroCamp = grid.camp or 999
            }
        end
    end
end

-- 生成某个服的录像展示数据
local function genSrvVideoSHowData(videoSecondIdx)
    local videoArr = CombatVideo.getCombatVideosByType(CombatVideo.VIDEOTYPE_AREABATTLE, videoSecondIdx)
    if #videoArr == 0 then
        return
    end

    VideoShowCache[videoSecondIdx] = {}

    for k, videoInfo in ipairs(videoArr) do
        local combatInfo = videoInfo.combatInfo
        VideoShowCache[videoSecondIdx][k] = VideoShowCache[videoSecondIdx][k] or {}
        local videoData = VideoShowCache[videoSecondIdx][k]

        videoData.atkData = {}
        genObjShowInfo(videoData.atkData, combatInfo.attacker, CombatDefine.SIDE2POS[CombatDefine.ATTACK_SIDE], combatInfo.objList)

        videoData.defenerData = {}
        genObjShowInfo(videoData.defenerData, combatInfo.defender, CombatDefine.SIDE2POS[CombatDefine.DEFEND_SIDE], combatInfo.objList)

        videoData.atkFormation = combatInfo.atkFormation
        videoData.defenerFormation = combatInfo.defFormation
        videoData.round = combatInfo.result.round
        videoData.maxRound = combatInfo.maxRound
        videoData.time = combatInfo.time
        videoData.videoUuid = videoInfo._id
        videoData.result = combatInfo.isWin == true and 1 or 0
    end
end



function initAfterStart()
    if _G.is_middle ~= true then return end
    loadData()
end

function ResetData()
    resetData()
end




function GetNowRoundStartTi()
    return AreaBattleData and AreaBattleData.nowRoundStartTi or 0
end


function GetUpLoadStartTi()
    return AreaBattleData and AreaBattleData.upLoadStartTi or 0
end

function UpdateUpLoadStartTi(newUpLoadStartTi)
    AreaBattleData = AreaBattleData or {}
    AreaBattleData.upLoadStartTi = newUpLoadStartTi
    updateValue("upLoadStartTi", newUpLoadStartTi)
end



function GetState()
    return AreaBattleData and AreaBattleData.state or AreaBattleDefine.STATE_END
end

function UpdateState(newState)
    AreaBattleData = AreaBattleData or {}
    AreaBattleData.state = newState
    updateValue("state", newState)

    if newState == AreaBattleDefine.STATE_PREPARE then
        local now = os.time()

        AreaBattleData.nowRoundStartTi = now
        updateValue("nowRoundStartTi", AreaBattleData.nowRoundStartTi)
    end
end


function GetJoinSrvArr()
    return AreaBattleData.joinSrvArr
end

function UpdateJoinSrvArr(newJoinSrvArr)
    AreaBattleData = AreaBattleData or {}
    AreaBattleData.joinSrvArr = newJoinSrvArr
    updateValue("joinSrvArr", newJoinSrvArr)
end


function GetServerList()
    return AreaBattleData.serverList
end

function UpdateServerList(newServerList)
    AreaBattleData = AreaBattleData or {}
    AreaBattleData.serverList = newServerList
    updateValue("serverList", newServerList)
end


function GetMatchSrvArr()
    return AreaBattleData.matchSrvArr
end

function UpdateMatchSrvArr(newMatchSrvArr)
    AreaBattleData = AreaBattleData or {}
    AreaBattleData.matchSrvArr = newMatchSrvArr
    updateValue("matchSrvArr", newMatchSrvArr)
end


function GetSrvVideoShowData(videoSecondIdx)
    if not videoSecondIdx then
        return
    end

    if not VideoShowCache[videoSecondIdx] then
        genSrvVideoSHowData(videoSecondIdx)
    end

    return VideoShowCache[videoSecondIdx]
end

function ResetVideoShowCache()
    VideoShowCache = {}
end