// Code generated by "requestgen -method GET -url v2/rewards/:path_type -type GetRewardsOfTypeRequest -responseType []Reward"; DO NOT EDIT.

package maxapi

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

func (g *GetRewardsOfTypeRequest) From(from int64) *GetRewardsOfTypeRequest {
	g.from = &from
	return g
}

func (g *GetRewardsOfTypeRequest) To(to int64) *GetRewardsOfTypeRequest {
	g.to = &to
	return g
}

func (g *GetRewardsOfTypeRequest) Page(page int64) *GetRewardsOfTypeRequest {
	g.page = &page
	return g
}

func (g *GetRewardsOfTypeRequest) Limit(limit int64) *GetRewardsOfTypeRequest {
	g.limit = &limit
	return g
}

func (g *GetRewardsOfTypeRequest) Offset(offset int64) *GetRewardsOfTypeRequest {
	g.offset = &offset
	return g
}

func (g *GetRewardsOfTypeRequest) PathType(pathType RewardType) *GetRewardsOfTypeRequest {
	g.pathType = &pathType
	return g
}

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

	query := url.Values{}
	for k, v := range params {
		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 (g *GetRewardsOfTypeRequest) GetParameters() (map[string]interface{}, error) {
	var params = map[string]interface{}{}
	// check from field -> json key from
	if g.from != nil {
		from := *g.from

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

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

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

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

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

	return params, nil
}

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

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

	for k, v := range params {
		if g.isVarSlice(v) {
			g.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 (g *GetRewardsOfTypeRequest) GetParametersJSON() ([]byte, error) {
	params, err := g.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 (g *GetRewardsOfTypeRequest) GetSlugParameters() (map[string]interface{}, error) {
	var params = map[string]interface{}{}
	// check pathType field -> json key path_type
	if g.pathType != nil {
		pathType := *g.pathType

		// assign parameter of pathType
		params["path_type"] = pathType

	}

	return params, nil
}

func (g *GetRewardsOfTypeRequest) 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 (g *GetRewardsOfTypeRequest) 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 (g *GetRewardsOfTypeRequest) isVarSlice(v interface{}) bool {
	rt := reflect.TypeOf(v)
	switch rt.Kind() {
	case reflect.Slice:
		return true
	}
	return false
}

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

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

	return slugs, nil
}

func (g *GetRewardsOfTypeRequest) Do(ctx context.Context) ([]Reward, error) {

	// empty params for GET operation
	var params interface{}
	query, err := g.GetParametersQuery()
	if err != nil {
		return nil, err
	}

	apiURL := "v2/rewards/:path_type"
	slugs, err := g.GetSlugsMap()
	if err != nil {
		return nil, err
	}

	apiURL = g.applySlugsToUrl(apiURL, slugs)

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

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

	var apiResponse []Reward
	if err := response.DecodeJSON(&apiResponse); err != nil {
		return nil, err
	}
	return apiResponse, nil
}
