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

// ChannelsSearchPostsRequest represents TL type `channels.searchPosts#f2c4f24d`.
// Globally search for posts from public channels »¹ (including those we aren't a
// member of) containing either a specific hashtag, or a full text query.
// Exactly one of query and hashtag must be set.
//
// Links:
//  1. https://core.telegram.org/api/channel
//
// See https://core.telegram.org/method/channels.searchPosts for reference.
type ChannelsSearchPostsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The hashtag to search, without the # character.
	//
	// Use SetHashtag and GetHashtag helpers.
	Hashtag string
	// The full text query: each user has a limited amount of free full text search slots,
	// after which payment is required, see here »¹ for more info on the full flow.
	//
	// Links:
	//  1) https://core.telegram.org/api/search#posts-tab
	//
	// Use SetQuery and GetQuery helpers.
	Query string
	// Initially 0, then set to the next_rate parameter of messages.messagesSlice¹, or if
	// that is absent, the date of the last returned message.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/messages.messagesSlice
	OffsetRate int
	// Offsets for pagination, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	OffsetPeer InputPeerClass
	// Offsets for pagination, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	OffsetID int
	// Maximum number of results to return, see pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Limit int
	// For full text post searches (query), allows payment of the specified amount of Stars
	// for the search, see here »¹ for more info on the full flow.
	//
	// Links:
	//  1) https://core.telegram.org/api/search#posts-tab
	//
	// Use SetAllowPaidStars and GetAllowPaidStars helpers.
	AllowPaidStars int64
}

// ChannelsSearchPostsRequestTypeID is TL type id of ChannelsSearchPostsRequest.
const ChannelsSearchPostsRequestTypeID = 0xf2c4f24d

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

func (s *ChannelsSearchPostsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Hashtag == "") {
		return false
	}
	if !(s.Query == "") {
		return false
	}
	if !(s.OffsetRate == 0) {
		return false
	}
	if !(s.OffsetPeer == nil) {
		return false
	}
	if !(s.OffsetID == 0) {
		return false
	}
	if !(s.Limit == 0) {
		return false
	}
	if !(s.AllowPaidStars == 0) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsSearchPostsRequest from given interface.
func (s *ChannelsSearchPostsRequest) FillFrom(from interface {
	GetHashtag() (value string, ok bool)
	GetQuery() (value string, ok bool)
	GetOffsetRate() (value int)
	GetOffsetPeer() (value InputPeerClass)
	GetOffsetID() (value int)
	GetLimit() (value int)
	GetAllowPaidStars() (value int64, ok bool)
}) {
	if val, ok := from.GetHashtag(); ok {
		s.Hashtag = val
	}

	if val, ok := from.GetQuery(); ok {
		s.Query = val
	}

	s.OffsetRate = from.GetOffsetRate()
	s.OffsetPeer = from.GetOffsetPeer()
	s.OffsetID = from.GetOffsetID()
	s.Limit = from.GetLimit()
	if val, ok := from.GetAllowPaidStars(); ok {
		s.AllowPaidStars = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *ChannelsSearchPostsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.searchPosts",
		ID:   ChannelsSearchPostsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Hashtag",
			SchemaName: "hashtag",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Query",
			SchemaName: "query",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "OffsetRate",
			SchemaName: "offset_rate",
		},
		{
			Name:       "OffsetPeer",
			SchemaName: "offset_peer",
		},
		{
			Name:       "OffsetID",
			SchemaName: "offset_id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "AllowPaidStars",
			SchemaName: "allow_paid_stars",
			Null:       !s.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *ChannelsSearchPostsRequest) SetFlags() {
	if !(s.Hashtag == "") {
		s.Flags.Set(0)
	}
	if !(s.Query == "") {
		s.Flags.Set(1)
	}
	if !(s.AllowPaidStars == 0) {
		s.Flags.Set(2)
	}
}

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

// EncodeBare implements bin.BareEncoder.
func (s *ChannelsSearchPostsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode channels.searchPosts#f2c4f24d as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.searchPosts#f2c4f24d: field flags: %w", err)
	}
	if s.Flags.Has(0) {
		b.PutString(s.Hashtag)
	}
	if s.Flags.Has(1) {
		b.PutString(s.Query)
	}
	b.PutInt(s.OffsetRate)
	if s.OffsetPeer == nil {
		return fmt.Errorf("unable to encode channels.searchPosts#f2c4f24d: field offset_peer is nil")
	}
	if err := s.OffsetPeer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.searchPosts#f2c4f24d: field offset_peer: %w", err)
	}
	b.PutInt(s.OffsetID)
	b.PutInt(s.Limit)
	if s.Flags.Has(2) {
		b.PutLong(s.AllowPaidStars)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *ChannelsSearchPostsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode channels.searchPosts#f2c4f24d to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#f2c4f24d: field flags: %w", err)
		}
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#f2c4f24d: field hashtag: %w", err)
		}
		s.Hashtag = value
	}
	if s.Flags.Has(1) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#f2c4f24d: field query: %w", err)
		}
		s.Query = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#f2c4f24d: field offset_rate: %w", err)
		}
		s.OffsetRate = value
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#f2c4f24d: field offset_peer: %w", err)
		}
		s.OffsetPeer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#f2c4f24d: field offset_id: %w", err)
		}
		s.OffsetID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#f2c4f24d: field limit: %w", err)
		}
		s.Limit = value
	}
	if s.Flags.Has(2) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#f2c4f24d: field allow_paid_stars: %w", err)
		}
		s.AllowPaidStars = value
	}
	return nil
}

// SetHashtag sets value of Hashtag conditional field.
func (s *ChannelsSearchPostsRequest) 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 *ChannelsSearchPostsRequest) GetHashtag() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Hashtag, true
}

// SetQuery sets value of Query conditional field.
func (s *ChannelsSearchPostsRequest) SetQuery(value string) {
	s.Flags.Set(1)
	s.Query = value
}

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

// GetOffsetRate returns value of OffsetRate field.
func (s *ChannelsSearchPostsRequest) GetOffsetRate() (value int) {
	if s == nil {
		return
	}
	return s.OffsetRate
}

// GetOffsetPeer returns value of OffsetPeer field.
func (s *ChannelsSearchPostsRequest) GetOffsetPeer() (value InputPeerClass) {
	if s == nil {
		return
	}
	return s.OffsetPeer
}

// GetOffsetID returns value of OffsetID field.
func (s *ChannelsSearchPostsRequest) GetOffsetID() (value int) {
	if s == nil {
		return
	}
	return s.OffsetID
}

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

// SetAllowPaidStars sets value of AllowPaidStars conditional field.
func (s *ChannelsSearchPostsRequest) SetAllowPaidStars(value int64) {
	s.Flags.Set(2)
	s.AllowPaidStars = value
}

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

// ChannelsSearchPosts invokes method channels.searchPosts#f2c4f24d returning error if any.
// Globally search for posts from public channels »¹ (including those we aren't a
// member of) containing either a specific hashtag, or a full text query.
// Exactly one of query and hashtag must be set.
//
// Links:
//  1. https://core.telegram.org/api/channel
//
// Possible errors:
//
//	420 FROZEN_METHOD_INVALID: The current account is frozen, and thus cannot execute the specified action.
//
// See https://core.telegram.org/method/channels.searchPosts for reference.
func (c *Client) ChannelsSearchPosts(ctx context.Context, request *ChannelsSearchPostsRequest) (MessagesMessagesClass, error) {
	var result MessagesMessagesBox

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