-- 根据战力获取档位
local function GetIndexByBattle(battle, default)
	if type(battle) ~= "number" then
		return default
	end
	
	if battle >= 0 and battle <= 300 then
		return 1
	elseif battle >= 301 and battle <= 375 then
		return 2
	elseif battle >= 376 and battle <= 450 then
		return 3
	elseif battle >= 451 and battle <= 999999 then
		return 4
	end

	return default
end

-- 激情服奖励配置
local RewardPassion = {
    [1] = {
     { ItemType=3327104, Num=1, Mono=0, },
     { ItemType=723700, Num=1, Mono=0, },
     { ItemType=725126, Num=1, Mono=0, }, 
    },
    [2] = {
	 { ItemType=3327104, Num=1, Mono=0, },
     { ItemType=723700, Num=1, Mono=0, },
     { ItemType=725126, Num=1, Mono=0, }, 
    },
    [3] = {
	 { ItemType=3327104, Num=1, Mono=0, },
     { ItemType=723700, Num=1, Mono=0, },
     { ItemType=725126, Num=1, Mono=0, }, 
    },
    [4] = {
	 { ItemType=3327104, Num=1, Mono=0, },
	 { ItemType=723700, Num=1, Mono=0, },
     { ItemType=725126, Num=1, Mono=0, }, 
    },
}

-- 各个档位奖品,最多四个
local Reward = {
    [1] = {
     { ItemType=3327104, Num=1, Mono=0, },
     { ItemType=723700, Num=1, Mono=0, },
     { ItemType=725126, Num=1, Mono=0, }, 
    },
    [2] = {
	 { ItemType=3327104, Num=1, Mono=0, },
     { ItemType=723700, Num=1, Mono=0, },
     { ItemType=725126, Num=1, Mono=0, }, 
    },
    [3] = {
	 { ItemType=3327104, Num=1, Mono=0, },
     { ItemType=723700, Num=1, Mono=0, },
     { ItemType=725126, Num=1, Mono=0, }, 
    },
    [4] = {
	 { ItemType=3327104, Num=1, Mono=0, },
	 { ItemType=723700, Num=1, Mono=0, },
     { ItemType=725126, Num=1, Mono=0, }, 
    },
}

-- 老兵服奖励配置
local RewardOldSoldier= {
    [1] = {
     { ItemType=1088000, Num=1, Mono=0, },
	 { ItemType=3008057, Num=1, Mono=0, },
    },
    [2] = {
     { ItemType=1088000, Num=2, Mono=0, },
	 { ItemType=3008057, Num=1, Mono=0, },
    },
    [3] = {
     { ItemType=1088000, Num=3, Mono=0, },
	 { ItemType=3008057, Num=1, Mono=0, },
    },
    [4] = {
	 { ItemType=1088000, Num=4, Mono=0, },
     { ItemType=3008058, Num=1, Mono=0, },
    },
}

-- 全部比赛显示 按页读取
local OldSoldierAllPKDesc = {
	-- 界面显示总页数
	page = 1,
	[1] = "STR_INI_LUA_BATTLEPK_OSALLPKDESC_1",
	-- [2] = "<color=0xffc38963>376-450战斗力:</color> 19:45 - 20:00\n \n \n<color=0xffc38963>451以上战斗力:</color> 21:45 - 22:00\n \n \n",
}

-- 个人可参赛列表显示
local OldSoldierPKDesc = {
	[1] = "STR_INI_LUA_BATTLEPK_OSPKDESC_1",
	[2] = "STR_INI_LUA_BATTLEPK_OSPKDESC_2",
	[3] = "STR_INI_LUA_BATTLEPK_OSPKDESC_3",
	[4] = "STR_INI_LUA_BATTLEPK_OSPKDESC_4"
}

-- 各个档位战斗力范围文本
local OldSoldierBattleRange = {
	[1] = "STR_INI_LUA_BATTLEPK_OSBATTLERANGE_1",
	[2] = "STR_INI_LUA_BATTLEPK_OSBATTLERANGE_2",
	[3] = "STR_INI_LUA_BATTLEPK_OSBATTLERANGE_3",
	[4] = "STR_INI_LUA_BATTLEPK_OSBATTLERANGE_4",
}


local function GetReward(level, index, key, default)
    local tReward = nil
    if (UserData_IsPassionServer() == true) then
        tReward = RewardPassion
	elseif (UserData_IsOldSoldierServer() == true) then
		tReward = RewardOldSoldier
	elseif (UserData_IsCoinsServer() == true) then
		tReward = RewardOldSoldier
    else
        tReward = Reward
    end

    local tItems = tReward[level]
    if (tItems == nil) then
        return default
    end

    local tItem = tItems[index]
    if (tItem == nil) then
        return default
    end

    local ret = tItem[key]
    if (ret == nil) then
        return default
    end

    return ret
end

-- 各个档位开启时间
local BeginTime = {
	[1] = "10:45",
	[2] = "15:45",
	[3] = "19:45",
	[4] = "21:45",
}

local function GetBeginTimeByIndex(index, default)
	if BeginTime[index] ~= nil then
		return BeginTime[index]
	end
	
	return default
end

-- 各个档位战斗力范围文本
local BattleRange = {
	[1] = "STR_INI_LUA_BATTLEPK_BATTLERANGE_1",
	[2] = "STR_INI_LUA_BATTLEPK_BATTLERANGE_2",
	[3] = "STR_INI_LUA_BATTLEPK_BATTLERANGE_3",
	[4] = "STR_INI_LUA_BATTLEPK_BATTLERANGE_4",
}

local function GetBattleRangeByIndex(index, default)
	if (UserData_IsOldSoldierServer() == true) or (UserData_IsCoinsServer() == true) then		
		if OldSoldierBattleRange[index] ~= nil then
			return OldSoldierBattleRange[index]
		end
	end

	if BattleRange[index] ~= nil then
		return BattleRange[index]
	end
	
	return default
end

-- 个人可参赛列表显示
local PKDesc = {
	[1] = "STR_INI_LUA_BATTLEPK_PKDESC_1",
	[2] = "STR_INI_LUA_BATTLEPK_PKDESC_2",
	[3] = "STR_INI_LUA_BATTLEPK_PKDESC_3",
	[4] = "STR_INI_LUA_BATTLEPK_PKDESC_4"
}

local function GetPKDescByIndex(index, default)
	if (UserData_IsOldSoldierServer() == true) or (UserData_IsCoinsServer() == true) then		
		if OldSoldierPKDesc[index] ~= nil then
			return OldSoldierPKDesc[index]
		end
	end

	if PKDesc[index] ~= nil then
		return PKDesc[index]
	end
	
	return default
end

-- 全部比赛显示 按页读取
local AllPKDesc = {
	-- 界面显示总页数
	page = 1,
	[1] = "STR_INI_LUA_BATTLEPK_ALLPKDESC_1",
	-- [2] = "<color=0xffc38963>376-450战斗力:</color> 19:45 - 20:00\n \n \n<color=0xffc38963>451以上战斗力:</color> 21:45 - 22:00\n \n \n",
}

local function GetAllPKInfo(index, default)
	if (UserData_IsOldSoldierServer() == true) or (UserData_IsCoinsServer() == true) then		
		if OldSoldierAllPKDesc[index] ~= nil then
			return OldSoldierAllPKDesc[index]
		end
	end

	if AllPKDesc[index] ~= nil then
		return AllPKDesc[index]
	end
	
	return default
end

-- 前往参赛-寻路配置
-- local AutoRunTarget = {
	-- mapid = 1002,
	-- x = 456,
	-- y = 291,
	-- npcid = 4990,
-- }
-- 前往参赛-寻路配置
local AutoRunTarget = {
	[0] = {
		mapid = 1002,
		x = 456,
		y = 291,
		npcid = 4990,
	},
	[1] = {
		mapid = 10364,
		x = 295,
		y = 141,
		npcid = 4990,
	},	
	[2] = {
		mapid = 10364,
		x = 297,
		y = 146,
		npcid = 4990,
	},
}

local BattlePKText = {
	-- 默认文本
	[0] = {
		["Time"] = "STR_BATTLE_PK_TIP5",
		["Way"] = "STR_BATTLE_PK_TIP6",
		["Rule"] = "STR_BATTLE_PK_TIP7",
	},

	-- 老兵服
	[1] = {
		["Time"] = "STR_BATTLE_PK_TIP5_OS",
		["Way"] = "STR_BATTLE_PK_TIP6_OS",
		["Rule"] = "STR_BATTLE_PK_TIP7_OS",
	},
}


local function GetAutoRunTargetInfo(index, default)
	local tTarget = nil
	if (UserData_IsPassionServer() == true) then
		tTarget = AutoRunTarget[1]
	elseif (UserData_IsOldSoldierServer() == true) then
		tTarget = AutoRunTarget[2]
	elseif (UserData_IsCoinsServer() == true) then
		tTarget = AutoRunTarget[2]
	else
		tTarget = AutoRunTarget[0]
	end

	if tTarget[index] ~= nil then
		return tTarget[index]
	end
	
	return default
end

-- local function GetAutoRunTargetInfo(index, default)
	-- if AutoRunTarget[index] ~= nil then
		-- return AutoRunTarget[index]
	-- end
	
	-- return default
-- end

-----------新增------------
local function GetRewardNum(level, default)
    local tReward = nil
    if (UserData_IsPassionServer() == true) then
        tReward = RewardPassion
	elseif (UserData_IsOldSoldierServer() == true) then
		tReward = RewardOldSoldier
	elseif (UserData_IsCoinsServer() == true) then
		tReward = RewardOldSoldier
    else
        tReward = Reward
    end

    return #tReward[level]
end

local function GetBattlePKText(key)
	local tText = nil
	if (UserData_IsPassionServer() == true) then
		tText = BattlePKText[0]
	elseif (UserData_IsOldSoldierServer() == true) then
		tText = BattlePKText[1]
	elseif (UserData_IsCoinsServer() == true) then
		tText = BattlePKText[1]
	else
		tText = BattlePKText[0]
	end

	if tText[key] ~= nil then
		return tText[key]
	end
	
	return nil
end


-- 清理变量释放内存
local function Unload()
	AllPKDesc = nil
	PKDesc = nil
    Reward = nil
	BattleRange = nil
	BeginTime = nil
	AutoRunTarget = nil
end

-- 通过require传给其他Lua脚本使用
local BattlePK = {
	GetRewardNum = GetRewardNum,
	GetAllPKInfo = GetAllPKInfo,
	GetPKDescByIndex = GetPKDescByIndex,
	GetBeginTimeByIndex = GetBeginTimeByIndex,
	GetBattleRangeByIndex = GetBattleRangeByIndex,
	GetReward = GetReward,
	GetBattleLevel = GetBattleLevel,
	GetIndexByBattle = GetIndexByBattle,
	GetAutoRunTargetInfo = GetAutoRunTargetInfo,
	GetBattlePKText = GetBattlePKText,
    Unload = Unload,
}

-- C++程序使用的global函数名
BattlePK_GetRewardNum = BattlePK.GetRewardNum
BattlePK_GetAutoRunTargetInfo = BattlePK.GetAutoRunTargetInfo
BattlePK_GetIndexByBattle = BattlePK.GetIndexByBattle
BattlePK_GetAllPKInfo = BattlePK.GetAllPKInfo
BattlePK_GetPKDescByIndex = BattlePK.GetPKDescByIndex
BattlePK_GetBeginTimeByIndex = BattlePK.GetBeginTimeByIndex
BattlePK_GetBattleRangeByIndex = BattlePK.GetBattleRangeByIndex
BattlePK_GetReward = BattlePK.GetReward
BattlePK_GetRewardByBattle = BattlePK.GetRewardByBattle
BattlePK_GetBattlePKText = BattlePK.GetBattlePKText
BattlePK_Unload = BattlePK.Unload

-- require返回值
return BattlePK
