---------------------------------------------------
-- 过关斩将/圣树试炼
---------------------------------------------------

local DrillExcel = require("excel.drill")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local CombatLogic = require("combat.CombatLogic")
local CombatDefine = require("combat.CombatDefine")
local CombatObj = require("combat.CombatObj")
local CombatPosLogic = require("combat.CombatPosLogic")
local DrillDB = require("drill.DrillDB")
local DrillLogic = require("drill.DrillLogic")

-- 操作类型
DRILL_OPER_1 = 1 -- 复活
DRILL_OPER_2 = 2 -- 回血
DRILL_OPER_3 = 3 -- 加buff

DRILL_BUFF_1 = 1 -- 攻击
DRILL_BUFF_2 = 2 -- 血量
DRILL_BUFF_3 = 3 -- 速度
DRILL_BUFF_4 = 4 -- 降低对方治疗量

-- 获取buff等级
function getBuffLv(drillData, id)
	if not drillData.buff then return 0 end
	return drillData.buff[id] or 0
end

-- 获取buff值
function getBuffValue(drillData, id)
	local buffConfig = DrillExcel.buff[id]
	if not buffConfig then return 0 end
	return buffConfig.per * getBuffLv(drillData, id)
end

-- 提升buff等级
function addBuffLv(drillData, id)
	drillData.buff = drillData.buff or {}
	drillData.buff[id] = (drillData.buff[id] or 0) + 1
end

-- 获取操作次数
function getOperCnt(drillData, optype)
	if not drillData.oper then return 0 end
	return drillData.oper[optype] or 0	
end

-- 操作次数+1
function addOperCnt(drillData, optype)
	drillData.oper = drillData.oper or {}
	drillData.oper[optype] = (drillData.oper[optype] or 0) + 1
end

-- 获取血量百分比 1表示满
function getHeroHpRate(drillData, uuid)
	if not drillData.heroStatus then return 1 end
	return drillData.heroStatus[uuid] or 1
end

-- 设置血量百分比
function setHeroHpRate(drillData, uuid, hpRate)
	hpRate = math.max(hpRate, 0)
	hpRate = math.min(hpRate, 1)
	drillData.heroStatus = drillData.heroStatus or {}
	drillData.heroStatus[uuid] = hpRate
end

-- 获取敌人血量百分比
function getObjHpRate(drillObj, index)
	if not drillObj.objStatus then return 1 end	
	return drillObj.objStatus[index] or 1
end

-- 设置敌人血量百分比
function setObjHpRate(drillObj, index, hpRate)
	hpRate = math.max(hpRate, 0)
	hpRate = math.min(hpRate, 1)	
	drillObj.objStatus = drillObj.objStatus or {}
	drillObj.objStatus[index] = hpRate
end

-- 重置buff等信息 (每关打完重置)
function clearBuff(drillData)
	drillData.buff = nil 
	drillData.oper = nil
end

function setHelp(drillData, uuid)
	drillData.helpUuids = drillData.helpUuids or {}
	drillData.helpUuids[uuid] = {}
	drillData.helpUuids[uuid].time = os.time()
end

function setHelpWin(drillData, uuid)
	drillData.helpUuids = drillData.helpUuids or {}
	if drillData.helpUuids[uuid] then
		drillData.helpUuids[uuid].win = 1
	end
end

-- 获取血量百分比 1表示满
function getHelpHpRate(drillData, uuid)
	if not drillData.helpHeroStatus then return 1 end
	return drillData.helpHeroStatus[uuid] or 1
end

-- 设置血量百分比
function setHelpHpRate(drillData, uuid, hpRate)
	hpRate = math.max(hpRate, 0)
	hpRate = math.min(hpRate, 1)
	drillData.helpHeroStatus = drillData.helpHeroStatus or {}
	drillData.helpHeroStatus[uuid] = hpRate
end

-------------------------------------------------------------------------------------------
-- 查询buff列表
function queryAttrInfo(human, drillData)
	--[[
drillData = drillData or DrillDB.getDrillDataByUuid(human.db._id)
	if not drillData then return end

    local msgRet = Msg.gc.GC_DRILL_ATTR_INFO
    msgRet.buff[0] = #DrillExcel.buff
    for i = 1, msgRet.buff[0] do
    	local buffConfig = DrillExcel.buff[i]
    	local buffNet = msgRet.buff[i]
    	local buffLv = getBuffLv(drillData, i)
    	local pp = (buffConfig.mode == 0) and 100 or 1
    	local value = buffConfig.per * buffLv / pp
    	buffNet.type = i 
    	buffNet.lv = buffLv
    	buffNet.name = buffConfig.name 
    	buffNet.desc2 = Util.format(buffConfig.desc2, value)
    end
    msgRet.oper[0] = #DrillExcel.oper
    for i = 1, msgRet.oper[0] do
    	local operConfig = DrillExcel.oper[i]
    	local operNet = msgRet.oper[i]
    	local operCnt = getOperCnt(drillData, i)
    	operNet.type = i 
    	operNet.consume = operConfig.consume + operCnt * operConfig.add
    end
	Grid.makeItem(msgRet.useItem, DrillDefine.itemID, 1)
	msgRet.price = DrillDefine.price
	Msg.send(msgRet, human.fd)
	]]
	
end

-- 回血 复活
function operHp(human, operType, heroIndex)
	if operType ~= DRILL_OPER_1 then -- 复活
		return
	end
	local heroIndexList = Util.split(heroIndex, "|")
	if #heroIndexList > 6 then
		return 
	end
	if #heroIndexList > 0  then
		for i = 1, #heroIndexList do
			local drillData = DrillDB.getDrillDataByUuid(human.db._id)
			if not drillData then return end

			if drillData.drillId > #DrillExcel.drill then
				return --已通关
			end

			local index = tonumber(heroIndexList[i])
			local heroGrid = (index > 0) and human.db.heroBag[index]
			if type(heroGrid) ~= "table" then return end

			-- 道具判断
			local operCnt = getOperCnt(drillData, operType)
			local operConfig = DrillExcel.oper[operCnt + 1]	
			if not operConfig then 
				-- 复活次数已经满了
				return 
			end
			local consume = operConfig.zuanshi
			if BagLogic.getItemCnt(human,  ItemDefine.ITEM_ZUANSHI_ID) < consume then		
				return Broadcast.sendErr(human, Util.format(Lang.COMMON_NO_ITEM, ItemDefine.getValue(ItemDefine.ITEM_ZUANSHI_ID, "name")))
			end

			local hpRate = getHeroHpRate(drillData, heroGrid.uuid)
			if hpRate == 1 then return end -- 满血
			
			if operType == DRILL_OPER_1 then -- 复活
				--死亡英雄不能回血 只能复活
				if hpRate > 0 then return end	    
			elseif operType == DRILL_OPER_2 then -- 回血
				--没死亡的英雄不能复活 只能回血
				if hpRate == 0 then return end
			else		
				return
			end
			
			-- 先扣道具
			BagLogic.delItem(human, ItemDefine.ITEM_ZUANSHI_ID, consume, "drill_oper")
			
			-- 再设置血量
			setHeroHpRate(drillData, heroGrid.uuid, 1)
			DrillDB.updateDrillData(drillData)				
		end	
		local drillData = DrillDB.getDrillDataByUuid(human.db._id)
		if not drillData then return end
		addOperCnt(drillData, operType)
		DrillDB.updateDrillData(drillData)	
		Broadcast.sendErr(human, Lang.DRILL_FUHUO_SUCCESS)	

		DrillLogic.fuhuoQuery(human)
		queryAttrInfo(human, drillData)
		DrillLogic.sendHeroList(human, drillData.heroStatus)
	end	
end

-- 加buff 针对关卡  没有BUF 了
function operBuff(human)
	--[[
	local drillData = DrillDB.getDrillDataByUuid(human.db._id)
	if not drillData then return end

	if drillData.drillId > #DrillExcel.drill then
		return --已通关
	end

	-- 道具判断
	local operType = DRILL_OPER_3
	local operCnt = getOperCnt(drillData, operType)
	local operConfig = DrillExcel.oper[operType]	
	local consume = operConfig.consume + operCnt * operConfig.add
    if BagLogic.getItemCnt(human, DrillDefine.itemID) < consume then		
	    return Broadcast.sendErr(human, Util.format(Lang.COMMON_NO_ITEM, ItemDefine.getValue(DrillDefine.itemID, "name")))
    end

	-- 先扣道具
	BagLogic.delItem(human, DrillDefine.itemID, consume, "drill_oper")
	addOperCnt(drillData, operType)
	-- 再给buff
	local buffType = math.random(1, #DrillExcel.buff)
	addBuffLv(drillData, buffType)
	DrillDB.updateDrillData(drillData)	
	queryAttrInfo(human, drillData)
    local msgRet = Msg.gc.GC_DRILL_OPER_BUFF 	
	msgRet.buffType = buffType
	Msg.send(msgRet, human.fd)
	]]
	
end
