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

// GetBotMediaPreviewInfoRequest represents TL type `getBotMediaPreviewInfo#50f60136`.
type GetBotMediaPreviewInfoRequest struct {
	// Identifier of the target bot. The bot must be owned and must have the main Web App
	BotUserID int64
	// A two-letter ISO 639-1 language code for which to get previews. If empty, then default
	// previews are returned
	LanguageCode string
}

// GetBotMediaPreviewInfoRequestTypeID is TL type id of GetBotMediaPreviewInfoRequest.
const GetBotMediaPreviewInfoRequestTypeID = 0x50f60136

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (g *GetBotMediaPreviewInfoRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotMediaPreviewInfo#50f60136 as nil")
	}
	b.PutID(GetBotMediaPreviewInfoRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetBotMediaPreviewInfoRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotMediaPreviewInfo#50f60136 as nil")
	}
	b.PutInt53(g.BotUserID)
	b.PutString(g.LanguageCode)
	return nil
}

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetBotMediaPreviewInfoRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getBotMediaPreviewInfo#50f60136 as nil")
	}
	b.ObjStart()
	b.PutID("getBotMediaPreviewInfo")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(g.BotUserID)
	b.Comma()
	b.FieldStart("language_code")
	b.PutString(g.LanguageCode)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// GetLanguageCode returns value of LanguageCode field.
func (g *GetBotMediaPreviewInfoRequest) GetLanguageCode() (value string) {
	if g == nil {
		return
	}
	return g.LanguageCode
}

// GetBotMediaPreviewInfo invokes method getBotMediaPreviewInfo#50f60136 returning error if any.
func (c *Client) GetBotMediaPreviewInfo(ctx context.Context, request *GetBotMediaPreviewInfoRequest) (*BotMediaPreviewInfo, error) {
	var result BotMediaPreviewInfo

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