// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// StoriesSearchPostsRequest represents TL type `stories.searchPosts#d1810907`.
// Globally search for stories¹ using a hashtag or a location media area², see here
// »³ for more info on the full flow.
// Either hashtag or area must be set when invoking the method.
//
// Links:
//  1. https://core.telegram.org/api/stories
//  2. https://core.telegram.org/api/stories#location-tags
//  3. https://core.telegram.org/api/stories#searching-stories
//
// See https://core.telegram.org/method/stories.searchPosts for reference.
type StoriesSearchPostsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Hashtag (without the #)
	//
	// Use SetHashtag and GetHashtag helpers.
	Hashtag string
	// A mediaAreaGeoPoint¹ or a mediaAreaVenue².  Note mediaAreaGeoPoint³ areas may be
	// searched only if they have an associated address.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/mediaAreaGeoPoint
	//  2) https://core.telegram.org/constructor/mediaAreaVenue
	//  3) https://core.telegram.org/constructor/mediaAreaGeoPoint
	//
	// Use SetArea and GetArea helpers.
	Area MediaAreaClass
	// If set, returns only stories posted by this peer.
	//
	// Use SetPeer and GetPeer helpers.
	Peer InputPeerClass
	// Offset for pagination¹: initially an empty string, then the next_offset from the
	// previously returned stories.foundStories².
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	//  2) https://core.telegram.org/constructor/stories.foundStories
	Offset string
	// Maximum number of results to return, see pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Limit int
}

// StoriesSearchPostsRequestTypeID is TL type id of StoriesSearchPostsRequest.
const StoriesSearchPostsRequestTypeID = 0xd1810907

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

func (s *StoriesSearchPostsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Hashtag == "") {
		return false
	}
	if !(s.Area == nil) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.Offset == "") {
		return false
	}
	if !(s.Limit == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesSearchPostsRequest from given interface.
func (s *StoriesSearchPostsRequest) FillFrom(from interface {
	GetHashtag() (value string, ok bool)
	GetArea() (value MediaAreaClass, ok bool)
	GetPeer() (value InputPeerClass, ok bool)
	GetOffset() (value string)
	GetLimit() (value int)
}) {
	if val, ok := from.GetHashtag(); ok {
		s.Hashtag = val
	}

	if val, ok := from.GetArea(); ok {
		s.Area = val
	}

	if val, ok := from.GetPeer(); ok {
		s.Peer = val
	}

	s.Offset = from.GetOffset()
	s.Limit = from.GetLimit()
}

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

// TypeName returns name of type in TL schema.
func (*StoriesSearchPostsRequest) TypeName() string {
	return "stories.searchPosts"
}

// TypeInfo returns info about TL type.
func (s *StoriesSearchPostsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.searchPosts",
		ID:   StoriesSearchPostsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hashtag",
			SchemaName: "hashtag",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Area",
			SchemaName: "area",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
			Null:       !s.Flags.Has(2),
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StoriesSearchPostsRequest) SetFlags() {
	if !(s.Hashtag == "") {
		s.Flags.Set(0)
	}
	if !(s.Area == nil) {
		s.Flags.Set(1)
	}
	if !(s.Peer == nil) {
		s.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (s *StoriesSearchPostsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stories.searchPosts#d1810907 as nil")
	}
	b.PutID(StoriesSearchPostsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoriesSearchPostsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stories.searchPosts#d1810907 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.searchPosts#d1810907: field flags: %w", err)
	}
	if s.Flags.Has(0) {
		b.PutString(s.Hashtag)
	}
	if s.Flags.Has(1) {
		if s.Area == nil {
			return fmt.Errorf("unable to encode stories.searchPosts#d1810907: field area is nil")
		}
		if err := s.Area.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.searchPosts#d1810907: field area: %w", err)
		}
	}
	if s.Flags.Has(2) {
		if s.Peer == nil {
			return fmt.Errorf("unable to encode stories.searchPosts#d1810907: field peer is nil")
		}
		if err := s.Peer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.searchPosts#d1810907: field peer: %w", err)
		}
	}
	b.PutString(s.Offset)
	b.PutInt(s.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (s *StoriesSearchPostsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stories.searchPosts#d1810907 to nil")
	}
	if err := b.ConsumeID(StoriesSearchPostsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.searchPosts#d1810907: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StoriesSearchPostsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stories.searchPosts#d1810907 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stories.searchPosts#d1810907: field flags: %w", err)
		}
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stories.searchPosts#d1810907: field hashtag: %w", err)
		}
		s.Hashtag = value
	}
	if s.Flags.Has(1) {
		value, err := DecodeMediaArea(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.searchPosts#d1810907: field area: %w", err)
		}
		s.Area = value
	}
	if s.Flags.Has(2) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.searchPosts#d1810907: field peer: %w", err)
		}
		s.Peer = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stories.searchPosts#d1810907: field offset: %w", err)
		}
		s.Offset = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stories.searchPosts#d1810907: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// SetHashtag sets value of Hashtag conditional field.
func (s *StoriesSearchPostsRequest) SetHashtag(value string) {
	s.Flags.Set(0)
	s.Hashtag = value
}

// GetHashtag returns value of Hashtag conditional field and
// boolean which is true if field was set.
func (s *StoriesSearchPostsRequest) GetHashtag() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Hashtag, true
}

// SetArea sets value of Area conditional field.
func (s *StoriesSearchPostsRequest) SetArea(value MediaAreaClass) {
	s.Flags.Set(1)
	s.Area = value
}

// GetArea returns value of Area conditional field and
// boolean which is true if field was set.
func (s *StoriesSearchPostsRequest) GetArea() (value MediaAreaClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.Area, true
}

// SetPeer sets value of Peer conditional field.
func (s *StoriesSearchPostsRequest) SetPeer(value InputPeerClass) {
	s.Flags.Set(2)
	s.Peer = value
}

// GetPeer returns value of Peer conditional field and
// boolean which is true if field was set.
func (s *StoriesSearchPostsRequest) GetPeer() (value InputPeerClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(2) {
		return value, false
	}
	return s.Peer, true
}

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

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

// StoriesSearchPosts invokes method stories.searchPosts#d1810907 returning error if any.
// Globally search for stories¹ using a hashtag or a location media area², see here
// »³ for more info on the full flow.
// Either hashtag or area must be set when invoking the method.
//
// Links:
//  1. https://core.telegram.org/api/stories
//  2. https://core.telegram.org/api/stories#location-tags
//  3. https://core.telegram.org/api/stories#searching-stories
//
// Possible errors:
//
//	400 HASHTAG_INVALID: The specified hashtag is invalid.
//
// See https://core.telegram.org/method/stories.searchPosts for reference.
func (c *Client) StoriesSearchPosts(ctx context.Context, request *StoriesSearchPostsRequest) (*StoriesFoundStories, error) {
	var result StoriesFoundStories

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