package cooper

import (
	"errors"
	"net/url"
)

//new... 人脸下发
func (that *CooperClient) NewHikFaceIssueRequest() *FaceIssueRequest {
	return &FaceIssueRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/hikFace/issue",
			publicParamsSlice: []string{
				"login_info", //登录设备参数
				"card_no",    //会员卡号
				"image",      //人脸图像
			},
		},
	}
}

//new... 人脸更新
func (that *CooperClient) NewHikFaceUpdateRequest() *FaceIssueRequest {
	return &FaceIssueRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/hikFace/update",
			publicParamsSlice: []string{
				"login_info", //登录设备参数
				"card_no",    //会员卡号
				"image",      //人脸图像
			},
		},
	}
}

//new... 人脸删除
func (that *CooperClient) NewHikFaceDeleteRequest() *FaceIssueRequest {
	return &FaceIssueRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/hikFace/delete",
			publicParamsSlice: []string{
				"login_info", //登录设备参数
				"card_no",    //会员卡号
			},
		},
	}
}

//SetParams...
func (that *FaceIssueRequest) SetLoginInfo(value string) *FaceIssueRequest {
	that.initParams()
	that.params.Set("login_info", value) //登录设备参数
	return that
}
func (that *FaceIssueRequest) SetCardNo(value string) *FaceIssueRequest {
	that.initParams()
	that.params.Set("card_no", value) //会员卡ID，即（第三方保存的用户唯一ID）
	return that
}
func (that *FaceIssueRequest) SetImage(value string) *FaceIssueRequest {
	that.initParams()
	that.params.Set("image", value) //人脸图像
	return that
}
func (that *FaceIssueRequest) SetCardPassword(value string) *FaceIssueRequest {
	that.initParams()
	that.params.Set("card_password", value) //卡密码、 非必填
	return that
}
func (that *FaceIssueRequest) SetName(value string) *FaceIssueRequest {
	that.initParams()
	that.params.Set("name", value) //会员名称、非必填
	return that
}
func (that *FaceIssueRequest) SetDomain(value string) *FaceIssueRequest {
	that.initParams()
	that.params.Set("domain", value) //代理穿透内网的域名
	return that
}

//Response...
type NewFaceIssueResponse struct {
	DeviceNo string `json:"device_no"` //设备序列号
	Status   int64  `json:"status"`    //下发状态
	Remark   string `json:"remark"`    //备注
	UserId   string `json:"user_id"`   //用户ID
}

//Request...
type FaceIssueRequest struct {
	Request
}

//Params ...
func (that *FaceIssueRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.publicParamsSlice != nil {
		for _, _v := range that.publicParamsSlice {
			if _params.Get(_v) == "" {
				return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
			}
		}
	}
	return _params, nil
}

//Do ...
func (that *FaceIssueRequest) Do() ([]*NewFaceIssueResponse, error) {
	//
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//删除人脸
	if that.apiAddress == "/hikFace/delete" {
		return nil, nil
	}
	//更新人脸
	if that.apiAddress == "/hikFace/update" {
		return nil, nil
	}
	//下发人脸
	_sliceData, _ok := _data.([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	_rs := make([]*NewFaceIssueResponse, 0)
	for _, _v := range _sliceData {
		_vData, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确2")
		}
		_tmp := new(NewFaceIssueResponse)
		if _err := that.ParseResponeMp(_tmp, _vData, false); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	return _rs, nil
}
