package gameServerMgr

import (
	"testing"
	"time"

	"goutil/timeUtil"
	"goutil/typeUtil"
)

func TestActiveServer(t *testing.T) {
	var managecenterUrl = "https://managecenterapitest-ds3.7qule.com/"
	var serverGroupId int32 = 1000

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}
}

func TestGetServerGroup(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//#region测试获取服务器组
	serverGroup := GetServerGroup()
	if serverGroup == nil {
		t.Fatalf("获取服务器组[GetServerGroup]失败：serverGroup = nil")
	}

	if serverGroup.Id != serverGroupId {
		t.Fatalf("获取服务器组[GetServerGroup]失败,期望[%d],结果[%d]", serverGroupId, serverGroup.Id)
	}
	//#endregion
}

func TestGetDbConfig(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//#region测试获取服务器组
	serverGroup := GetServerGroup()
	if serverGroup == nil {
		t.Fatalf("获取服务器组[GetServerGroup]失败：serverGroup = nil")
	}

	if serverGroup.Id != serverGroupId {
		t.Fatalf("获取服务器组[GetServerGroup]失败,期望[%d],结果[%d]", serverGroupId, serverGroup.Id)
	}
	//#endregion

	//#region 测试获取数据库配置
	/*
		{
		  "GameDB": "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_player_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=20;maximumpoolsize=200;command timeout=60;",
		  "LogDB": "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_log_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=20;maximumpoolsize=200;command timeout=60;",
		  "GameModelDB": "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_model_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=10;maximumpoolsize=10;command timeout=60;"
		}
	*/
	gameDb := "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_player_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=20;maximumpoolsize=200;command timeout=60;"
	logDb := "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_log_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=20;maximumpoolsize=200;command timeout=60;"
	modelDb := "DataSource=10.1.0.4;port=3306;UserId=admin;Password=MOQIkaka$#@!1234;Database=h5xh_model_master;Allow Zero Datetime=true;charset=utf8;pooling=true;MinimumPoolSize=10;maximumpoolsize=10;command timeout=60;"

	dbConfig, err := serverGroup.GetDBConfig()
	if err != nil {
		t.Fatalf("获取服务器数据库配置[GetDBConfig]失败：%v", error)
	}

	if dbConfig.GameDB != gameDb {
		t.Fatalf("获取服务器数据库配置[GetDBConfig]失败,GameDB,期望[%s],结果[%s]", gameDb, dbConfig.GameDB)
	}

	if dbConfig.LogDB != logDb {
		t.Fatalf("获取服务器数据库配置[GetDBConfig]失败,LogDB,期望[%s],结果[%s]", logDb, dbConfig.LogDB)
	}

	if dbConfig.GameModelDB != modelDb {
		t.Fatalf("获取服务器数据库配置[GetDBConfig]失败,GameModelDB,期望[%s],结果[%s]", modelDb, dbConfig.GameModelDB)
	}
	//#endregion
}

func TestGetServerItem(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var partnerId int32 = 1001
	var serverGroupId int32 = 20001
	var serverId = serverGroupId

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//#region测试获取服务器
	serverItem, exists := GetServerItem(partnerId, serverId)
	if !exists || serverItem == nil {
		t.Fatalf("获取服务器[GetServerItem][%d]失败,PartnerId[%d],服务器不存在！", partnerId, serverId)
	}

	if serverItem.Id != serverId {
		t.Fatalf("获取服务器[GetServerItem]失败,期望[%d],结果[%d]", serverId, serverItem.Id)
	}
	//#endregion
}

func TestGetServerName(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var partnerId int32 = 1001
	var serverGroupId int32 = 20001
	var serverId = serverGroupId

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//测试获取服务器名称
	var expectServerName = "开发测试服"
	serverName := GetServerName(partnerId, serverId)
	if serverName != expectServerName {
		t.Fatalf("获取服务器名称[GetServerName]失败,期望[%s],结果[%s]", expectServerName, serverName)
	}
}

func TestIfServerExists(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var partnerId int32 = 1001
	var serverGroupId int32 = 20001
	var serverId = serverGroupId

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//测试服务器是否存在
	if !IfServerExists(partnerId, serverId) {
		t.Fatalf("判断服务器是否存在[IfServerExists]失败,PartnerId[%d],服务器[%d]不存在", partnerId, serverId)
	}
}

func TestGetPartnerServerPairString(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//获取合作商、服务器的组合字符串
	expectStr := "1001_20001|6666001_20001|"
	partnerServerPairStr := GetPartnerServerPairString()
	if expectStr != partnerServerPairStr {
		t.Fatalf("获取合作商、服务器的组合字符串[GetPartnerServerPairString]失败,期望[%s],结果[%s]", expectStr, partnerServerPairStr)
	}
}

func TestGetLoginKey(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var partnerId int32 = 1001
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//#region测试获取登录密钥
	var expectStr = "a0482eaf-14e8-4a65-950e-864214f62da5"
	loginKey, exists := GetLoginKey(partnerId)
	if !exists {
		t.Fatalf("获取登录密钥[GetLoginKey]失败,未找到渠道[%d]配置", partnerId)
	}

	if expectStr != loginKey {
		t.Fatalf("获取登录密钥[GetLoginKey]失败,期望[%s],结果[%s]", expectStr, loginKey)
	}
	//#endregion
}

func TestGetServerOpenDate(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//测试获取开服日期 2020/4/21 20:19:56
	expectOpenDate := time.Date(2020, 4, 21, 20, 19, 56, 0, time.Local)
	openTimeTick := GetServerOpenDate()
	openDate, _ := typeUtil.DateTime(openTimeTick)
	if openDate != expectOpenDate {
		t.Fatalf("获取服务器开服日期[GetServerOpenDate]失败,期望[%s],结果[%s]", timeUtil.ToDateTimeString(expectOpenDate), timeUtil.ToDateTimeString(openDate))
	}
}

func TestServerOpenDays(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//测试获取服务器开服天数
	now := time.Now()
	nowDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
	expectOpenDate := time.Date(2020, 4, 21, 0, 0, 0, 0, time.Local)
	expectOpenDays := int32(nowDate.Sub(expectOpenDate).Hours()/24) + 1
	openDays := ServerOpenDays()
	if expectOpenDays != openDays {
		t.Fatalf("获取服务器开服天数[ServerOpenDays]失败,期望[%d],结果[%d]", expectOpenDays, openDays)
	}
}

func TestCheckMaintainStatus(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//测试服务器维护检查
	expectMsg := "维护中"
	maintainMessage, isMaintaining := CheckMaintainStatus()
	if isMaintaining && expectMsg != maintainMessage {
		t.Fatalf("服务器维护检查[CheckMaintainStatus]失败,期望维护消息[%s],结果维护消息[%s]", expectMsg, maintainMessage)
	}
}

func TestCheckNewGameVersion(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var partnerId int32 = 1001
	var serverGroupId int32 = 20001
	var serverId = serverGroupId

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//#region 测试检查是否有新版本

	var gameVersionId int32 = 55         //老版本
	var expectGameVersionUrl = "testUrl" //已配置的游戏版本地址（IOS用）
	gameVersionUrl, exist := CheckNewGameVersion(partnerId, serverId, gameVersionId)
	if !exist {
		t.Fatalf("检查是否有新版本[CheckNewGameVersion]失败,未找到渠道[%d]配置或检查版本[%d]失败", partnerId, gameVersionId)
	} else if gameVersionUrl != expectGameVersionUrl {
		t.Fatalf("检查是否有新版本[CheckNewGameVersion]失败,期望版本地址[%s],结果版本地址[%s]", expectGameVersionUrl, gameVersionUrl)
	}

	gameVersionId = 100 //当前版本
	_, exist = CheckNewGameVersion(partnerId, serverId, gameVersionId)
	if exist {
		t.Fatalf("检查是否有新版本[CheckNewGameVersion]失败,渠道[%d],版本[%d]不应有更新", partnerId, gameVersionId)
	}

	//#endregion
}

func TestCheckNewResourceVersion(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var partnerId int32 = 1001
	var serverGroupId int32 = 20001
	var serverId = serverGroupId

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//#region 测试检查新资源版本
	partnerId = 6666001
	var gameVersionId int32 = 100
	resourceVersionName := "1587372891_9eab40313feec913cace0adf0fe05341" //非最新资源版本号
	_, exist := CheckNewResourceVersion(partnerId, serverId, gameVersionId, resourceVersionName)
	if !exist {
		t.Fatalf("检查新资源版本[CheckNewResourceVersion]失败,渠道[%d],服务器[%d],游戏版本Id[%d],资源版本号[%s],应有更新!", partnerId, serverId, gameVersionId, resourceVersionName)
	}

	gameVersionId = 100
	resourceVersionName = "1587374043_cee48a8611276d3e3450782a1585c1a3" //最新资源版本号
	_, exist = CheckNewResourceVersion(partnerId, serverId, gameVersionId, resourceVersionName)
	if exist {
		t.Fatalf("检查新资源版本[CheckNewResourceVersion]失败,渠道[%d],服务器[%d],游戏版本Id[%d],资源版本号[%s],不应有更新!", partnerId, serverId, gameVersionId, resourceVersionName)
	}

	gameVersionId = 123456 //不存在的游戏版本
	resourceVersionName = "1587374043_cee48a8611276d3e3450782a1585c1a3"
	_, exist = CheckNewResourceVersion(partnerId, serverId, gameVersionId, resourceVersionName)
	if exist {
		t.Fatalf("检查新资源版本[CheckNewResourceVersion]失败,渠道[%d],服务器[%d],游戏版本Id[%d],资源版本号[%s],不应有更新!", partnerId, serverId, gameVersionId, resourceVersionName)
	}

	//#endregion
}

func TestIsInWhiteList(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var partnerId int32 = 1001
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//测试白名单
	partnerId = 1001
	var userId = "446bfd6ccccd4229aa295686f9e5855b" //已配置的白名单用户Id
	if !IsInWhiteList(partnerId, userId) {
		t.Fatalf("检查是否用户在白名单中[IsInWhiteList]失败,渠道[%d],UserId[%s]", partnerId, userId)
	}
}

// 测试获取服务器组
func TestGetOtherConfigInfo(t *testing.T) {
	var managecenterUrl = "http://managecenterapitest-xxx.79yougame.com/"
	var partnerId int32 = 1001
	var serverGroupId int32 = 20001

	//激活器服务器,获取mc配置
	error := ActiveServer(managecenterUrl, serverGroupId)
	if error != nil {
		t.Fatalf("激活服务器出现错误：%v", error)
	}

	//#region测试获取其他配置
	var configKey = "AppKey"
	var expectConfigValue = "a0482eaf-14e8-4a65-950e-864214f62da5" //已配置的配置值
	configValue, exist, error := GetOtherConfigInfo(partnerId, configKey)
	if error != nil {
		t.Fatalf("获取渠道其他配置[GetOtherConfigInfo]失败,渠道[%d],ConfigKey[%s]", partnerId, configKey)
	}
	if !exist {
		t.Fatalf("获取渠道其他配置[GetOtherConfigInfo]失败,渠道[%d],ConfigKey[%s],渠道或配置不存在！", partnerId, configKey)
	}

	if configValue != expectConfigValue {
		t.Fatalf("获取渠道其他配置[GetOtherConfigInfo]失败,期望[%s],结果[%s]", expectConfigValue, configValue)
	}
	//#endregion
}
