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

// Backgrounds represents TL type `backgrounds#b4671319`.
type Backgrounds struct {
	// A list of backgrounds
	Backgrounds []Background
}

// BackgroundsTypeID is TL type id of Backgrounds.
const BackgroundsTypeID = 0xb4671319

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (b *Backgrounds) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode backgrounds#b4671319 as nil")
	}
	buf.PutID(BackgroundsTypeID)
	return b.EncodeBare(buf)
}

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

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

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

		if headerLen > 0 {
			b.Backgrounds = make([]Background, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Background
			if err := value.DecodeBare(buf); err != nil {
				return fmt.Errorf("unable to decode bare backgrounds#b4671319: field backgrounds: %w", err)
			}
			b.Backgrounds = append(b.Backgrounds, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *Backgrounds) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode backgrounds#b4671319 as nil")
	}
	buf.ObjStart()
	buf.PutID("backgrounds")
	buf.Comma()
	buf.FieldStart("backgrounds")
	buf.ArrStart()
	for idx, v := range b.Backgrounds {
		if err := v.EncodeTDLibJSON(buf); err != nil {
			return fmt.Errorf("unable to encode backgrounds#b4671319: field backgrounds 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 *Backgrounds) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode backgrounds#b4671319 to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("backgrounds"); err != nil {
				return fmt.Errorf("unable to decode backgrounds#b4671319: %w", err)
			}
		case "backgrounds":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				var value Background
				if err := value.DecodeTDLibJSON(buf); err != nil {
					return fmt.Errorf("unable to decode backgrounds#b4671319: field backgrounds: %w", err)
				}
				b.Backgrounds = append(b.Backgrounds, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode backgrounds#b4671319: field backgrounds: %w", err)
			}
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetBackgrounds returns value of Backgrounds field.
func (b *Backgrounds) GetBackgrounds() (value []Background) {
	if b == nil {
		return
	}
	return b.Backgrounds
}
