package gameServerMgr

import (
	"encoding/json"
	"fmt"
	"strings"

	. "Framework/managecenterModel"
	"goutil/logUtil"
	"goutil/securityUtil"
	"goutil/webUtil"
)

// 登陆助手类
type LoginUtil struct{}

// 验证登陆信息
// partnerId:合作商Id
// userId:合作商用户Id
// loginInfo:登陆信息
// isIntranet:是否是内网：true，内网；false：外网
// 返回值:
// 成功与否
// 错误对象
func (this *LoginUtil) CheckLoginInfo(partnerId int32, userId, loginInfo string, isIntranet bool) (success bool, err error) {
	// 验证用户合法性
	loginItemList := strings.Split(loginInfo, "_")
	if len(loginItemList) != 2 {
		err = fmt.Errorf("CheckLoginInfo Failed. partnerId:%d, userId:%s, loginInfo:%s", partnerId, userId, loginInfo)
		return
	}

	//将requestUrl地址进行拆分
	requestDomainList := strings.Split(loginItemList[1], ";")

	//请求的主域名
	requestDomain := ""
	if isIntranet || len(requestDomainList) == 1 {
		requestDomain = requestDomainList[0]
	} else {
		requestDomain = requestDomainList[1]
	}

	//构造请求url
	requestUrl := fmt.Sprintf("http://%s/API/CheckDynamicLoginKey.ashx", requestDomain)

	// 定义请求参数
	postDict := make(map[string]string)
	postDict["UserId"] = userId
	postDict["LoginKey"] = loginItemList[0]

	//请求url,请求头
	header := webUtil.GetFormHeader()
	transport := webUtil.NewTransport()
	transport.DisableKeepAlives = true
	transport = webUtil.GetTimeoutTransport(transport, 30)

	statusCode, returnBytes, err1 := webUtil.PostMapData(requestUrl, postDict, header, transport)
	if err1 != nil {
		err = fmt.Errorf("CheckLoginInfo Failed. partnerId:%d, userId:%s, loginInfo:%s, err:%s", partnerId, userId, loginInfo, err1)
		return
	}
	if statusCode != 200 {
		err = fmt.Errorf("CheckLoginInfo Failed. partnerId:%d, userId:%s, loginInfo:%s, statusCode:%d", partnerId, userId, loginInfo, statusCode)
		return
	}

	// 解析返回值
	returnObj := new(ReturnObject)
	if err = json.Unmarshal(returnBytes, &returnObj); err != nil {
		err = fmt.Errorf("CheckLoginInfo Failed. partnerId:%d, userId:%s, loginInfo:%s, err:%s", partnerId, userId, loginInfo, err)
		return
	}

	// 判断返回状态是否为成功
	if returnObj.Code != 0 {
		logUtil.ErrorLog(fmt.Sprintf("CheckLoginInfo Failed. partnerId:%d, userId:%s, loginInfo:%s, Code:%d, Message:%s", partnerId, userId, loginInfo, returnObj.Code, returnObj.Message))
		return
	}

	success = true

	return
}

// 本地验证登陆信息
// partnerId:合作商Id
// userId:合作商用户Id
// loginInfo:登陆信息
// 返回值:
// 成功与否
// 错误对象
func CheckDynamicTokenLocal(partnerId int32, userId, loginInfo string) (success bool, err error) {
	//1001直接返回true
	if partnerId == 1001 {
		return true, nil
	}

	//非1001渠道验证
	if len(loginInfo) == 0 {
		success = false
		err = fmt.Errorf("Err:%s", "LoginInfo is null!")
		return
	}
	// 验证用户合法性
	loginItemList := strings.Split(loginInfo, "_")
	if len(loginItemList) != 2 {
		success = false
		err = fmt.Errorf("CheckLoginInfo Failed. userId:%s, loginInfo:%s", userId, loginInfo)
		return
	}

	//生成key
	localSign := securityUtil.Md5String(userId+GetSysConfig().DynamicLoginKey+loginItemList[1], true)

	//判断签名是佛正确
	if localSign != loginItemList[0] {
		success = false
		err = fmt.Errorf("CheckLoginInfo Failed. Sign Check Failed! userId:%s,LocalSign:%s,loginInfo:%s", userId, localSign, loginInfo)
		return
	}

	success = true
	return
}

// ------------------类型定义和业务逻辑的分隔符-------------------------

var (
	LoginUtilObj = new(LoginUtil)
)
