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

// MessagesGetSponsoredMessagesRequest represents TL type `messages.getSponsoredMessages#3d6ce850`.
// Get a list of sponsored messages for a peer, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/sponsored-messages
//
// See https://core.telegram.org/method/messages.getSponsoredMessages for reference.
type MessagesGetSponsoredMessagesRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The currently open channel/bot.
	Peer InputPeerClass
	// Must be set when fetching sponsored messages to show on channel videos »¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/sponsored-messages#getting-sponsored-video-advertisements
	//
	// Use SetMsgID and GetMsgID helpers.
	MsgID int
}

// MessagesGetSponsoredMessagesRequestTypeID is TL type id of MessagesGetSponsoredMessagesRequest.
const MessagesGetSponsoredMessagesRequestTypeID = 0x3d6ce850

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

func (g *MessagesGetSponsoredMessagesRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.MsgID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetSponsoredMessagesRequest from given interface.
func (g *MessagesGetSponsoredMessagesRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetMsgID() (value int, ok bool)
}) {
	g.Peer = from.GetPeer()
	if val, ok := from.GetMsgID(); ok {
		g.MsgID = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*MessagesGetSponsoredMessagesRequest) TypeName() string {
	return "messages.getSponsoredMessages"
}

// TypeInfo returns info about TL type.
func (g *MessagesGetSponsoredMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getSponsoredMessages",
		ID:   MessagesGetSponsoredMessagesRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "MsgID",
			SchemaName: "msg_id",
			Null:       !g.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *MessagesGetSponsoredMessagesRequest) SetFlags() {
	if !(g.MsgID == 0) {
		g.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (g *MessagesGetSponsoredMessagesRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSponsoredMessages#3d6ce850 as nil")
	}
	b.PutID(MessagesGetSponsoredMessagesRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetSponsoredMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getSponsoredMessages#3d6ce850 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSponsoredMessages#3d6ce850: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode messages.getSponsoredMessages#3d6ce850: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getSponsoredMessages#3d6ce850: field peer: %w", err)
	}
	if g.Flags.Has(0) {
		b.PutInt(g.MsgID)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *MessagesGetSponsoredMessagesRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getSponsoredMessages#3d6ce850 to nil")
	}
	if err := b.ConsumeID(MessagesGetSponsoredMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.getSponsoredMessages#3d6ce850: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetSponsoredMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getSponsoredMessages#3d6ce850 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.getSponsoredMessages#3d6ce850: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSponsoredMessages#3d6ce850: field peer: %w", err)
		}
		g.Peer = value
	}
	if g.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getSponsoredMessages#3d6ce850: field msg_id: %w", err)
		}
		g.MsgID = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (g *MessagesGetSponsoredMessagesRequest) GetPeer() (value InputPeerClass) {
	if g == nil {
		return
	}
	return g.Peer
}

// SetMsgID sets value of MsgID conditional field.
func (g *MessagesGetSponsoredMessagesRequest) SetMsgID(value int) {
	g.Flags.Set(0)
	g.MsgID = value
}

// GetMsgID returns value of MsgID conditional field and
// boolean which is true if field was set.
func (g *MessagesGetSponsoredMessagesRequest) GetMsgID() (value int, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(0) {
		return value, false
	}
	return g.MsgID, true
}

// MessagesGetSponsoredMessages invokes method messages.getSponsoredMessages#3d6ce850 returning error if any.
// Get a list of sponsored messages for a peer, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/sponsored-messages
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 CHANNEL_PRIVATE: You haven't joined this channel/supergroup.
//
// See https://core.telegram.org/method/messages.getSponsoredMessages for reference.
func (c *Client) MessagesGetSponsoredMessages(ctx context.Context, request *MessagesGetSponsoredMessagesRequest) (MessagesSponsoredMessagesClass, error) {
	var result MessagesSponsoredMessagesBox

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