// Code generated by "requestgen -type GetKLinesRequest"; DO NOT EDIT.

package kucoinapi

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

func (r *GetKLinesRequest) Symbol(symbol string) *GetKLinesRequest {
	r.symbol = symbol
	return r
}

func (r *GetKLinesRequest) Interval(interval string) *GetKLinesRequest {
	r.interval = interval
	return r
}

func (r *GetKLinesRequest) StartAt(startAt time.Time) *GetKLinesRequest {
	r.startAt = &startAt
	return r
}

func (r *GetKLinesRequest) EndAt(endAt time.Time) *GetKLinesRequest {
	r.endAt = &endAt
	return r
}

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

	// assign parameter of symbol
	params["symbol"] = symbol
	// check interval field -> json key type
	interval := r.interval

	switch interval {
	case "1min", "3min", "5min", "15min", "30min", "1hour", "2hour", "4hour", "6hour", "8hour", "12hour", "1day", "1week":
		params["type"] = interval

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

	}

	// assign parameter of interval
	params["type"] = interval
	// check startAt field -> json key startAt
	if r.startAt != nil {
		startAt := *r.startAt

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

		// assign parameter of endAt
		// convert time.Time to seconds time stamp
		params["endAt"] = strconv.FormatInt(endAt.Unix(), 10)
	}

	return params, nil
}

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

	params, err := r.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 (r *GetKLinesRequest) GetParametersJSON() ([]byte, error) {
	params, err := r.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 (r *GetKLinesRequest) GetSlugParameters() (map[string]interface{}, error) {
	var params = map[string]interface{}{}

	return params, nil
}

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

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

	return slugs, nil
}
