// 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{}
)

// SearchPublicStoriesByLocationRequest represents TL type `searchPublicStoriesByLocation#5f2bd988`.
type SearchPublicStoriesByLocationRequest struct {
	// Address of the location
	Address LocationAddress
	// Offset of the first entry to return as received from the previous request; use empty
	// string to get the first chunk of results
	Offset string
	// The maximum number of stories to be returned; up to 100. For optimal performance, the
	// number of returned stories is chosen by TDLib and can be smaller than the specified
	// limit
	Limit int32
}

// SearchPublicStoriesByLocationRequestTypeID is TL type id of SearchPublicStoriesByLocationRequest.
const SearchPublicStoriesByLocationRequestTypeID = 0x5f2bd988

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

func (s *SearchPublicStoriesByLocationRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Address.Zero()) {
		return false
	}
	if !(s.Offset == "") {
		return false
	}
	if !(s.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchPublicStoriesByLocationRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchPublicStoriesByLocation",
		ID:   SearchPublicStoriesByLocationRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Address",
			SchemaName: "address",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchPublicStoriesByLocationRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicStoriesByLocation#5f2bd988 as nil")
	}
	b.PutID(SearchPublicStoriesByLocationRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchPublicStoriesByLocationRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicStoriesByLocation#5f2bd988 as nil")
	}
	if err := s.Address.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchPublicStoriesByLocation#5f2bd988: field address: %w", err)
	}
	b.PutString(s.Offset)
	b.PutInt32(s.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (s *SearchPublicStoriesByLocationRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchPublicStoriesByLocation#5f2bd988 to nil")
	}
	if err := b.ConsumeID(SearchPublicStoriesByLocationRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode searchPublicStoriesByLocation#5f2bd988: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SearchPublicStoriesByLocationRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchPublicStoriesByLocation#5f2bd988 to nil")
	}
	{
		if err := s.Address.Decode(b); err != nil {
			return fmt.Errorf("unable to decode searchPublicStoriesByLocation#5f2bd988: field address: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchPublicStoriesByLocation#5f2bd988: field offset: %w", err)
		}
		s.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchPublicStoriesByLocation#5f2bd988: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchPublicStoriesByLocationRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPublicStoriesByLocation#5f2bd988 as nil")
	}
	b.ObjStart()
	b.PutID("searchPublicStoriesByLocation")
	b.Comma()
	b.FieldStart("address")
	if err := s.Address.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode searchPublicStoriesByLocation#5f2bd988: field address: %w", err)
	}
	b.Comma()
	b.FieldStart("offset")
	b.PutString(s.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(s.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SearchPublicStoriesByLocationRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchPublicStoriesByLocation#5f2bd988 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("searchPublicStoriesByLocation"); err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByLocation#5f2bd988: %w", err)
			}
		case "address":
			if err := s.Address.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByLocation#5f2bd988: field address: %w", err)
			}
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByLocation#5f2bd988: field offset: %w", err)
			}
			s.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode searchPublicStoriesByLocation#5f2bd988: field limit: %w", err)
			}
			s.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetAddress returns value of Address field.
func (s *SearchPublicStoriesByLocationRequest) GetAddress() (value LocationAddress) {
	if s == nil {
		return
	}
	return s.Address
}

// GetOffset returns value of Offset field.
func (s *SearchPublicStoriesByLocationRequest) GetOffset() (value string) {
	if s == nil {
		return
	}
	return s.Offset
}

// GetLimit returns value of Limit field.
func (s *SearchPublicStoriesByLocationRequest) GetLimit() (value int32) {
	if s == nil {
		return
	}
	return s.Limit
}

// SearchPublicStoriesByLocation invokes method searchPublicStoriesByLocation#5f2bd988 returning error if any.
func (c *Client) SearchPublicStoriesByLocation(ctx context.Context, request *SearchPublicStoriesByLocationRequest) (*FoundStories, error) {
	var result FoundStories

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