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

// SavedMessagesTag represents TL type `savedMessagesTag#6a67bc61`.
type SavedMessagesTag struct {
	// The tag
	Tag ReactionTypeClass
	// Label of the tag; 0-12 characters. Always empty if the tag is returned for a Saved
	// Messages topic
	Label string
	// Number of times the tag was used; may be 0 if the tag has non-empty label
	Count int32
}

// SavedMessagesTagTypeID is TL type id of SavedMessagesTag.
const SavedMessagesTagTypeID = 0x6a67bc61

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

func (s *SavedMessagesTag) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Tag == nil) {
		return false
	}
	if !(s.Label == "") {
		return false
	}
	if !(s.Count == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SavedMessagesTag) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "savedMessagesTag",
		ID:   SavedMessagesTagTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Tag",
			SchemaName: "tag",
		},
		{
			Name:       "Label",
			SchemaName: "label",
		},
		{
			Name:       "Count",
			SchemaName: "count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SavedMessagesTag) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode savedMessagesTag#6a67bc61 as nil")
	}
	b.PutID(SavedMessagesTagTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SavedMessagesTag) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode savedMessagesTag#6a67bc61 as nil")
	}
	if s.Tag == nil {
		return fmt.Errorf("unable to encode savedMessagesTag#6a67bc61: field tag is nil")
	}
	if err := s.Tag.Encode(b); err != nil {
		return fmt.Errorf("unable to encode savedMessagesTag#6a67bc61: field tag: %w", err)
	}
	b.PutString(s.Label)
	b.PutInt32(s.Count)
	return nil
}

// Decode implements bin.Decoder.
func (s *SavedMessagesTag) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode savedMessagesTag#6a67bc61 to nil")
	}
	if err := b.ConsumeID(SavedMessagesTagTypeID); err != nil {
		return fmt.Errorf("unable to decode savedMessagesTag#6a67bc61: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SavedMessagesTag) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode savedMessagesTag#6a67bc61 to nil")
	}
	{
		value, err := DecodeReactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode savedMessagesTag#6a67bc61: field tag: %w", err)
		}
		s.Tag = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode savedMessagesTag#6a67bc61: field label: %w", err)
		}
		s.Label = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode savedMessagesTag#6a67bc61: field count: %w", err)
		}
		s.Count = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SavedMessagesTag) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode savedMessagesTag#6a67bc61 as nil")
	}
	b.ObjStart()
	b.PutID("savedMessagesTag")
	b.Comma()
	b.FieldStart("tag")
	if s.Tag == nil {
		return fmt.Errorf("unable to encode savedMessagesTag#6a67bc61: field tag is nil")
	}
	if err := s.Tag.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode savedMessagesTag#6a67bc61: field tag: %w", err)
	}
	b.Comma()
	b.FieldStart("label")
	b.PutString(s.Label)
	b.Comma()
	b.FieldStart("count")
	b.PutInt32(s.Count)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SavedMessagesTag) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode savedMessagesTag#6a67bc61 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("savedMessagesTag"); err != nil {
				return fmt.Errorf("unable to decode savedMessagesTag#6a67bc61: %w", err)
			}
		case "tag":
			value, err := DecodeTDLibJSONReactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode savedMessagesTag#6a67bc61: field tag: %w", err)
			}
			s.Tag = value
		case "label":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode savedMessagesTag#6a67bc61: field label: %w", err)
			}
			s.Label = value
		case "count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode savedMessagesTag#6a67bc61: field count: %w", err)
			}
			s.Count = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTag returns value of Tag field.
func (s *SavedMessagesTag) GetTag() (value ReactionTypeClass) {
	if s == nil {
		return
	}
	return s.Tag
}

// GetLabel returns value of Label field.
func (s *SavedMessagesTag) GetLabel() (value string) {
	if s == nil {
		return
	}
	return s.Label
}

// GetCount returns value of Count field.
func (s *SavedMessagesTag) GetCount() (value int32) {
	if s == nil {
		return
	}
	return s.Count
}
