-----------------------------------------------------------------
-- 文件名       ：  DarkForbiddenAreaLogic.lua
-- 文件说明     ：  幽暗禁地
-- 创建时间     :   2025/07/11
-- 创建人       :   FC
-----------------------------------------------------------

local Msg = require("core.Msg")
local Util = require("common.Util")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local DarkConf = require("excel.darkForbiddenara")
local CombatDefine = require("combat.CombatDefine")
local CombatLogic = require("combat.CombatLogic")
local MonsterGrid = require("monster.MonsterGrid")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")


local DARKFORBIDDENAREA_OPEN_LV = 100  -- 可参与活动等级
local DARKFORBIDDENAREA_BIT = 30       -- 2进制位

local DARKFORBIDDENAREA_NO_SAODANG = 0  -- 不可扫荡
local DARKFORBIDDENAREA_SAODANG = 1 -- 可扫荡


---------------------------------- 内部调用 ----------------------------------------------
-- 获取怪物配置
local function DarkForbiddenArea_GetMonsterConf()
    return DarkConf.darkenemy
end

-- 获取奖励配置
local function DarkForbiddenArea_GetPrizeConf()
    return DarkConf.darkprize
end

-- 创建DB数据
local function DarkForbiddenArea_CreateDB(human)
    local tConfig = DarkForbiddenArea_GetMonsterConf()

    human.db.DarkForArea = {}

    for nID, v in pairs(tConfig) do
        local nIndex = math.ceil(nID / DARKFORBIDDENAREA_BIT)

        if not human.db.DarkForArea[nIndex] then
            human.db.DarkForArea[nIndex] = DARKFORBIDDENAREA_NO_SAODANG
        end
    end
end

-- 设置DB数据
local function DarkForbiddenArea_SetDB(human, nID)
    if not human.db.DarkForArea then
        DarkForbiddenArea_CreateDB(human)
    end

    local nIndex = math.ceil(nID / DARKFORBIDDENAREA_BIT)
    local byteIndex = nID % DARKFORBIDDENAREA_BIT

    human.db.DarkForArea[nIndex] = Util.setBit(human.db.DarkForArea[nIndex], byteIndex)
end

-- 获取DB数据
local function DarkForbiddenArea_GetDB(human, nID)
    if not human.db.DarkForArea then
        DarkForbiddenArea_CreateDB(human)
    end

    local nIndex = math.ceil(nID / DARKFORBIDDENAREA_BIT)
    local byteIndex = nID % DARKFORBIDDENAREA_BIT

    --print("[DarkForbiddenArea_GetDB] nID = "..nID.." nIndex = "..nIndex.." byteIndex = "..byteIndex)
    return Util.getBit(human.db.DarkForArea[nIndex], byteIndex) > 0 and DARKFORBIDDENAREA_SAODANG or DARKFORBIDDENAREA_NO_SAODANG
end

-- 获取奖励
local function DarkForbiddenArea_GetPrize(nPrizeID, nRandPrizeNum)
    local tPrizeConf = DarkForbiddenArea_GetPrizeConf()
    local tPrizeInfo, nAllWeight = {}, 0
    for nID, v in ipairs(tPrizeConf) do
        if v.nPrizeID == nPrizeID then
            nAllWeight = nAllWeight + v.nWeight
            table.insert(tPrizeInfo, {nWeight = nAllWeight, tPrize = v.tPrize})
        end
    end

    if nil == _G.next(tPrizeInfo) then
        return nil
    end

    local tItem = {}
    for i = 1, nRandPrizeNum, 1 do
        local nRandWeight = math.random(1, nAllWeight)
        for _, v in pairs(tPrizeInfo) do
            if nRandWeight <= v.nWeight then
                local nGoodsID = v.tPrize[1]
                local nGoodsNum = v.tPrize[2]
                if not tItem[nGoodsID] then
                    tItem[nGoodsID] = 0
                end

                tItem[nGoodsID] = tItem[nGoodsID] + nGoodsNum
                break
            end
        end
    end

    if nil == _G.next(tItem) then
        return nil
    end

    local tGoods = {}
    for nGoodsID, nGoodsNum in pairs(tItem) do
        table.insert(tGoods, {nGoodsID, nGoodsNum})
    end

    return tGoods
end

---------------------------------- 客户端请求 ----------------------------------------------
-- 请求幽暗禁地信息
function DarkForAreaLogic_Query(human, nType)
    if not human.db.DarkForArea then
        DarkForbiddenArea_CreateDB(human)
    end

    local tMsgData = Msg.gc.GC_DARK_FORBIDDEN_AREA_QUEY
    local bFillUse, bFillHave = false, false
    local nAllLen, nSendLen = 0, 0
    local tMonstConf = DarkForbiddenArea_GetMonsterConf()
    for nID, v in pairs(tMonstConf) do
        if v.nDifficulty == nType then
            nAllLen = nAllLen + 1
        end
    end

    tMsgData.tMonster[0] = 0
    tMsgData.bEnd = 0
   
    for nID, v in pairs(tMonstConf) do
        if v.nDifficulty == nType then
            nSendLen = nSendLen  + 1
            tMsgData.tMonster[0] = tMsgData.tMonster[0] + 1
            local tData = tMsgData.tMonster[tMsgData.tMonster[0]]
            tData.nID = nID
            tData.szName = v.szMonsterName
            tData.nMonsterShowID = v.nMonsterStyling
            tData.nMonsterLv = v.nMonsterLv
            tData.bSaoDang = DarkForbiddenArea_GetDB(human, nID)

            tData.nPower = 0
            --怪物数据
            local objList = MonsterGrid.createMonsterObjListByMonsterOutID(v.nMonsterID)
            if objList then
                for _, monster in ipairs(objList) do
                    tData.nPower = tData.nPower + (monster.power or 0)
                end
            end

            -- 奖励配置
            local tPrizeData = tData.tPrize
            tPrizeData[0] = 0

            local nPrizeID = v.nPrizeID
            local tPrizeConf = DarkForbiddenArea_GetPrizeConf()
            for _, value in ipairs(tPrizeConf) do
                if value.nPrizeID == nPrizeID and value.tShow and value.tShow == 1 then
                    tPrizeData[0] =  tPrizeData[0] + 1
                    Grid.makeItem(tPrizeData[tPrizeData[0]], value.tPrize[1], value.tPrize[2])
                end
            end

            if false == bFillUse then
                Grid.makeItem(tMsgData.tUseGoods, v.nUseGood, v.nUseNum)
                bFillUse = true
            end

            if false == bFillHave then
                tMsgData.nHaveNum = BagLogic.getItemCnt(human, v.nUseGood)
                bFillHave = true
            end

            if tMsgData.tMonster[0] >= 5 then
                tMsgData.bEnd = nSendLen == nAllLen and 1 or 0
                Msg.send(tMsgData, human.fd)
                tMsgData.tMonster[0] = 0
            end
        end
    end

    if tMsgData.tMonster[0] > 0 and tMsgData.bEnd == 0 then
        tMsgData.bEnd = 1
        Msg.send(tMsgData, human.fd)
    end
    print("[DarkForAreaLogic_Query] 下发协议结束")
end

-- 请求扫荡
function DarkForAreaLogic_SaoDang(human, nID)
    local tConfig = DarkForbiddenArea_GetMonsterConf()
    if not tConfig or not tConfig[nID] then
        return
    end

    local bSaoDang = DarkForbiddenArea_GetDB(human, nID)
    if DARKFORBIDDENAREA_NO_SAODANG == bSaoDang then
        print("[CG_DARK_FORBIDDEN_AREA_SAODANG] 当前没有解锁扫荡")
        return
    end

    local tMonsterConf = tConfig[nID]
    local nNowGoodsNum = BagLogic.getItemCnt(human, tMonsterConf.nUseGood)
    if tMonsterConf.nUseNum > nNowGoodsNum then
        print("[CG_DARK_FORBIDDEN_AREA_SAODANG] 当前扫荡物品不足")
        return
    end

    local tItem = DarkForbiddenArea_GetPrize(tMonsterConf.nPrizeID, tMonsterConf.nRandPrizeNum)
    if nil ~= tItem and nil ~= _G.next(tItem) then
        BagLogic.delItem(human, tMonsterConf.nUseGood, tMonsterConf.nUseNum, "darkforarea")
        
        BagLogic.addItemList(human, tItem, "darkforarea")

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1215)
        --DarkForAreaLogic_Query(human,  tMonsterConf.nDifficulty)
    end
end


---------------------------------- 战斗相关 ----------------------------------------------
-- 调用开始战斗
function fight(human, args, combatType)
    if not human or combatType ~= CombatDefine.COMBAT_TYPE31 then
        print("[arkForbiddenAreaLogic_fight] 不正确的参数 combatType = "..combatType)
        return
    end

    if not args[1] or not args[2] then
        print("[arkForbiddenAreaLogic_fight] 不存在对应的 args  参数")
        return
    end

    local nID = tonumber(args[2])
    local tMonsterConf = DarkForbiddenArea_GetMonsterConf()
    print("[arkForbiddenAreaLogic_fight] args1 = "..args[1].." args2 = "..args[2])
    if not nID  then
        print("[DarkForbiddenAreaLogic_fight] 参数不正确没有传递正确的对应对战的怪物ID ")
        return
    end

    if not tMonsterConf[nID] then
        print("[DarkForbiddenAreaLogic_fight]不存在对应配置 ")
        return
    end
    
   
    if not nID or not tMonsterConf[nID] then
        print("[DarkForbiddenAreaLogic_fight] 参数不正确没有传递正确的对应对战的怪物ID ")
        return
    end

    local nUseGood, nUseNum = tMonsterConf[nID].nUseGood, tMonsterConf[nID].nUseNum
    local nNowGoodsNum = BagLogic.getItemCnt(human, nUseGood)
    if nNowGoodsNum < nUseNum then
        print("[DarkForbiddenAreaLogic_fight] 挑战消耗的物品不足 nNowGoodsNum = "..nNowGoodsNum.." nUseNum = "..nUseNum)
        return
    end

    -- 删除挑战消耗物品
    BagLogic.delItem(human, nUseGood, nUseNum, "darkforarea")

    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1215)

    print("[arkForbiddenAreaLogic_fight] 开始进入战斗")
	CombatLogic.combatBegin(human, 1001, args, CombatDefine.COMBAT_TYPE31, nID)

end

-- 获取人物对战列表
function getCombatObjList(human, side, args)
    if side ~= CombatDefine.ATTACK_SIDE then
		return
	end
	-- local uuid = args[1]
	-- if not uuid then return end
	-- local fakeHuman = CombatLogic.createCombatFakeHuman(uuid)
	-- if not fakeHuman then return end
	return CombatLogic.getHumanObjList(human, CombatDefine.COMBAT_TYPE31)
end

-- 获取机器人对应战斗ID
function getCombatMonsterOutID(human, side, args)
    if side ~= CombatDefine.DEFEND_SIDE then
        return
    end

    local nID = tonumber(args[2])
    local tMonsterConf = DarkForbiddenArea_GetMonsterConf()
    if not nID or not tMonsterConf[nID] then
        print("[DarkForbiddenAreaLogic_getCombatMonsterOutID] 参数不正确没有传递正确的对应对战的怪物ID ")
        return
    end

    local nMonsterOutID = tMonsterConf[nID].nMonsterID
    if not nMonsterOutID then
        print("[DarkForbiddenAreaLogic_getCombatMonsterOutID] 配置中没有配置正确的怪物ID")
        return
    end

    return nMonsterOutID
end

-- 挑战结束
function onFightEnd(human, result, combatType, nIndexID, combatInfo)
    if CombatDefine.COMBAT_TYPE31 ~= combatType then
        return
    end

    local nID = tonumber(nIndexID)
    if CombatDefine.RESULT_WIN == result then
        local tMonsterConf = DarkForbiddenArea_GetMonsterConf()
        if not tMonsterConf[nID] then
            print("[DarkForbiddenAreaLogic_onFightEnd] 挑战结束, 但是获取不到对应的怪物配置 nID = "..nID)
            return
        end

        local tItem = DarkForbiddenArea_GetPrize(tMonsterConf[nID].nPrizeID, tMonsterConf[nID].nRandPrizeNum)
        if nil ~= tItem and nil ~= _G.next(tItem) then
            combatInfo.rewardItem =  combatInfo.rewardItem  or {}
            for i, v in pairs(tItem) do
                BagLogic.addItem(human, v[1], v[2], "darkforarea")
                combatInfo.rewardItem[i] = combatInfo.rewardItem[i] or { }
                combatInfo.rewardItem[i] = {v[1], v[2]}
            end
            -- BagLogic.addItemList(human, tItem, "darkforarea")
        end

        DarkForbiddenArea_SetDB(human, nID)
    end
end
