package gameapi

import (
	"bytes"
	"strconv"
)

type requestheader struct {
	agent     string //代理编号(平台提供)
	timestamp string //时间戳 获取当前时间
	param     string //各种参数
	key       string //md5校验字符串
}

type TaskStatistics struct {
	tasks     map[string]int //已完成任务局数
	Guid      uint64
	validDate string
}

func (r requestheader) string() string {
	var buffer bytes.Buffer
	buffer.WriteString("agent=")
	buffer.WriteString(r.agent)
	buffer.WriteString("&")
	buffer.WriteString("timestamp=")
	buffer.WriteString(r.timestamp)
	buffer.WriteString("&")
	buffer.WriteString("param=")
	buffer.WriteString(r.param)
	buffer.WriteString("&")
	buffer.WriteString("key=")
	buffer.WriteString(r.key)
	return buffer.String()
}

type requestParams struct {
	s        string  //子操作类型
	account  string  //玩家账号
	ip       string  //客户端ip
	currency string  //默认 CNY
	money    float64 //加钱金额
	orderid  string  //流水号 格式:代理编号+yyyyMMddHHmmssSSS+account
	status   int     //状态码
	code     int     //错误码
}

//获取登陆参数
func (r requestParams) getLoginString() string {
	var buffer bytes.Buffer
	buffer.WriteString("(")
	buffer.WriteString("s=")
	buffer.WriteString(r.s)
	buffer.WriteString("&")
	buffer.WriteString("account=")
	buffer.WriteString(r.account)
	buffer.WriteString("&")
	buffer.WriteString("currency=")
	buffer.WriteString(r.currency)
	buffer.WriteString("&")
	buffer.WriteString("ip=")
	buffer.WriteString(r.ip)
	buffer.WriteString(")")
	return buffer.String()
}

//查询玩家是否在线
func (r requestParams) getQueryUserTaskStatisticsString() string {
	var buffer bytes.Buffer
	buffer.WriteString("(")
	buffer.WriteString("s=")
	buffer.WriteString(r.s)
	buffer.WriteString("&")
	buffer.WriteString("account=")
	buffer.WriteString(r.account)
	buffer.WriteString(")")
	return buffer.String()
}

//查询余额
func (r requestParams) getQueryBalanceString() string {
	var buffer bytes.Buffer
	buffer.WriteString("(")
	buffer.WriteString("s=")
	buffer.WriteString(r.s)
	buffer.WriteString("&")
	buffer.WriteString("account=")
	buffer.WriteString(r.account)
	buffer.WriteString("&")
	buffer.WriteString("ip=")
	buffer.WriteString(r.ip)
	buffer.WriteString(")")
	return buffer.String()
}

//加钱
func (r requestParams) getAddMoneyString() string {
	var buffer bytes.Buffer
	buffer.WriteString("(")
	buffer.WriteString("s=")
	buffer.WriteString(r.s)
	buffer.WriteString("&")
	buffer.WriteString("account=")
	buffer.WriteString(r.account)
	buffer.WriteString("&")
	buffer.WriteString("currency=")
	buffer.WriteString(r.currency)
	buffer.WriteString("&")
	buffer.WriteString("money=")
	buffer.WriteString(strconv.FormatFloat(r.money, 'f', -1, 64))
	buffer.WriteString("&")
	buffer.WriteString("orderid=")
	buffer.WriteString(string(r.orderid))
	buffer.WriteString(")")
	return buffer.String()
}

//扣钱
func (r requestParams) getDeductMoneyString() string {
	var buffer bytes.Buffer
	buffer.WriteString("(")
	buffer.WriteString("s=")
	buffer.WriteString(r.s)
	buffer.WriteString("&")
	buffer.WriteString("account=")
	buffer.WriteString(r.account)
	buffer.WriteString("&")
	buffer.WriteString("currency=")
	buffer.WriteString(r.currency)
	buffer.WriteString("&")
	buffer.WriteString("money=")
	buffer.WriteString(strconv.FormatFloat(r.money, 'f', -1, 64))
	buffer.WriteString("&")
	buffer.WriteString("orderid=")
	buffer.WriteString(string(r.orderid))
	buffer.WriteString(")")
	return buffer.String()
}

//订单查询
func (r requestParams) getQueryOrderString() string {
	var buffer bytes.Buffer
	buffer.WriteString("(")
	buffer.WriteString("s=")
	buffer.WriteString(r.s)
	buffer.WriteString("&")
	buffer.WriteString("orderid=")
	buffer.WriteString(string(r.orderid))
	buffer.WriteString(")")
	return buffer.String()
}

//查询玩家是否在线
func (r requestParams) getQueryUserOnlineString() string {
	var buffer bytes.Buffer
	buffer.WriteString("(")
	buffer.WriteString("s=")
	buffer.WriteString(r.s)
	buffer.WriteString("&")
	buffer.WriteString("account=")
	buffer.WriteString(r.account)
	buffer.WriteString(")")
	return buffer.String()
}

//踢人
func (r requestParams) getKickOutUserString() string {
	var buffer bytes.Buffer
	buffer.WriteString("(")
	buffer.WriteString("s=")
	buffer.WriteString(r.s)
	buffer.WriteString("&")
	buffer.WriteString("account=")
	buffer.WriteString(r.account)
	buffer.WriteString(")")
	return buffer.String()
}

type ResponseHeader struct {
	S string         //子操作类型
	M string         //主操作类型
	D ResponseParams //数据结果
}

type ResponseParams struct {
	Url     string  //游戏url
	Code    int     //错误码
	Money   float64 //加钱金额
	Status  int     //状态码
	Account string  //玩家账号
	Amount  float64 //可下分的金额
}
