package pass

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

//NewUidGet ...
func (that *PassClient) NewUid() *UidRequest {
	return &UidRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/uid/id/get",
		},
	}
}

type UidReturn struct {
	Uid []int64
}

//UidRequest 唯一id生成器
type UidRequest struct {
	Request
}

func (that *UidRequest) SetName(name string, startID ...int64) *UidRequest {
	that.initParams()
	that.params.Set("name", name)
	if len(startID) > 0 {
		that.params.Set("start_id", utils.ToString(startID[0]))
	}
	return that
}

func (that *UidRequest) SetPreDomain(value string) *UidRequest {
	that.initParams()
	that.params.Set("pre_domain", value) //例如：20211230000000 + ....
	return that
}

func (that *UidRequest) SetCount(count int64) *UidRequest {
	that.apiAddress = "/uid/id/getSlice"
	that.initParams()
	that.params.Set("count", utils.ToString(count))
	return that
}

//Params ...
func (that *UidRequest) 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 *UidRequest) Do() (*UidReturn, error) {
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	_intSlice := []int64{}
	switch _v := _data.(type) {
	case float64:
		_intSlice = append(_intSlice, utils.ToInt64(_v))
	case string:
		_intSlice = append(_intSlice, utils.ToInt64(_v))
	case int64:
		_intSlice = append(_intSlice, utils.ToInt64(_v))
	case int:
		_intSlice = append(_intSlice, utils.ToInt64(_v))
	case []int64:
		_intSlice = append(_intSlice, _v...)
	case []interface{}:
		for _, _val := range _v {
			_intSlice = append(_intSlice, utils.ToInt64(_val))
		}
	default:
		return nil, errors.New("uid return data type is:" + utils.TypeOf(_data))
	}
	return &UidReturn{_intSlice}, nil
}
