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

// Background represents TL type `background#e65f291c`.
type Background struct {
	// Unique background identifier
	ID int64
	// True, if this is one of default backgrounds
	IsDefault bool
	// True, if the background is dark and is recommended to be used with dark theme
	IsDark bool
	// Unique background name
	Name string
	// Document with the background; may be null. Null only for filled and chat theme
	// backgrounds
	Document Document
	// Type of the background
	Type BackgroundTypeClass
}

// BackgroundTypeID is TL type id of Background.
const BackgroundTypeID = 0xe65f291c

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

func (b *Background) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.ID == 0) {
		return false
	}
	if !(b.IsDefault == false) {
		return false
	}
	if !(b.IsDark == false) {
		return false
	}
	if !(b.Name == "") {
		return false
	}
	if !(b.Document.Zero()) {
		return false
	}
	if !(b.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *Background) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "background",
		ID:   BackgroundTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "IsDefault",
			SchemaName: "is_default",
		},
		{
			Name:       "IsDark",
			SchemaName: "is_dark",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Document",
			SchemaName: "document",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *Background) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode background#e65f291c as nil")
	}
	buf.PutID(BackgroundTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *Background) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode background#e65f291c as nil")
	}
	buf.PutLong(b.ID)
	buf.PutBool(b.IsDefault)
	buf.PutBool(b.IsDark)
	buf.PutString(b.Name)
	if err := b.Document.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode background#e65f291c: field document: %w", err)
	}
	if b.Type == nil {
		return fmt.Errorf("unable to encode background#e65f291c: field type is nil")
	}
	if err := b.Type.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode background#e65f291c: field type: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (b *Background) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode background#e65f291c to nil")
	}
	{
		value, err := buf.Long()
		if err != nil {
			return fmt.Errorf("unable to decode background#e65f291c: field id: %w", err)
		}
		b.ID = value
	}
	{
		value, err := buf.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode background#e65f291c: field is_default: %w", err)
		}
		b.IsDefault = value
	}
	{
		value, err := buf.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode background#e65f291c: field is_dark: %w", err)
		}
		b.IsDark = value
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode background#e65f291c: field name: %w", err)
		}
		b.Name = value
	}
	{
		if err := b.Document.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode background#e65f291c: field document: %w", err)
		}
	}
	{
		value, err := DecodeBackgroundType(buf)
		if err != nil {
			return fmt.Errorf("unable to decode background#e65f291c: field type: %w", err)
		}
		b.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *Background) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode background#e65f291c as nil")
	}
	buf.ObjStart()
	buf.PutID("background")
	buf.Comma()
	buf.FieldStart("id")
	buf.PutLong(b.ID)
	buf.Comma()
	buf.FieldStart("is_default")
	buf.PutBool(b.IsDefault)
	buf.Comma()
	buf.FieldStart("is_dark")
	buf.PutBool(b.IsDark)
	buf.Comma()
	buf.FieldStart("name")
	buf.PutString(b.Name)
	buf.Comma()
	buf.FieldStart("document")
	if err := b.Document.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode background#e65f291c: field document: %w", err)
	}
	buf.Comma()
	buf.FieldStart("type")
	if b.Type == nil {
		return fmt.Errorf("unable to encode background#e65f291c: field type is nil")
	}
	if err := b.Type.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode background#e65f291c: field type: %w", err)
	}
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

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

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("background"); err != nil {
				return fmt.Errorf("unable to decode background#e65f291c: %w", err)
			}
		case "id":
			value, err := buf.Long()
			if err != nil {
				return fmt.Errorf("unable to decode background#e65f291c: field id: %w", err)
			}
			b.ID = value
		case "is_default":
			value, err := buf.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode background#e65f291c: field is_default: %w", err)
			}
			b.IsDefault = value
		case "is_dark":
			value, err := buf.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode background#e65f291c: field is_dark: %w", err)
			}
			b.IsDark = value
		case "name":
			value, err := buf.String()
			if err != nil {
				return fmt.Errorf("unable to decode background#e65f291c: field name: %w", err)
			}
			b.Name = value
		case "document":
			if err := b.Document.DecodeTDLibJSON(buf); err != nil {
				return fmt.Errorf("unable to decode background#e65f291c: field document: %w", err)
			}
		case "type":
			value, err := DecodeTDLibJSONBackgroundType(buf)
			if err != nil {
				return fmt.Errorf("unable to decode background#e65f291c: field type: %w", err)
			}
			b.Type = value
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (b *Background) GetID() (value int64) {
	if b == nil {
		return
	}
	return b.ID
}

// GetIsDefault returns value of IsDefault field.
func (b *Background) GetIsDefault() (value bool) {
	if b == nil {
		return
	}
	return b.IsDefault
}

// GetIsDark returns value of IsDark field.
func (b *Background) GetIsDark() (value bool) {
	if b == nil {
		return
	}
	return b.IsDark
}

// GetName returns value of Name field.
func (b *Background) GetName() (value string) {
	if b == nil {
		return
	}
	return b.Name
}

// GetDocument returns value of Document field.
func (b *Background) GetDocument() (value Document) {
	if b == nil {
		return
	}
	return b.Document
}

// GetType returns value of Type field.
func (b *Background) GetType() (value BackgroundTypeClass) {
	if b == nil {
		return
	}
	return b.Type
}
