// Code generated by gotdgen, DO NOT EDIT.

package tdapi

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// GetCurrentWeatherRequest represents TL type `getCurrentWeather#8ada9bc9`.
type GetCurrentWeatherRequest struct {
	// The location
	Location Location
}

// GetCurrentWeatherRequestTypeID is TL type id of GetCurrentWeatherRequest.
const GetCurrentWeatherRequestTypeID = 0x8ada9bc9

// Ensuring interfaces in compile-time for GetCurrentWeatherRequest.
var (
	_ bin.Encoder     = &GetCurrentWeatherRequest{}
	_ bin.Decoder     = &GetCurrentWeatherRequest{}
	_ bin.BareEncoder = &GetCurrentWeatherRequest{}
	_ bin.BareDecoder = &GetCurrentWeatherRequest{}
)

func (g *GetCurrentWeatherRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Location.Zero()) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (g *GetCurrentWeatherRequest) String() string {
	if g == nil {
		return "GetCurrentWeatherRequest(nil)"
	}
	type Alias GetCurrentWeatherRequest
	return fmt.Sprintf("GetCurrentWeatherRequest%+v", Alias(*g))
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*GetCurrentWeatherRequest) TypeID() uint32 {
	return GetCurrentWeatherRequestTypeID
}

// TypeName returns name of type in TL schema.
func (*GetCurrentWeatherRequest) TypeName() string {
	return "getCurrentWeather"
}

// TypeInfo returns info about TL type.
func (g *GetCurrentWeatherRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getCurrentWeather",
		ID:   GetCurrentWeatherRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Location",
			SchemaName: "location",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetCurrentWeatherRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getCurrentWeather#8ada9bc9 as nil")
	}
	b.PutID(GetCurrentWeatherRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetCurrentWeatherRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getCurrentWeather#8ada9bc9 as nil")
	}
	if err := g.Location.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getCurrentWeather#8ada9bc9: field location: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GetCurrentWeatherRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getCurrentWeather#8ada9bc9 to nil")
	}
	if err := b.ConsumeID(GetCurrentWeatherRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getCurrentWeather#8ada9bc9: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetCurrentWeatherRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getCurrentWeather#8ada9bc9 to nil")
	}
	{
		if err := g.Location.Decode(b); err != nil {
			return fmt.Errorf("unable to decode getCurrentWeather#8ada9bc9: field location: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetCurrentWeatherRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getCurrentWeather#8ada9bc9 as nil")
	}
	b.ObjStart()
	b.PutID("getCurrentWeather")
	b.Comma()
	b.FieldStart("location")
	if err := g.Location.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getCurrentWeather#8ada9bc9: field location: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetCurrentWeatherRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getCurrentWeather#8ada9bc9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getCurrentWeather"); err != nil {
				return fmt.Errorf("unable to decode getCurrentWeather#8ada9bc9: %w", err)
			}
		case "location":
			if err := g.Location.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode getCurrentWeather#8ada9bc9: field location: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetLocation returns value of Location field.
func (g *GetCurrentWeatherRequest) GetLocation() (value Location) {
	if g == nil {
		return
	}
	return g.Location
}

// GetCurrentWeather invokes method getCurrentWeather#8ada9bc9 returning error if any.
func (c *Client) GetCurrentWeather(ctx context.Context, location Location) (*CurrentWeather, error) {
	var result CurrentWeather

	request := &GetCurrentWeatherRequest{
		Location: location,
	}
	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
