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

// ChatBackground represents TL type `chatBackground#62891968`.
type ChatBackground struct {
	// The background
	Background Background
	// Dimming of the background in dark themes, as a percentage; 0-100. Applied only to
	// Wallpaper and Fill types of background
	DarkThemeDimming int32
}

// ChatBackgroundTypeID is TL type id of ChatBackground.
const ChatBackgroundTypeID = 0x62891968

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

func (c *ChatBackground) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Background.Zero()) {
		return false
	}
	if !(c.DarkThemeDimming == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatBackground) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatBackground",
		ID:   ChatBackgroundTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Background",
			SchemaName: "background",
		},
		{
			Name:       "DarkThemeDimming",
			SchemaName: "dark_theme_dimming",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatBackground) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatBackground#62891968 as nil")
	}
	b.PutID(ChatBackgroundTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatBackground) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatBackground#62891968 as nil")
	}
	if err := c.Background.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatBackground#62891968: field background: %w", err)
	}
	b.PutInt32(c.DarkThemeDimming)
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatBackground) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatBackground#62891968 to nil")
	}
	if err := b.ConsumeID(ChatBackgroundTypeID); err != nil {
		return fmt.Errorf("unable to decode chatBackground#62891968: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatBackground) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatBackground#62891968 to nil")
	}
	{
		if err := c.Background.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatBackground#62891968: field background: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatBackground#62891968: field dark_theme_dimming: %w", err)
		}
		c.DarkThemeDimming = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatBackground) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatBackground#62891968 as nil")
	}
	b.ObjStart()
	b.PutID("chatBackground")
	b.Comma()
	b.FieldStart("background")
	if err := c.Background.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatBackground#62891968: field background: %w", err)
	}
	b.Comma()
	b.FieldStart("dark_theme_dimming")
	b.PutInt32(c.DarkThemeDimming)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatBackground) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatBackground#62891968 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatBackground"); err != nil {
				return fmt.Errorf("unable to decode chatBackground#62891968: %w", err)
			}
		case "background":
			if err := c.Background.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode chatBackground#62891968: field background: %w", err)
			}
		case "dark_theme_dimming":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatBackground#62891968: field dark_theme_dimming: %w", err)
			}
			c.DarkThemeDimming = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBackground returns value of Background field.
func (c *ChatBackground) GetBackground() (value Background) {
	if c == nil {
		return
	}
	return c.Background
}

// GetDarkThemeDimming returns value of DarkThemeDimming field.
func (c *ChatBackground) GetDarkThemeDimming() (value int32) {
	if c == nil {
		return
	}
	return c.DarkThemeDimming
}
