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

// BotMediaPreviewInfo represents TL type `botMediaPreviewInfo#e49169db`.
type BotMediaPreviewInfo struct {
	// List of media previews
	Previews []BotMediaPreview
	// List of language codes for which the bot has dedicated previews
	LanguageCodes []string
}

// BotMediaPreviewInfoTypeID is TL type id of BotMediaPreviewInfo.
const BotMediaPreviewInfoTypeID = 0xe49169db

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

func (b *BotMediaPreviewInfo) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Previews == nil) {
		return false
	}
	if !(b.LanguageCodes == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BotMediaPreviewInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botMediaPreviewInfo",
		ID:   BotMediaPreviewInfoTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Previews",
			SchemaName: "previews",
		},
		{
			Name:       "LanguageCodes",
			SchemaName: "language_codes",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BotMediaPreviewInfo) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreviewInfo#e49169db as nil")
	}
	buf.PutID(BotMediaPreviewInfoTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotMediaPreviewInfo) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreviewInfo#e49169db as nil")
	}
	buf.PutInt(len(b.Previews))
	for idx, v := range b.Previews {
		if err := v.EncodeBare(buf); err != nil {
			return fmt.Errorf("unable to encode bare botMediaPreviewInfo#e49169db: field previews element with index %d: %w", idx, err)
		}
	}
	buf.PutInt(len(b.LanguageCodes))
	for _, v := range b.LanguageCodes {
		buf.PutString(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BotMediaPreviewInfo) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMediaPreviewInfo#e49169db to nil")
	}
	if err := buf.ConsumeID(BotMediaPreviewInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BotMediaPreviewInfo) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMediaPreviewInfo#e49169db to nil")
	}
	{
		headerLen, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: field previews: %w", err)
		}

		if headerLen > 0 {
			b.Previews = make([]BotMediaPreview, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BotMediaPreview
			if err := value.DecodeBare(buf); err != nil {
				return fmt.Errorf("unable to decode bare botMediaPreviewInfo#e49169db: field previews: %w", err)
			}
			b.Previews = append(b.Previews, value)
		}
	}
	{
		headerLen, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: field language_codes: %w", err)
		}

		if headerLen > 0 {
			b.LanguageCodes = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := buf.String()
			if err != nil {
				return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: field language_codes: %w", err)
			}
			b.LanguageCodes = append(b.LanguageCodes, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BotMediaPreviewInfo) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreviewInfo#e49169db as nil")
	}
	buf.ObjStart()
	buf.PutID("botMediaPreviewInfo")
	buf.Comma()
	buf.FieldStart("previews")
	buf.ArrStart()
	for idx, v := range b.Previews {
		if err := v.EncodeTDLibJSON(buf); err != nil {
			return fmt.Errorf("unable to encode botMediaPreviewInfo#e49169db: field previews element with index %d: %w", idx, err)
		}
		buf.Comma()
	}
	buf.StripComma()
	buf.ArrEnd()
	buf.Comma()
	buf.FieldStart("language_codes")
	buf.ArrStart()
	for _, v := range b.LanguageCodes {
		buf.PutString(v)
		buf.Comma()
	}
	buf.StripComma()
	buf.ArrEnd()
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BotMediaPreviewInfo) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode botMediaPreviewInfo#e49169db to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("botMediaPreviewInfo"); err != nil {
				return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: %w", err)
			}
		case "previews":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				var value BotMediaPreview
				if err := value.DecodeTDLibJSON(buf); err != nil {
					return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: field previews: %w", err)
				}
				b.Previews = append(b.Previews, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: field previews: %w", err)
			}
		case "language_codes":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				value, err := buf.String()
				if err != nil {
					return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: field language_codes: %w", err)
				}
				b.LanguageCodes = append(b.LanguageCodes, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode botMediaPreviewInfo#e49169db: field language_codes: %w", err)
			}
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetPreviews returns value of Previews field.
func (b *BotMediaPreviewInfo) GetPreviews() (value []BotMediaPreview) {
	if b == nil {
		return
	}
	return b.Previews
}

// GetLanguageCodes returns value of LanguageCodes field.
func (b *BotMediaPreviewInfo) GetLanguageCodes() (value []string) {
	if b == nil {
		return
	}
	return b.LanguageCodes
}
