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

// SavedMessagesTopic represents TL type `savedMessagesTopic#d2a8e1a4`.
type SavedMessagesTopic struct {
	// Unique topic identifier
	ID int64
	// Type of the topic
	Type SavedMessagesTopicTypeClass
	// True, if the topic is pinned
	IsPinned bool
	// A parameter used to determine order of the topic in the topic list. Topics must be
	// sorted by the order in descending order
	Order int64
	// Last message in the topic; may be null if none or unknown
	LastMessage Message
	// A draft of a message in the topic; may be null if none
	DraftMessage DraftMessage
}

// SavedMessagesTopicTypeID is TL type id of SavedMessagesTopic.
const SavedMessagesTopicTypeID = 0xd2a8e1a4

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

func (s *SavedMessagesTopic) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ID == 0) {
		return false
	}
	if !(s.Type == nil) {
		return false
	}
	if !(s.IsPinned == false) {
		return false
	}
	if !(s.Order == 0) {
		return false
	}
	if !(s.LastMessage.Zero()) {
		return false
	}
	if !(s.DraftMessage.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SavedMessagesTopic) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "savedMessagesTopic",
		ID:   SavedMessagesTopicTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "IsPinned",
			SchemaName: "is_pinned",
		},
		{
			Name:       "Order",
			SchemaName: "order",
		},
		{
			Name:       "LastMessage",
			SchemaName: "last_message",
		},
		{
			Name:       "DraftMessage",
			SchemaName: "draft_message",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SavedMessagesTopic) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode savedMessagesTopic#d2a8e1a4 as nil")
	}
	b.PutID(SavedMessagesTopicTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SavedMessagesTopic) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode savedMessagesTopic#d2a8e1a4 as nil")
	}
	b.PutInt53(s.ID)
	if s.Type == nil {
		return fmt.Errorf("unable to encode savedMessagesTopic#d2a8e1a4: field type is nil")
	}
	if err := s.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode savedMessagesTopic#d2a8e1a4: field type: %w", err)
	}
	b.PutBool(s.IsPinned)
	b.PutLong(s.Order)
	if err := s.LastMessage.Encode(b); err != nil {
		return fmt.Errorf("unable to encode savedMessagesTopic#d2a8e1a4: field last_message: %w", err)
	}
	if err := s.DraftMessage.Encode(b); err != nil {
		return fmt.Errorf("unable to encode savedMessagesTopic#d2a8e1a4: field draft_message: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SavedMessagesTopic) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode savedMessagesTopic#d2a8e1a4 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field id: %w", err)
		}
		s.ID = value
	}
	{
		value, err := DecodeSavedMessagesTopicType(b)
		if err != nil {
			return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field type: %w", err)
		}
		s.Type = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field is_pinned: %w", err)
		}
		s.IsPinned = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field order: %w", err)
		}
		s.Order = value
	}
	{
		if err := s.LastMessage.Decode(b); err != nil {
			return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field last_message: %w", err)
		}
	}
	{
		if err := s.DraftMessage.Decode(b); err != nil {
			return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field draft_message: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SavedMessagesTopic) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode savedMessagesTopic#d2a8e1a4 as nil")
	}
	b.ObjStart()
	b.PutID("savedMessagesTopic")
	b.Comma()
	b.FieldStart("id")
	b.PutInt53(s.ID)
	b.Comma()
	b.FieldStart("type")
	if s.Type == nil {
		return fmt.Errorf("unable to encode savedMessagesTopic#d2a8e1a4: field type is nil")
	}
	if err := s.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode savedMessagesTopic#d2a8e1a4: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("is_pinned")
	b.PutBool(s.IsPinned)
	b.Comma()
	b.FieldStart("order")
	b.PutLong(s.Order)
	b.Comma()
	b.FieldStart("last_message")
	if err := s.LastMessage.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode savedMessagesTopic#d2a8e1a4: field last_message: %w", err)
	}
	b.Comma()
	b.FieldStart("draft_message")
	if err := s.DraftMessage.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode savedMessagesTopic#d2a8e1a4: field draft_message: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("savedMessagesTopic"); err != nil {
				return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: %w", err)
			}
		case "id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field id: %w", err)
			}
			s.ID = value
		case "type":
			value, err := DecodeTDLibJSONSavedMessagesTopicType(b)
			if err != nil {
				return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field type: %w", err)
			}
			s.Type = value
		case "is_pinned":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field is_pinned: %w", err)
			}
			s.IsPinned = value
		case "order":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field order: %w", err)
			}
			s.Order = value
		case "last_message":
			if err := s.LastMessage.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field last_message: %w", err)
			}
		case "draft_message":
			if err := s.DraftMessage.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode savedMessagesTopic#d2a8e1a4: field draft_message: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (s *SavedMessagesTopic) GetID() (value int64) {
	if s == nil {
		return
	}
	return s.ID
}

// GetType returns value of Type field.
func (s *SavedMessagesTopic) GetType() (value SavedMessagesTopicTypeClass) {
	if s == nil {
		return
	}
	return s.Type
}

// GetIsPinned returns value of IsPinned field.
func (s *SavedMessagesTopic) GetIsPinned() (value bool) {
	if s == nil {
		return
	}
	return s.IsPinned
}

// GetOrder returns value of Order field.
func (s *SavedMessagesTopic) GetOrder() (value int64) {
	if s == nil {
		return
	}
	return s.Order
}

// GetLastMessage returns value of LastMessage field.
func (s *SavedMessagesTopic) GetLastMessage() (value Message) {
	if s == nil {
		return
	}
	return s.LastMessage
}

// GetDraftMessage returns value of DraftMessage field.
func (s *SavedMessagesTopic) GetDraftMessage() (value DraftMessage) {
	if s == nil {
		return
	}
	return s.DraftMessage
}
