package pposV2

import (
	"PPOSUtils/utils"
	"errors"
	"fmt"
	"net/url"
)

//NewFaceVerifyRequest ...
func (that *PPOSV2Client) NewFaceVerifyRequest() *FaceVerifyRequest {
	return &FaceVerifyRequest{
		Client:     that,
		apiAddress: "/gate/gate/readForAlfFace",
	}
}

type FaceVerifyResponse struct {
	Success  bool   `json:"Success"`  //
	Pass     bool   `json:"Pass"`     //
	UserName string `json:"UserName"` //
	Msg      string `json:"Msg"`      //
}

//GateCheckRequest ...
type FaceVerifyRequest struct {
	Client        *PPOSV2Client
	apiAddress    string
	desEncryptKey string
	userID        string `json:"user_id"`   //用户id
	deviceID      string `json:"device_id"` //用户分组id
	faceURL       string `json:"face_url"`  //
}

func (that *FaceVerifyRequest) SetDesEncryptKey(desEncryptKey string) *FaceVerifyRequest {
	that.desEncryptKey = desEncryptKey
	return that
}

//SetFaceURL...
func (that *FaceVerifyRequest) SetFaceURL(faceURL string) *FaceVerifyRequest {
	that.faceURL = faceURL
	return that
}

func (that *FaceVerifyRequest) SetDeviceID(deviceID string) *FaceVerifyRequest {
	that.deviceID = deviceID
	return that
}

func (that *FaceVerifyRequest) SetUserID(userID string) *FaceVerifyRequest {
	that.userID = userID
	return that
}

//Params ...
func (that *FaceVerifyRequest) Params() (url.Values, error) {
	_callbackDataParams := make(url.Values)
	_callbackDataParams.Set("user_id", that.userID)
	_callbackDataParams.Set("device_id", that.deviceID)
	_callbackDataParams.Set("face_url", that.faceURL)
	_callbackData, _ := utils.DesEncrypt([]byte(_callbackDataParams.Encode()), []byte(that.desEncryptKey))
	_checkParams := make(url.Values)
	_checkParams.Set("data", fmt.Sprintf("%X", _callbackData))
	return _checkParams, nil
}

//Do ...
func (that *FaceVerifyRequest) Do() (*FaceVerifyResponse, error) {
	_rs := new(FaceVerifyResponse)
	//
	_data, _err := that.Client.PostV2(that.apiAddress, that)
	if _err != nil {
		return _rs, _err
	}
	//
	_vData, _ok := _data.(map[string]interface{})
	if !_ok {
		return _rs, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	if _err := that.Client.ParseResponeMp(_rs, _vData); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	return _rs, nil
}
