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

// ForumTopicIcon represents TL type `forumTopicIcon#cf32a193`.
type ForumTopicIcon struct {
	// Color of the topic icon in RGB format
	Color int32
	// Unique identifier of the custom emoji shown on the topic icon; 0 if none
	CustomEmojiID int64
}

// ForumTopicIconTypeID is TL type id of ForumTopicIcon.
const ForumTopicIconTypeID = 0xcf32a193

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

func (f *ForumTopicIcon) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Color == 0) {
		return false
	}
	if !(f.CustomEmojiID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *ForumTopicIcon) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "forumTopicIcon",
		ID:   ForumTopicIconTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Color",
			SchemaName: "color",
		},
		{
			Name:       "CustomEmojiID",
			SchemaName: "custom_emoji_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *ForumTopicIcon) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode forumTopicIcon#cf32a193 as nil")
	}
	b.PutID(ForumTopicIconTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *ForumTopicIcon) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode forumTopicIcon#cf32a193 as nil")
	}
	b.PutInt32(f.Color)
	b.PutLong(f.CustomEmojiID)
	return nil
}

// Decode implements bin.Decoder.
func (f *ForumTopicIcon) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode forumTopicIcon#cf32a193 to nil")
	}
	if err := b.ConsumeID(ForumTopicIconTypeID); err != nil {
		return fmt.Errorf("unable to decode forumTopicIcon#cf32a193: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *ForumTopicIcon) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode forumTopicIcon#cf32a193 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode forumTopicIcon#cf32a193: field color: %w", err)
		}
		f.Color = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode forumTopicIcon#cf32a193: field custom_emoji_id: %w", err)
		}
		f.CustomEmojiID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *ForumTopicIcon) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode forumTopicIcon#cf32a193 as nil")
	}
	b.ObjStart()
	b.PutID("forumTopicIcon")
	b.Comma()
	b.FieldStart("color")
	b.PutInt32(f.Color)
	b.Comma()
	b.FieldStart("custom_emoji_id")
	b.PutLong(f.CustomEmojiID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *ForumTopicIcon) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode forumTopicIcon#cf32a193 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("forumTopicIcon"); err != nil {
				return fmt.Errorf("unable to decode forumTopicIcon#cf32a193: %w", err)
			}
		case "color":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode forumTopicIcon#cf32a193: field color: %w", err)
			}
			f.Color = value
		case "custom_emoji_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode forumTopicIcon#cf32a193: field custom_emoji_id: %w", err)
			}
			f.CustomEmojiID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetColor returns value of Color field.
func (f *ForumTopicIcon) GetColor() (value int32) {
	if f == nil {
		return
	}
	return f.Color
}

// GetCustomEmojiID returns value of CustomEmojiID field.
func (f *ForumTopicIcon) GetCustomEmojiID() (value int64) {
	if f == nil {
		return
	}
	return f.CustomEmojiID
}
