// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/v1/hist-orders -type ListHistoryOrdersRequest -responseDataType .HistoryOrderListPage"; DO NOT EDIT.

package kucoinapi

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

func (l *ListHistoryOrdersRequest) Symbol(symbol string) *ListHistoryOrdersRequest {
	l.symbol = &symbol
	return l
}

func (l *ListHistoryOrdersRequest) StartAt(startAt time.Time) *ListHistoryOrdersRequest {
	l.startAt = &startAt
	return l
}

func (l *ListHistoryOrdersRequest) EndAt(endAt time.Time) *ListHistoryOrdersRequest {
	l.endAt = &endAt
	return l
}

// GetQueryParameters builds and checks the query parameters and returns url.Values
func (l *ListHistoryOrdersRequest) 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 (l *ListHistoryOrdersRequest) GetParameters() (map[string]interface{}, error) {
	var params = map[string]interface{}{}
	// check symbol field -> json key symbol
	if l.symbol != nil {
		symbol := *l.symbol

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

		// assign parameter of startAt
		// convert time.Time to milliseconds time stamp
		params["startAt"] = strconv.FormatInt(startAt.UnixNano()/int64(time.Millisecond), 10)
	} else {
	}
	// check endAt field -> json key endAt
	if l.endAt != nil {
		endAt := *l.endAt

		// assign parameter of endAt
		// convert time.Time to milliseconds time stamp
		params["endAt"] = strconv.FormatInt(endAt.UnixNano()/int64(time.Millisecond), 10)
	} else {
	}

	return params, nil
}

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

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

	for k, v := range params {
		query.Add(k, fmt.Sprintf("%v", v))
	}

	return query, nil
}

// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (l *ListHistoryOrdersRequest) GetParametersJSON() ([]byte, error) {
	params, err := l.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 (l *ListHistoryOrdersRequest) GetSlugParameters() (map[string]interface{}, error) {
	var params = map[string]interface{}{}

	return params, nil
}

func (l *ListHistoryOrdersRequest) 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 (l *ListHistoryOrdersRequest) GetSlugsMap() (map[string]string, error) {
	slugs := map[string]string{}
	params, err := l.GetSlugParameters()
	if err != nil {
		return slugs, nil
	}

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

	return slugs, nil
}

func (l *ListHistoryOrdersRequest) Do(ctx context.Context) (*HistoryOrderListPage, error) {

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

	apiURL := "/api/v1/hist-orders"

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

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

	var apiResponse APIResponse
	if err := response.DecodeJSON(&apiResponse); err != nil {
		return nil, err
	}
	var data HistoryOrderListPage
	if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
		return nil, err
	}
	return &data, nil
}
