/*
Copyright (C) 2025 filifa

This file is part of mlbstats.

mlbstats is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

mlbstats is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
mlbstats. If not, see <https://www.gnu.org/licenses/>.
*/

/*
 * Stats API Documentation
 *
 * Official API for Major League Baseball.
 *
 * API version: 2.0.0
 * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
 */
package api

import (
	"context"
	"fmt"
	"github.com/antihax/optional"
	"io/ioutil"
	"net/http"
	"net/url"
	"scm.dairydemon.net/filifa/mlbstats/api/models"
	"strings"
)

// Linger please
var (
	_ context.Context
)

type HighLowApiService service

/*
HighLowApiService View high/low stats by player or team
 * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 * @param highLowType Type of high/low stats (&#x27;player&#x27;, &#x27;team&#x27;, &#x27;game&#x27;)
 * @param optional nil or *HighLowApiHighLowOpts - Optional Parameters:
     * @param "StatGroup" (optional.Interface of []StatGroup) -  Comma delimited list of  categories of statistic to return. Available types in /api/v1/statGroups
     * @param "SortStat" (optional.Interface of []HighLowStatEnum) -  Comma delimited list of baseball stats to sort splits by.
     * @param "Season" (optional.Interface of []string) -  Comma delimited list of Seasons of play
     * @param "GameType" (optional.Interface of []models.GameTypeEnum) -  Comma delimited list of type of Game. Available types in /api/v1/gameTypes
     * @param "TeamId" (optional.Int32) -  Unique Team Identifier. Format: 141, 147, etc
     * @param "LeagueId" (optional.Int32) -  Unique League Identifier
     * @param "SportId" (optional.Int32) -  Top level organization of a sport
     * @param "Offset" (optional.Int32) -  The pointer to start for a return set; used for pagination
     * @param "Limit" (optional.Int32) -  Number of results to return
     * @param "Fields" (optional.Interface of []string) -  Comma delimited list of specific fields to be returned. Format: topLevelNode, childNode, attribute
@return models.HighLowWrapperRestObject
*/

type HighLowApiHighLowOpts struct {
	StatGroup optional.Interface
	SortStat  optional.Interface
	Season    optional.Interface
	GameType  optional.Interface
	TeamId    optional.Int32
	LeagueId  optional.Int32
	SportId   optional.Int32
	Offset    optional.Int32
	Limit     optional.Int32
	Fields    optional.Interface
}

func (a *HighLowApiService) HighLow(ctx context.Context, highLowType models.HighLowTypeEnum, localVarOptionals *HighLowApiHighLowOpts) (models.HighLowWrapperRestObject, *http.Response, error) {
	var (
		localVarHttpMethod  = strings.ToUpper("Get")
		localVarPostBody    interface{}
		localVarFileName    string
		localVarFileBytes   []byte
		localVarReturnValue models.HighLowWrapperRestObject
	)

	// create path and map variables
	localVarPath := a.client.cfg.BasePath + "/api/v1/highLow/{highLowType}"
	localVarPath = strings.Replace(localVarPath, "{"+"highLowType"+"}", fmt.Sprintf("%v", highLowType), -1)

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}

	if localVarOptionals != nil && localVarOptionals.StatGroup.IsSet() {
		localVarQueryParams.Add("statGroup", parameterToString(localVarOptionals.StatGroup.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.SortStat.IsSet() {
		localVarQueryParams.Add("sortStat", parameterToString(localVarOptionals.SortStat.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.Season.IsSet() {
		localVarQueryParams.Add("season", parameterToString(localVarOptionals.Season.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.GameType.IsSet() {
		localVarQueryParams.Add("gameType", parameterToString(localVarOptionals.GameType.Value(), "multi"))
	}
	if localVarOptionals != nil && localVarOptionals.TeamId.IsSet() {
		localVarQueryParams.Add("teamId", parameterToString(localVarOptionals.TeamId.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.LeagueId.IsSet() {
		localVarQueryParams.Add("leagueId", parameterToString(localVarOptionals.LeagueId.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.SportId.IsSet() {
		localVarQueryParams.Add("sportId", parameterToString(localVarOptionals.SportId.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.Offset.IsSet() {
		localVarQueryParams.Add("offset", parameterToString(localVarOptionals.Offset.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.Limit.IsSet() {
		localVarQueryParams.Add("limit", parameterToString(localVarOptionals.Limit.Value(), ""))
	}
	if localVarOptionals != nil && localVarOptionals.Fields.IsSet() {
		localVarQueryParams.Add("fields", parameterToString(localVarOptionals.Fields.Value(), "multi"))
	}
	// to determine the Content-Type header
	localVarHttpContentTypes := []string{}

	// set Content-Type header
	localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
	if localVarHttpContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHttpContentType
	}

	// to determine the Accept header
	localVarHttpHeaderAccepts := []string{"*/*"}

	// set Accept header
	localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
	if localVarHttpHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
	}
	r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHttpResponse, err := a.client.callAPI(r)
	if err != nil || localVarHttpResponse == nil {
		return localVarReturnValue, localVarHttpResponse, err
	}

	localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
	localVarHttpResponse.Body.Close()
	if err != nil {
		return localVarReturnValue, localVarHttpResponse, err
	}

	if localVarHttpResponse.StatusCode < 300 {
		// If we succeed, return the data, otherwise pass on to decode error.
		err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
		if err == nil {
			return localVarReturnValue, localVarHttpResponse, err
		}
	}

	if localVarHttpResponse.StatusCode >= 300 {
		newErr := GenericSwaggerError{
			body:  localVarBody,
			error: localVarHttpResponse.Status,
		}
		if localVarHttpResponse.StatusCode == 200 {
			var v models.HighLowWrapperRestObject
			err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarReturnValue, localVarHttpResponse, newErr
			}
			newErr.model = v
			return localVarReturnValue, localVarHttpResponse, newErr
		}
		return localVarReturnValue, localVarHttpResponse, newErr
	}

	return localVarReturnValue, localVarHttpResponse, nil
}

/*
HighLowApiService View high/low stat types
  - @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().

@return []models.BaseballStatsTypeRestObject
*/
func (a *HighLowApiService) HighLowStats(ctx context.Context) ([]models.BaseballStatsTypeRestObject, *http.Response, error) {
	var (
		localVarHttpMethod  = strings.ToUpper("Get")
		localVarPostBody    interface{}
		localVarFileName    string
		localVarFileBytes   []byte
		localVarReturnValue []models.BaseballStatsTypeRestObject
	)

	// create path and map variables
	localVarPath := a.client.cfg.BasePath + "/api/v1/highLow/types"

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}

	// to determine the Content-Type header
	localVarHttpContentTypes := []string{}

	// set Content-Type header
	localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
	if localVarHttpContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHttpContentType
	}

	// to determine the Accept header
	localVarHttpHeaderAccepts := []string{"*/*"}

	// set Accept header
	localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts)
	if localVarHttpHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHttpHeaderAccept
	}
	r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHttpResponse, err := a.client.callAPI(r)
	if err != nil || localVarHttpResponse == nil {
		return localVarReturnValue, localVarHttpResponse, err
	}

	localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body)
	localVarHttpResponse.Body.Close()
	if err != nil {
		return localVarReturnValue, localVarHttpResponse, err
	}

	if localVarHttpResponse.StatusCode < 300 {
		// If we succeed, return the data, otherwise pass on to decode error.
		err = a.client.decode(&localVarReturnValue, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
		if err == nil {
			return localVarReturnValue, localVarHttpResponse, err
		}
	}

	if localVarHttpResponse.StatusCode >= 300 {
		newErr := GenericSwaggerError{
			body:  localVarBody,
			error: localVarHttpResponse.Status,
		}
		if localVarHttpResponse.StatusCode == 200 {
			var v []models.BaseballStatsTypeRestObject
			err = a.client.decode(&v, localVarBody, localVarHttpResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarReturnValue, localVarHttpResponse, newErr
			}
			newErr.model = v
			return localVarReturnValue, localVarHttpResponse, newErr
		}
		return localVarReturnValue, localVarHttpResponse, newErr
	}

	return localVarReturnValue, localVarHttpResponse, nil
}
