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

// BotsGetPreviewInfoRequest represents TL type `bots.getPreviewInfo#423ab3ad`.
// Bot owners only, fetch main mini app preview information, see here »¹ for more info.
// Note: technically non-owners may also invoke this method, but it will always behave
// exactly as bots.getPreviewMedias¹, returning only previews for the current language
// and an empty lang_codes array, regardless of the passed lang_code, so please only use
// bots.getPreviewMedias² if you're not the owner of the bot.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps#main-mini-app-previews
//  2. https://core.telegram.org/method/bots.getPreviewMedias
//  3. https://core.telegram.org/method/bots.getPreviewMedias
//
// See https://core.telegram.org/method/bots.getPreviewInfo for reference.
type BotsGetPreviewInfoRequest struct {
	// The bot that owns the Main Mini App.
	Bot InputUserClass
	// Fetch previews for the specified ISO 639-1 language code.
	LangCode string
}

// BotsGetPreviewInfoRequestTypeID is TL type id of BotsGetPreviewInfoRequest.
const BotsGetPreviewInfoRequestTypeID = 0x423ab3ad

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

func (g *BotsGetPreviewInfoRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Bot == nil) {
		return false
	}
	if !(g.LangCode == "") {
		return false
	}

	return true
}

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

// FillFrom fills BotsGetPreviewInfoRequest from given interface.
func (g *BotsGetPreviewInfoRequest) FillFrom(from interface {
	GetBot() (value InputUserClass)
	GetLangCode() (value string)
}) {
	g.Bot = from.GetBot()
	g.LangCode = from.GetLangCode()
}

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

// TypeName returns name of type in TL schema.
func (*BotsGetPreviewInfoRequest) TypeName() string {
	return "bots.getPreviewInfo"
}

// TypeInfo returns info about TL type.
func (g *BotsGetPreviewInfoRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.getPreviewInfo",
		ID:   BotsGetPreviewInfoRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Bot",
			SchemaName: "bot",
		},
		{
			Name:       "LangCode",
			SchemaName: "lang_code",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *BotsGetPreviewInfoRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode bots.getPreviewInfo#423ab3ad as nil")
	}
	b.PutID(BotsGetPreviewInfoRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *BotsGetPreviewInfoRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode bots.getPreviewInfo#423ab3ad as nil")
	}
	if g.Bot == nil {
		return fmt.Errorf("unable to encode bots.getPreviewInfo#423ab3ad: field bot is nil")
	}
	if err := g.Bot.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.getPreviewInfo#423ab3ad: field bot: %w", err)
	}
	b.PutString(g.LangCode)
	return nil
}

// Decode implements bin.Decoder.
func (g *BotsGetPreviewInfoRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode bots.getPreviewInfo#423ab3ad to nil")
	}
	if err := b.ConsumeID(BotsGetPreviewInfoRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.getPreviewInfo#423ab3ad: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *BotsGetPreviewInfoRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode bots.getPreviewInfo#423ab3ad to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.getPreviewInfo#423ab3ad: field bot: %w", err)
		}
		g.Bot = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.getPreviewInfo#423ab3ad: field lang_code: %w", err)
		}
		g.LangCode = value
	}
	return nil
}

// GetBot returns value of Bot field.
func (g *BotsGetPreviewInfoRequest) GetBot() (value InputUserClass) {
	if g == nil {
		return
	}
	return g.Bot
}

// GetLangCode returns value of LangCode field.
func (g *BotsGetPreviewInfoRequest) GetLangCode() (value string) {
	if g == nil {
		return
	}
	return g.LangCode
}

// BotsGetPreviewInfo invokes method bots.getPreviewInfo#423ab3ad returning error if any.
// Bot owners only, fetch main mini app preview information, see here »¹ for more info.
// Note: technically non-owners may also invoke this method, but it will always behave
// exactly as bots.getPreviewMedias¹, returning only previews for the current language
// and an empty lang_codes array, regardless of the passed lang_code, so please only use
// bots.getPreviewMedias² if you're not the owner of the bot.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps#main-mini-app-previews
//  2. https://core.telegram.org/method/bots.getPreviewMedias
//  3. https://core.telegram.org/method/bots.getPreviewMedias
//
// Possible errors:
//
//	400 BOT_INVALID: This is not a valid bot.
//
// See https://core.telegram.org/method/bots.getPreviewInfo for reference.
func (c *Client) BotsGetPreviewInfo(ctx context.Context, request *BotsGetPreviewInfoRequest) (*BotsPreviewInfo, error) {
	var result BotsPreviewInfo

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