package protocol

import (
	"fmt"
	"opp/models"
	"sync/atomic"
)

const (
	LoginTypePassPord = "signInPassword"
	LoginTypeSmdcode  = "signInCaptcha"
)

const (
	DeviceType = 1
)

const TokenExpire = 60 * 15
const RefreshTokenExipre = 3600 * 24 * 30 * 3 //刷新token 三个月过期

const SmscodeDayLimitTime = 10 //短信验证码每天最多发10次

var Nums = []byte("0123456789")

type RequestHeader struct {
	TimeStamp   string
	Version     string
	Uuid        string
	Sign        string
	DeviceType  int
	AppProject  string
	AccessToken string
	Uid         int64 //用户基本信息Id
	CompanyId   int64
	UserId      int64 //UserId 唯一标识,唯一关联所有用户信息(=user_company.id)

	requestId string //请求编号 md5
	reqIndex  int64  //请求链序号
}

func (reqHead *RequestHeader) SetRequestId(addString ...string) {
	if len(addString) == 0 {
		return
	}
	reqHead.requestId = addString[0]
}
func (reqHead *RequestHeader) GetRequestId() string {
	atomic.AddInt64(&reqHead.reqIndex, 1)
	return fmt.Sprintf("%s.%d", reqHead.requestId, reqHead.reqIndex)
}

/*Login */
type LoginRequest struct {
	Uid      int64  `json:"cuid" valid:"Required;"`
	Token    string `json:"credentials" valid:"Required;"`
	ClientId string `json:"clientId"`
}
type LoginResponse struct {
	AuthCode string `json:"authCode"`
}

/*Authorize 模块认证 (登录V2版本)*/
type AuthorizeRequest struct {
	Token    string `json:"credentials" valid:"Required;"` //登录凭证
	ClientId string `json:"clientId"`                      //客户端密钥
	Uid      int64  `json:"cuid" valid:"Required;"`        //统一用户中心用户 UID
	Cid      int64  `json:"cid" valid:"Required;"`         //统一用户中心公司 ID
	Muid     int64  `json:"muid" valid:"Required;"`        //企业平台中的用户 UID
}
type AuthorizeResponse struct {
	AuthCode string `json:"authCode"`
}

/*统一用户中心登录*/
type UserCenterLoginRequest struct {
	Phone     string `json:"phone"`
	PassWord  string `json:"password"`
	Code      string `json:"code"`
	GrantType string `json:"grantType" valid:"Required"`
	ClientId  string `json:"clientId" valid:"Required"`
}

type UserCenterLoginResponse struct {
	Id              int64  `json:"id"`
	Phone           string `json:"phone"`
	NickName        string `json:"nickname"`
	Avatar          string `json:"avatar"`
	Token           string `json:"token"`
	Accid           string `json:"accid"`
	CustomerAccount string `json:"customerAccount"`
}

/*SmsCode*/
type SmsCodeRequest struct {
	Phone    string `json:"phone" valid:"Required;Mobile"`
	Content  string `json:"-"`
	SendType string `json:"send_type"` //sms_login_code  sms_change_mobile

}
type SmsCodeResponse struct {
	Code string `json:"-"`
}

/*UpdateDevice*/
type UpdateDeviceRequest struct {
	ClientId    string `json:"clientId" valid:"Required"`
	DeviceToken string `json:"deviceToken"`
}
type UpdateDeviceResponse struct {
}

/*AccessToken */
type AccessTokenRequest struct {
	ClientId     string `json:"clientId" valid:"Required"`
	ClientSecret string `json:"clientSecret" valid:"Required"`
	AuthCode     string `json:"authCode" valid:"Required"`
}
type AccessTokenResponse struct {
	RefreshToken string `json:"refreshToken"`
	AccessToken  string `json:"accessToken"`
	ExpiresIn    int    `json:"expiresIn"`
}

/*RefreshToken */
type RefreshTokenRequest struct {
	ClientId     string `json:"clientId" valid:"Required"`
	ClientSecret string `json:"clientSecret" valid:"Required"`
	RefreshToken string `json:"refreshToken" valid:"Required"`

	Uid       int64  `json:"-"`
	LoginType string `json:"-"`
}
type RefreshTokenResponse struct {
	RefreshToken string `json:"refreshToken"`
	AccessToken  string `json:"accessToken"`
	ExpiresIn    int    `json:"expiresIn"`
}

type Access struct {
	Uid          int64
	Type         string
	AccessToken  string
	RefreshToken string
}

/*CheckToken */
type CheckTokenRequest struct {
	Token string
}
type CheckTokenResponse struct {
	IsValid  bool //true:过期  false:没有过期
	UserInfo *models.UserAuth
}

/*CheckUuid */
type CheckUuidRequest struct {
	Uuid string
}
type CheckUuidResponse struct {
}

/*CheckIm */
type CheckImRequest struct {
	Uid       string
	Uname     string
	Icon      string
	IsCreated bool
}
type CheckImResponse struct {
	ImToken   string //net im token
	CsAccount int64  //客服id
}

/*Revoke */
type RevokeRequest struct {
}
type RevokeResponse struct {
}