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

// BotMediaPreviews represents TL type `botMediaPreviews#f1b8f27d`.
type BotMediaPreviews struct {
	// List of media previews
	Previews []BotMediaPreview
}

// BotMediaPreviewsTypeID is TL type id of BotMediaPreviews.
const BotMediaPreviewsTypeID = 0xf1b8f27d

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (b *BotMediaPreviews) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreviews#f1b8f27d as nil")
	}
	buf.PutID(BotMediaPreviewsTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotMediaPreviews) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreviews#f1b8f27d 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 botMediaPreviews#f1b8f27d: field previews element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (b *BotMediaPreviews) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMediaPreviews#f1b8f27d to nil")
	}
	{
		headerLen, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode botMediaPreviews#f1b8f27d: 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 botMediaPreviews#f1b8f27d: field previews: %w", err)
			}
			b.Previews = append(b.Previews, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BotMediaPreviews) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreviews#f1b8f27d as nil")
	}
	buf.ObjStart()
	buf.PutID("botMediaPreviews")
	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 botMediaPreviews#f1b8f27d: field previews element with index %d: %w", idx, err)
		}
		buf.Comma()
	}
	buf.StripComma()
	buf.ArrEnd()
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

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

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("botMediaPreviews"); err != nil {
				return fmt.Errorf("unable to decode botMediaPreviews#f1b8f27d: %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 botMediaPreviews#f1b8f27d: field previews: %w", err)
				}
				b.Previews = append(b.Previews, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode botMediaPreviews#f1b8f27d: field previews: %w", err)
			}
		default:
			return buf.Skip()
		}
		return nil
	})
}

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