// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// ChatTheme represents TL type `chatTheme#c3dffc04`.
// A chat theme
//
// See https://core.telegram.org/constructor/chatTheme for reference.
type ChatTheme struct {
	// The emoji identifying the chat theme.
	Emoticon string
}

// ChatThemeTypeID is TL type id of ChatTheme.
const ChatThemeTypeID = 0xc3dffc04

// construct implements constructor of ChatThemeClass.
func (c ChatTheme) construct() ChatThemeClass { return &c }

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

	_ ChatThemeClass = &ChatTheme{}
)

func (c *ChatTheme) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Emoticon == "") {
		return false
	}

	return true
}

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

// FillFrom fills ChatTheme from given interface.
func (c *ChatTheme) FillFrom(from interface {
	GetEmoticon() (value string)
}) {
	c.Emoticon = from.GetEmoticon()
}

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

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

// TypeInfo returns info about TL type.
func (c *ChatTheme) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatTheme",
		ID:   ChatThemeTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Emoticon",
			SchemaName: "emoticon",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatTheme) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatTheme#c3dffc04 as nil")
	}
	b.PutID(ChatThemeTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatTheme) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatTheme#c3dffc04 as nil")
	}
	b.PutString(c.Emoticon)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatTheme) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatTheme#c3dffc04 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode chatTheme#c3dffc04: field emoticon: %w", err)
		}
		c.Emoticon = value
	}
	return nil
}

// GetEmoticon returns value of Emoticon field.
func (c *ChatTheme) GetEmoticon() (value string) {
	if c == nil {
		return
	}
	return c.Emoticon
}

// ChatThemeUniqueGift represents TL type `chatThemeUniqueGift#3458f9c8`.
// A chat theme based on a collectible gift »¹.
//
// Links:
//  1. https://core.telegram.org/api/gifts#collectible-gifts
//
// See https://core.telegram.org/constructor/chatThemeUniqueGift for reference.
type ChatThemeUniqueGift struct {
	// The owned collectible gift on which this theme is based, as a starGiftUnique¹
	// constructor.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/starGiftUnique
	Gift StarGiftClass
	// Theme settings.
	ThemeSettings []ThemeSettings
}

// ChatThemeUniqueGiftTypeID is TL type id of ChatThemeUniqueGift.
const ChatThemeUniqueGiftTypeID = 0x3458f9c8

// construct implements constructor of ChatThemeClass.
func (c ChatThemeUniqueGift) construct() ChatThemeClass { return &c }

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

	_ ChatThemeClass = &ChatThemeUniqueGift{}
)

func (c *ChatThemeUniqueGift) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Gift == nil) {
		return false
	}
	if !(c.ThemeSettings == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ChatThemeUniqueGift from given interface.
func (c *ChatThemeUniqueGift) FillFrom(from interface {
	GetGift() (value StarGiftClass)
	GetThemeSettings() (value []ThemeSettings)
}) {
	c.Gift = from.GetGift()
	c.ThemeSettings = from.GetThemeSettings()
}

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

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

// TypeInfo returns info about TL type.
func (c *ChatThemeUniqueGift) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatThemeUniqueGift",
		ID:   ChatThemeUniqueGiftTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Gift",
			SchemaName: "gift",
		},
		{
			Name:       "ThemeSettings",
			SchemaName: "theme_settings",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatThemeUniqueGift) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatThemeUniqueGift#3458f9c8 as nil")
	}
	b.PutID(ChatThemeUniqueGiftTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatThemeUniqueGift) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatThemeUniqueGift#3458f9c8 as nil")
	}
	if c.Gift == nil {
		return fmt.Errorf("unable to encode chatThemeUniqueGift#3458f9c8: field gift is nil")
	}
	if err := c.Gift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatThemeUniqueGift#3458f9c8: field gift: %w", err)
	}
	b.PutVectorHeader(len(c.ThemeSettings))
	for idx, v := range c.ThemeSettings {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode chatThemeUniqueGift#3458f9c8: field theme_settings element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatThemeUniqueGift) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatThemeUniqueGift#3458f9c8 to nil")
	}
	{
		value, err := DecodeStarGift(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatThemeUniqueGift#3458f9c8: field gift: %w", err)
		}
		c.Gift = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode chatThemeUniqueGift#3458f9c8: field theme_settings: %w", err)
		}

		if headerLen > 0 {
			c.ThemeSettings = make([]ThemeSettings, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ThemeSettings
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode chatThemeUniqueGift#3458f9c8: field theme_settings: %w", err)
			}
			c.ThemeSettings = append(c.ThemeSettings, value)
		}
	}
	return nil
}

// GetGift returns value of Gift field.
func (c *ChatThemeUniqueGift) GetGift() (value StarGiftClass) {
	if c == nil {
		return
	}
	return c.Gift
}

// GetThemeSettings returns value of ThemeSettings field.
func (c *ChatThemeUniqueGift) GetThemeSettings() (value []ThemeSettings) {
	if c == nil {
		return
	}
	return c.ThemeSettings
}

// ChatThemeClassName is schema name of ChatThemeClass.
const ChatThemeClassName = "ChatTheme"

// ChatThemeClass represents ChatTheme generic type.
//
// See https://core.telegram.org/type/ChatTheme for reference.
//
// Example:
//
//	g, err := tg.DecodeChatTheme(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.ChatTheme: // chatTheme#c3dffc04
//	case *tg.ChatThemeUniqueGift: // chatThemeUniqueGift#3458f9c8
//	default: panic(v)
//	}
type ChatThemeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() ChatThemeClass

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

// AsInput tries to map ChatTheme to InputChatTheme.
func (c *ChatTheme) AsInput() *InputChatTheme {
	value := new(InputChatTheme)
	value.Emoticon = c.GetEmoticon()

	return value
}

// DecodeChatTheme implements binary de-serialization for ChatThemeClass.
func DecodeChatTheme(buf *bin.Buffer) (ChatThemeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case ChatThemeTypeID:
		// Decoding chatTheme#c3dffc04.
		v := ChatTheme{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatThemeClass: %w", err)
		}
		return &v, nil
	case ChatThemeUniqueGiftTypeID:
		// Decoding chatThemeUniqueGift#3458f9c8.
		v := ChatThemeUniqueGift{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatThemeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatThemeClass: %w", bin.NewUnexpectedID(id))
	}
}

// ChatTheme boxes the ChatThemeClass providing a helper.
type ChatThemeBox struct {
	ChatTheme ChatThemeClass
}

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

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