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

// InputBackgroundLocal represents TL type `inputBackgroundLocal#97dd74a4`.
type InputBackgroundLocal struct {
	// Background file to use. Only inputFileLocal and inputFileGenerated are supported. The
	// file must be in JPEG format for wallpapers and in PNG format for patterns
	Background InputFileClass
}

// InputBackgroundLocalTypeID is TL type id of InputBackgroundLocal.
const InputBackgroundLocalTypeID = 0x97dd74a4

// construct implements constructor of InputBackgroundClass.
func (i InputBackgroundLocal) construct() InputBackgroundClass { return &i }

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

	_ InputBackgroundClass = &InputBackgroundLocal{}
)

func (i *InputBackgroundLocal) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Background == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InputBackgroundLocal) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputBackgroundLocal",
		ID:   InputBackgroundLocalTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Background",
			SchemaName: "background",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputBackgroundLocal) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundLocal#97dd74a4 as nil")
	}
	b.PutID(InputBackgroundLocalTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputBackgroundLocal) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundLocal#97dd74a4 as nil")
	}
	if i.Background == nil {
		return fmt.Errorf("unable to encode inputBackgroundLocal#97dd74a4: field background is nil")
	}
	if err := i.Background.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputBackgroundLocal#97dd74a4: field background: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InputBackgroundLocal) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundLocal#97dd74a4 to nil")
	}
	if err := b.ConsumeID(InputBackgroundLocalTypeID); err != nil {
		return fmt.Errorf("unable to decode inputBackgroundLocal#97dd74a4: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputBackgroundLocal) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundLocal#97dd74a4 to nil")
	}
	{
		value, err := DecodeInputFile(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputBackgroundLocal#97dd74a4: field background: %w", err)
		}
		i.Background = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InputBackgroundLocal) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundLocal#97dd74a4 as nil")
	}
	b.ObjStart()
	b.PutID("inputBackgroundLocal")
	b.Comma()
	b.FieldStart("background")
	if i.Background == nil {
		return fmt.Errorf("unable to encode inputBackgroundLocal#97dd74a4: field background is nil")
	}
	if err := i.Background.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputBackgroundLocal#97dd74a4: field background: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InputBackgroundLocal) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundLocal#97dd74a4 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inputBackgroundLocal"); err != nil {
				return fmt.Errorf("unable to decode inputBackgroundLocal#97dd74a4: %w", err)
			}
		case "background":
			value, err := DecodeTDLibJSONInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputBackgroundLocal#97dd74a4: field background: %w", err)
			}
			i.Background = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBackground returns value of Background field.
func (i *InputBackgroundLocal) GetBackground() (value InputFileClass) {
	if i == nil {
		return
	}
	return i.Background
}

// InputBackgroundRemote represents TL type `inputBackgroundRemote#ef9c3219`.
type InputBackgroundRemote struct {
	// The background identifier
	BackgroundID int64
}

// InputBackgroundRemoteTypeID is TL type id of InputBackgroundRemote.
const InputBackgroundRemoteTypeID = 0xef9c3219

// construct implements constructor of InputBackgroundClass.
func (i InputBackgroundRemote) construct() InputBackgroundClass { return &i }

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

	_ InputBackgroundClass = &InputBackgroundRemote{}
)

func (i *InputBackgroundRemote) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.BackgroundID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InputBackgroundRemote) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputBackgroundRemote",
		ID:   InputBackgroundRemoteTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BackgroundID",
			SchemaName: "background_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputBackgroundRemote) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundRemote#ef9c3219 as nil")
	}
	b.PutID(InputBackgroundRemoteTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputBackgroundRemote) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundRemote#ef9c3219 as nil")
	}
	b.PutLong(i.BackgroundID)
	return nil
}

// Decode implements bin.Decoder.
func (i *InputBackgroundRemote) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundRemote#ef9c3219 to nil")
	}
	if err := b.ConsumeID(InputBackgroundRemoteTypeID); err != nil {
		return fmt.Errorf("unable to decode inputBackgroundRemote#ef9c3219: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputBackgroundRemote) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundRemote#ef9c3219 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode inputBackgroundRemote#ef9c3219: field background_id: %w", err)
		}
		i.BackgroundID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InputBackgroundRemote) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundRemote#ef9c3219 as nil")
	}
	b.ObjStart()
	b.PutID("inputBackgroundRemote")
	b.Comma()
	b.FieldStart("background_id")
	b.PutLong(i.BackgroundID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InputBackgroundRemote) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundRemote#ef9c3219 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inputBackgroundRemote"); err != nil {
				return fmt.Errorf("unable to decode inputBackgroundRemote#ef9c3219: %w", err)
			}
		case "background_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode inputBackgroundRemote#ef9c3219: field background_id: %w", err)
			}
			i.BackgroundID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBackgroundID returns value of BackgroundID field.
func (i *InputBackgroundRemote) GetBackgroundID() (value int64) {
	if i == nil {
		return
	}
	return i.BackgroundID
}

// InputBackgroundPrevious represents TL type `inputBackgroundPrevious#eb06575e`.
type InputBackgroundPrevious struct {
	// Identifier of the message with the background
	MessageID int64
}

// InputBackgroundPreviousTypeID is TL type id of InputBackgroundPrevious.
const InputBackgroundPreviousTypeID = 0xeb06575e

// construct implements constructor of InputBackgroundClass.
func (i InputBackgroundPrevious) construct() InputBackgroundClass { return &i }

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

	_ InputBackgroundClass = &InputBackgroundPrevious{}
)

func (i *InputBackgroundPrevious) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.MessageID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InputBackgroundPrevious) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputBackgroundPrevious",
		ID:   InputBackgroundPreviousTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputBackgroundPrevious) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundPrevious#eb06575e as nil")
	}
	b.PutID(InputBackgroundPreviousTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputBackgroundPrevious) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundPrevious#eb06575e as nil")
	}
	b.PutInt53(i.MessageID)
	return nil
}

// Decode implements bin.Decoder.
func (i *InputBackgroundPrevious) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundPrevious#eb06575e to nil")
	}
	if err := b.ConsumeID(InputBackgroundPreviousTypeID); err != nil {
		return fmt.Errorf("unable to decode inputBackgroundPrevious#eb06575e: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputBackgroundPrevious) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundPrevious#eb06575e to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode inputBackgroundPrevious#eb06575e: field message_id: %w", err)
		}
		i.MessageID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InputBackgroundPrevious) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inputBackgroundPrevious#eb06575e as nil")
	}
	b.ObjStart()
	b.PutID("inputBackgroundPrevious")
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(i.MessageID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InputBackgroundPrevious) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inputBackgroundPrevious#eb06575e to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inputBackgroundPrevious"); err != nil {
				return fmt.Errorf("unable to decode inputBackgroundPrevious#eb06575e: %w", err)
			}
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode inputBackgroundPrevious#eb06575e: field message_id: %w", err)
			}
			i.MessageID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMessageID returns value of MessageID field.
func (i *InputBackgroundPrevious) GetMessageID() (value int64) {
	if i == nil {
		return
	}
	return i.MessageID
}

// InputBackgroundClassName is schema name of InputBackgroundClass.
const InputBackgroundClassName = "InputBackground"

// InputBackgroundClass represents InputBackground generic type.
//
// Example:
//
//	g, err := tdapi.DecodeInputBackground(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.InputBackgroundLocal: // inputBackgroundLocal#97dd74a4
//	case *tdapi.InputBackgroundRemote: // inputBackgroundRemote#ef9c3219
//	case *tdapi.InputBackgroundPrevious: // inputBackgroundPrevious#eb06575e
//	default: panic(v)
//	}
type InputBackgroundClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() InputBackgroundClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeInputBackground implements binary de-serialization for InputBackgroundClass.
func DecodeInputBackground(buf *bin.Buffer) (InputBackgroundClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case InputBackgroundLocalTypeID:
		// Decoding inputBackgroundLocal#97dd74a4.
		v := InputBackgroundLocal{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode InputBackgroundClass: %w", err)
		}
		return &v, nil
	case InputBackgroundRemoteTypeID:
		// Decoding inputBackgroundRemote#ef9c3219.
		v := InputBackgroundRemote{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode InputBackgroundClass: %w", err)
		}
		return &v, nil
	case InputBackgroundPreviousTypeID:
		// Decoding inputBackgroundPrevious#eb06575e.
		v := InputBackgroundPrevious{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode InputBackgroundClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode InputBackgroundClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONInputBackground implements binary de-serialization for InputBackgroundClass.
func DecodeTDLibJSONInputBackground(buf tdjson.Decoder) (InputBackgroundClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "inputBackgroundLocal":
		// Decoding inputBackgroundLocal#97dd74a4.
		v := InputBackgroundLocal{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode InputBackgroundClass: %w", err)
		}
		return &v, nil
	case "inputBackgroundRemote":
		// Decoding inputBackgroundRemote#ef9c3219.
		v := InputBackgroundRemote{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode InputBackgroundClass: %w", err)
		}
		return &v, nil
	case "inputBackgroundPrevious":
		// Decoding inputBackgroundPrevious#eb06575e.
		v := InputBackgroundPrevious{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode InputBackgroundClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode InputBackgroundClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// InputBackground boxes the InputBackgroundClass providing a helper.
type InputBackgroundBox struct {
	InputBackground InputBackgroundClass
}

// Decode implements bin.Decoder for InputBackgroundBox.
func (b *InputBackgroundBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode InputBackgroundBox to nil")
	}
	v, err := DecodeInputBackground(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.InputBackground = v
	return nil
}

// Encode implements bin.Encode for InputBackgroundBox.
func (b *InputBackgroundBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.InputBackground == nil {
		return fmt.Errorf("unable to encode InputBackgroundClass as nil")
	}
	return b.InputBackground.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for InputBackgroundBox.
func (b *InputBackgroundBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode InputBackgroundBox to nil")
	}
	v, err := DecodeTDLibJSONInputBackground(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.InputBackground = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for InputBackgroundBox.
func (b *InputBackgroundBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.InputBackground == nil {
		return fmt.Errorf("unable to encode InputBackgroundClass as nil")
	}
	return b.InputBackground.EncodeTDLibJSON(buf)
}
