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

// AddBotMediaPreviewRequest represents TL type `addBotMediaPreview#504b852b`.
type AddBotMediaPreviewRequest 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 preview is added. If empty, then the
	// preview will be shown to all users for whose languages there are no dedicated previews.
	LanguageCode string
	// Content of the added preview
	Content InputStoryContentClass
}

// AddBotMediaPreviewRequestTypeID is TL type id of AddBotMediaPreviewRequest.
const AddBotMediaPreviewRequestTypeID = 0x504b852b

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AddBotMediaPreviewRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "addBotMediaPreview",
		ID:   AddBotMediaPreviewRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "LanguageCode",
			SchemaName: "language_code",
		},
		{
			Name:       "Content",
			SchemaName: "content",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AddBotMediaPreviewRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addBotMediaPreview#504b852b as nil")
	}
	b.PutID(AddBotMediaPreviewRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AddBotMediaPreviewRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addBotMediaPreview#504b852b as nil")
	}
	b.PutInt53(a.BotUserID)
	b.PutString(a.LanguageCode)
	if a.Content == nil {
		return fmt.Errorf("unable to encode addBotMediaPreview#504b852b: field content is nil")
	}
	if err := a.Content.Encode(b); err != nil {
		return fmt.Errorf("unable to encode addBotMediaPreview#504b852b: field content: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AddBotMediaPreviewRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addBotMediaPreview#504b852b to nil")
	}
	if err := b.ConsumeID(AddBotMediaPreviewRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode addBotMediaPreview#504b852b: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AddBotMediaPreviewRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addBotMediaPreview#504b852b to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode addBotMediaPreview#504b852b: field bot_user_id: %w", err)
		}
		a.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode addBotMediaPreview#504b852b: field language_code: %w", err)
		}
		a.LanguageCode = value
	}
	{
		value, err := DecodeInputStoryContent(b)
		if err != nil {
			return fmt.Errorf("unable to decode addBotMediaPreview#504b852b: field content: %w", err)
		}
		a.Content = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AddBotMediaPreviewRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode addBotMediaPreview#504b852b as nil")
	}
	b.ObjStart()
	b.PutID("addBotMediaPreview")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(a.BotUserID)
	b.Comma()
	b.FieldStart("language_code")
	b.PutString(a.LanguageCode)
	b.Comma()
	b.FieldStart("content")
	if a.Content == nil {
		return fmt.Errorf("unable to encode addBotMediaPreview#504b852b: field content is nil")
	}
	if err := a.Content.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode addBotMediaPreview#504b852b: field content: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AddBotMediaPreviewRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode addBotMediaPreview#504b852b to nil")
	}

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

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

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

// GetContent returns value of Content field.
func (a *AddBotMediaPreviewRequest) GetContent() (value InputStoryContentClass) {
	if a == nil {
		return
	}
	return a.Content
}

// AddBotMediaPreview invokes method addBotMediaPreview#504b852b returning error if any.
func (c *Client) AddBotMediaPreview(ctx context.Context, request *AddBotMediaPreviewRequest) (*BotMediaPreview, error) {
	var result BotMediaPreview

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