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

// GetBotMediaPreviewsRequest represents TL type `getBotMediaPreviews#22665458`.
type GetBotMediaPreviewsRequest struct {
	// Identifier of the target bot. The bot must have the main Web App
	BotUserID int64
}

// GetBotMediaPreviewsRequestTypeID is TL type id of GetBotMediaPreviewsRequest.
const GetBotMediaPreviewsRequestTypeID = 0x22665458

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

func (g *GetBotMediaPreviewsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.BotUserID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetBotMediaPreviewsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getBotMediaPreviews",
		ID:   GetBotMediaPreviewsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetBotMediaPreviewsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotMediaPreviews#22665458 as nil")
	}
	b.PutID(GetBotMediaPreviewsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetBotMediaPreviewsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotMediaPreviews#22665458 as nil")
	}
	b.PutInt53(g.BotUserID)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetBotMediaPreviewsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBotMediaPreviews#22665458 to nil")
	}
	if err := b.ConsumeID(GetBotMediaPreviewsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getBotMediaPreviews#22665458: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetBotMediaPreviewsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBotMediaPreviews#22665458 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getBotMediaPreviews#22665458: field bot_user_id: %w", err)
		}
		g.BotUserID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetBotMediaPreviewsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotMediaPreviews#22665458 as nil")
	}
	b.ObjStart()
	b.PutID("getBotMediaPreviews")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(g.BotUserID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetBotMediaPreviewsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getBotMediaPreviews#22665458 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getBotMediaPreviews"); err != nil {
				return fmt.Errorf("unable to decode getBotMediaPreviews#22665458: %w", err)
			}
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getBotMediaPreviews#22665458: field bot_user_id: %w", err)
			}
			g.BotUserID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBotUserID returns value of BotUserID field.
func (g *GetBotMediaPreviewsRequest) GetBotUserID() (value int64) {
	if g == nil {
		return
	}
	return g.BotUserID
}

// GetBotMediaPreviews invokes method getBotMediaPreviews#22665458 returning error if any.
func (c *Client) GetBotMediaPreviews(ctx context.Context, botuserid int64) (*BotMediaPreviews, error) {
	var result BotMediaPreviews

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