// Code generated by "requestgen -type SubmitFundingOfferRequest -method POST -url /v2/auth/w/funding/offer/submit -responseType .SubmitFundingOfferResponse"; DO NOT EDIT.

package bfxapi

import (
	"context"
	"encoding/json"
	"fmt"
	"net/url"
	"reflect"
	"regexp"
)

/*
 * Symbol sets
 */
func (s *SubmitFundingOfferRequest) Symbol(symbol string) *SubmitFundingOfferRequest {
	s.symbol = symbol
	return s
}

/*
 * Amount sets
 */
func (s *SubmitFundingOfferRequest) Amount(amount string) *SubmitFundingOfferRequest {
	s.amount = amount
	return s
}

/*
 * Rate sets
 */
func (s *SubmitFundingOfferRequest) Rate(rate string) *SubmitFundingOfferRequest {
	s.rate = rate
	return s
}

/*
 * Period sets
 */
func (s *SubmitFundingOfferRequest) Period(period int) *SubmitFundingOfferRequest {
	s.period = period
	return s
}

/*
 * OfferType sets
 */
func (s *SubmitFundingOfferRequest) OfferType(offerType FundingOfferType) *SubmitFundingOfferRequest {
	s.offerType = offerType
	return s
}

/*
 * Flags sets
 */
func (s *SubmitFundingOfferRequest) Flags(flags int64) *SubmitFundingOfferRequest {
	s.flags = &flags
	return s
}

/*
 * AutoRenew sets
 */
func (s *SubmitFundingOfferRequest) AutoRenew(autoRenew bool) *SubmitFundingOfferRequest {
	s.autoRenew = &autoRenew
	return s
}

/*
 * Hidden sets
 */
func (s *SubmitFundingOfferRequest) Hidden(hidden bool) *SubmitFundingOfferRequest {
	s.hidden = &hidden
	return s
}

/*
 * Notify sets
 */
func (s *SubmitFundingOfferRequest) Notify(notify bool) *SubmitFundingOfferRequest {
	s.notify = &notify
	return s
}

// GetQueryParameters builds and checks the query parameters and returns url.Values
func (s *SubmitFundingOfferRequest) GetQueryParameters() (url.Values, error) {
	var params = map[string]interface{}{}

	query := url.Values{}
	for _k, _v := range params {
		if s.isVarSlice(_v) {
			s.iterateSlice(_v, func(it interface{}) {
				query.Add(_k+"[]", fmt.Sprintf("%v", it))
			})
		} else {
			query.Add(_k, fmt.Sprintf("%v", _v))
		}
	}

	return query, nil
}

// GetParameters builds and checks the parameters and return the result in a map object
func (s *SubmitFundingOfferRequest) GetParameters() (map[string]interface{}, error) {
	var params = map[string]interface{}{}
	// check symbol field -> json key symbol
	symbol := s.symbol

	// TEMPLATE check-required
	if len(symbol) == 0 {
		return nil, fmt.Errorf("symbol is required, empty string given")
	}
	// END TEMPLATE check-required

	// assign parameter of symbol
	params["symbol"] = symbol
	// check amount field -> json key amount
	amount := s.amount

	// TEMPLATE check-required
	if len(amount) == 0 {
		return nil, fmt.Errorf("amount is required, empty string given")
	}
	// END TEMPLATE check-required

	// assign parameter of amount
	params["amount"] = amount
	// check rate field -> json key rate
	rate := s.rate

	// TEMPLATE check-required
	if len(rate) == 0 {
		return nil, fmt.Errorf("rate is required, empty string given")
	}
	// END TEMPLATE check-required

	// assign parameter of rate
	params["rate"] = rate
	// check period field -> json key period
	period := s.period

	// TEMPLATE check-required

	if period == 0 {
		return nil, fmt.Errorf("period is required, 0 given")
	}
	// END TEMPLATE check-required

	// assign parameter of period
	params["period"] = period
	// check offerType field -> json key type
	offerType := s.offerType

	// TEMPLATE check-required
	if len(offerType) == 0 {
		return nil, fmt.Errorf("type is required, empty string given")
	}
	// END TEMPLATE check-required

	// TEMPLATE check-valid-values
	switch offerType {
	case FundingOfferTypeLimit, FundingOfferTypeFRRDeltaFix, FundingOfferTypeFRRDeltaVar:
		params["type"] = offerType

	default:
		return nil, fmt.Errorf("type value %v is invalid", offerType)

	}
	// END TEMPLATE check-valid-values

	// assign parameter of offerType
	params["type"] = offerType
	// check flags field -> json key flags
	if s.flags != nil {
		flags := *s.flags

		// TEMPLATE check-required

		if flags == 0 {
		}
		// END TEMPLATE check-required

		// assign parameter of flags
		params["flags"] = flags
	} else {
	}
	// check autoRenew field -> json key renew
	if s.autoRenew != nil {
		autoRenew := *s.autoRenew

		// TEMPLATE check-required
		// END TEMPLATE check-required

		// assign parameter of autoRenew
		params["renew"] = autoRenew
	} else {
	}
	// check hidden field -> json key hidden
	if s.hidden != nil {
		hidden := *s.hidden

		// TEMPLATE check-required
		// END TEMPLATE check-required

		// assign parameter of hidden
		params["hidden"] = hidden
	} else {
	}
	// check notify field -> json key notify
	if s.notify != nil {
		notify := *s.notify

		// TEMPLATE check-required
		// END TEMPLATE check-required

		// assign parameter of notify
		params["notify"] = notify
	} else {
	}

	return params, nil
}

// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (s *SubmitFundingOfferRequest) GetParametersQuery() (url.Values, error) {
	query := url.Values{}

	params, err := s.GetParameters()
	if err != nil {
		return query, err
	}

	for _k, _v := range params {
		if s.isVarSlice(_v) {
			s.iterateSlice(_v, func(it interface{}) {
				query.Add(_k+"[]", fmt.Sprintf("%v", it))
			})
		} else {
			query.Add(_k, fmt.Sprintf("%v", _v))
		}
	}

	return query, nil
}

// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (s *SubmitFundingOfferRequest) GetParametersJSON() ([]byte, error) {
	params, err := s.GetParameters()
	if err != nil {
		return nil, err
	}

	return json.Marshal(params)
}

// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (s *SubmitFundingOfferRequest) GetSlugParameters() (map[string]interface{}, error) {
	var params = map[string]interface{}{}

	return params, nil
}

func (s *SubmitFundingOfferRequest) applySlugsToUrl(url string, slugs map[string]string) string {
	for _k, _v := range slugs {
		needleRE := regexp.MustCompile(":" + _k + "\\b")
		url = needleRE.ReplaceAllString(url, _v)
	}

	return url
}

func (s *SubmitFundingOfferRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
	sliceValue := reflect.ValueOf(slice)
	for _i := 0; _i < sliceValue.Len(); _i++ {
		it := sliceValue.Index(_i).Interface()
		_f(it)
	}
}

func (s *SubmitFundingOfferRequest) isVarSlice(_v interface{}) bool {
	rt := reflect.TypeOf(_v)
	switch rt.Kind() {
	case reflect.Slice:
		return true
	}
	return false
}

func (s *SubmitFundingOfferRequest) GetSlugsMap() (map[string]string, error) {
	slugs := map[string]string{}
	params, err := s.GetSlugParameters()
	if err != nil {
		return slugs, nil
	}

	for _k, _v := range params {
		slugs[_k] = fmt.Sprintf("%v", _v)
	}

	return slugs, nil
}

// GetPath returns the request path of the API
func (s *SubmitFundingOfferRequest) GetPath() string {
	return "/v2/auth/w/funding/offer/submit"
}

// Do generates the request object and send the request object to the API endpoint
func (s *SubmitFundingOfferRequest) Do(ctx context.Context) (*SubmitFundingOfferResponse, error) {

	params, err := s.GetParameters()
	if err != nil {
		return nil, err
	}
	query := url.Values{}

	var apiURL string

	apiURL = s.GetPath()

	req, err := s.client.NewAuthenticatedRequest(ctx, "POST", apiURL, query, params)
	if err != nil {
		return nil, err
	}

	response, err := s.client.SendRequest(req)
	if err != nil {
		return nil, err
	}

	var apiResponse SubmitFundingOfferResponse

	type responseUnmarshaler interface {
		Unmarshal(data []byte) error
	}

	if unmarshaler, ok := interface{}(&apiResponse).(responseUnmarshaler); ok {
		if err := unmarshaler.Unmarshal(response.Body); err != nil {
			return nil, err
		}
	} else {
		// The line below checks the content type, however, some API server might not send the correct content type header,
		// Hence, this is commented for backward compatibility
		// response.IsJSON()
		if err := response.DecodeJSON(&apiResponse); err != nil {
			return nil, err
		}
	}

	type responseValidator interface {
		Validate() error
	}

	if validator, ok := interface{}(&apiResponse).(responseValidator); ok {
		if err := validator.Validate(); err != nil {
			return nil, err
		}
	}
	return &apiResponse, nil
}
