// 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#d19f987b`.
// Globally search for posts from public channels »¹ (including those we aren't a
// member of) containing a specific hashtag.
//
// Links:
//  1. https://core.telegram.org/api/channel
//
// See https://core.telegram.org/method/channels.searchPosts for reference.
type ChannelsSearchPostsRequest struct {
	// The hashtag to search, without the # character.
	Hashtag string
	// Initially 0, then set to the next_rate parameter of messages.messagesSlice¹
	//
	// 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
}

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

// 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.Hashtag == "") {
		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
	}

	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)
	GetOffsetRate() (value int)
	GetOffsetPeer() (value InputPeerClass)
	GetOffsetID() (value int)
	GetLimit() (value int)
}) {
	s.Hashtag = from.GetHashtag()
	s.OffsetRate = from.GetOffsetRate()
	s.OffsetPeer = from.GetOffsetPeer()
	s.OffsetID = from.GetOffsetID()
	s.Limit = from.GetLimit()
}

// 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",
		},
		{
			Name:       "OffsetRate",
			SchemaName: "offset_rate",
		},
		{
			Name:       "OffsetPeer",
			SchemaName: "offset_peer",
		},
		{
			Name:       "OffsetID",
			SchemaName: "offset_id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *ChannelsSearchPostsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode channels.searchPosts#d19f987b 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#d19f987b as nil")
	}
	b.PutString(s.Hashtag)
	b.PutInt(s.OffsetRate)
	if s.OffsetPeer == nil {
		return fmt.Errorf("unable to encode channels.searchPosts#d19f987b: field offset_peer is nil")
	}
	if err := s.OffsetPeer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.searchPosts#d19f987b: field offset_peer: %w", err)
	}
	b.PutInt(s.OffsetID)
	b.PutInt(s.Limit)
	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#d19f987b to nil")
	}
	if err := b.ConsumeID(ChannelsSearchPostsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.searchPosts#d19f987b: %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#d19f987b to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#d19f987b: field hashtag: %w", err)
		}
		s.Hashtag = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#d19f987b: field offset_rate: %w", err)
		}
		s.OffsetRate = value
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#d19f987b: field offset_peer: %w", err)
		}
		s.OffsetPeer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#d19f987b: field offset_id: %w", err)
		}
		s.OffsetID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode channels.searchPosts#d19f987b: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// GetHashtag returns value of Hashtag field.
func (s *ChannelsSearchPostsRequest) GetHashtag() (value string) {
	if s == nil {
		return
	}
	return s.Hashtag
}

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

// ChannelsSearchPosts invokes method channels.searchPosts#d19f987b returning error if any.
// Globally search for posts from public channels »¹ (including those we aren't a
// member of) containing a specific hashtag.
//
// Links:
//  1. https://core.telegram.org/api/channel
//
// 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
}
