package cooper

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

//NewMQ ...
func (that *CooperClient) NewFaceMultiSearchRequest() *FaceMultiSearchRequest {
	return &FaceMultiSearchRequest{
		Client:     that,
		apiAddress: "/baiduFace/multiSearch",
	}
}

//FaceMultiSearchRequest ...
type FaceMultiSearchRequest struct {
	Client       *CooperClient
	apiAddress   string
	FaceImageUrl string `json:"face_image_url"` //用户id
	GroupIds     string `json:"group_ids"`      //用户分组id
	IsCache      int64  `json:"is_cache"`       //
}

//SetExchangeName...
func (that *FaceMultiSearchRequest) SetFaceImage(faceImageURL string, isCache int64) *FaceMultiSearchRequest {
	that.FaceImageUrl = faceImageURL
	that.IsCache = isCache
	return that
}

//SetData...
func (that *FaceMultiSearchRequest) SetGroupID(groupID string) *FaceMultiSearchRequest {
	that.GroupIds = groupID
	return that
}

//Params ...
func (that *FaceMultiSearchRequest) Params() (url.Values, error) {
	var m = make(url.Values)
	m.Set("face_image_url", that.FaceImageUrl)
	m.Set("is_cache", utils.ToString(that.IsCache, true))
	m.Set("group_ids", that.GroupIds)
	return m, nil
}

//Do ...
func (that *FaceMultiSearchRequest) Do() ([]*FaceSearchResponse, error) {
	_rs := make([]*FaceSearchResponse, 0)
	//
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return _rs, _err
	}
	fmt.Println("响应+++++++++", _data)
	_faceInfoSlice, _ok := _data.([]interface{})
	if !_ok {
		return nil, errors.New("响应参数解析失败")
	}

	for _, _finfo := range _faceInfoSlice {
		_finfoMap, _ok := _finfo.(map[string]interface{})
		if !_ok {
			return nil, errors.New("响应参数解析失败1")
		}
		_faceSearchResponse := &FaceSearchResponse{}
		_faceToken, _ok := _finfoMap["face_token"]
		if !_ok {
			return nil, errors.New("响应参数解析失败2")
		}
		_faceSearchResponse.FaceToken = utils.ToString(_faceToken)
		_userList, _ok := _finfoMap["user_list"]
		if !_ok {
			return nil, errors.New("响应参数解析失败3")
		}
		if _userList != nil {
			_ul, _ok := _userList.([]interface{})
			if !_ok {
				return nil, errors.New("响应参数解析失败4")
			}
			for _, _ulv := range _ul {
				_userInfo := &FaceSearchResponseUserList{}
				if _err := utils.MapToStruct(_ulv, _userInfo); _err != nil {
					return nil, errors.New("响应参数解析失败5" + _err.Error())
				}
				_faceSearchResponse.UserList = append(_faceSearchResponse.UserList, _userInfo)
			}
			_rs = append(_rs, _faceSearchResponse)
		}
	}
	return _rs, nil
}
