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

// ForumTopics represents TL type `forumTopics#2d99b4ae`.
type ForumTopics struct {
	// Approximate total number of forum topics found
	TotalCount int32
	// List of forum topics
	Topics []ForumTopic
	// Offset date for the next getForumTopics request
	NextOffsetDate int32
	// Offset message identifier for the next getForumTopics request
	NextOffsetMessageID int64
	// Offset message thread identifier for the next getForumTopics request
	NextOffsetMessageThreadID int64
}

// ForumTopicsTypeID is TL type id of ForumTopics.
const ForumTopicsTypeID = 0x2d99b4ae

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

func (f *ForumTopics) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.TotalCount == 0) {
		return false
	}
	if !(f.Topics == nil) {
		return false
	}
	if !(f.NextOffsetDate == 0) {
		return false
	}
	if !(f.NextOffsetMessageID == 0) {
		return false
	}
	if !(f.NextOffsetMessageThreadID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *ForumTopics) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "forumTopics",
		ID:   ForumTopicsTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Topics",
			SchemaName: "topics",
		},
		{
			Name:       "NextOffsetDate",
			SchemaName: "next_offset_date",
		},
		{
			Name:       "NextOffsetMessageID",
			SchemaName: "next_offset_message_id",
		},
		{
			Name:       "NextOffsetMessageThreadID",
			SchemaName: "next_offset_message_thread_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *ForumTopics) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode forumTopics#2d99b4ae as nil")
	}
	b.PutID(ForumTopicsTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *ForumTopics) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode forumTopics#2d99b4ae as nil")
	}
	b.PutInt32(f.TotalCount)
	b.PutInt(len(f.Topics))
	for idx, v := range f.Topics {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare forumTopics#2d99b4ae: field topics element with index %d: %w", idx, err)
		}
	}
	b.PutInt32(f.NextOffsetDate)
	b.PutInt53(f.NextOffsetMessageID)
	b.PutInt53(f.NextOffsetMessageThreadID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (f *ForumTopics) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode forumTopics#2d99b4ae to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field total_count: %w", err)
		}
		f.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field topics: %w", err)
		}

		if headerLen > 0 {
			f.Topics = make([]ForumTopic, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ForumTopic
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare forumTopics#2d99b4ae: field topics: %w", err)
			}
			f.Topics = append(f.Topics, value)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field next_offset_date: %w", err)
		}
		f.NextOffsetDate = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field next_offset_message_id: %w", err)
		}
		f.NextOffsetMessageID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field next_offset_message_thread_id: %w", err)
		}
		f.NextOffsetMessageThreadID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *ForumTopics) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode forumTopics#2d99b4ae as nil")
	}
	b.ObjStart()
	b.PutID("forumTopics")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(f.TotalCount)
	b.Comma()
	b.FieldStart("topics")
	b.ArrStart()
	for idx, v := range f.Topics {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode forumTopics#2d99b4ae: field topics element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_offset_date")
	b.PutInt32(f.NextOffsetDate)
	b.Comma()
	b.FieldStart("next_offset_message_id")
	b.PutInt53(f.NextOffsetMessageID)
	b.Comma()
	b.FieldStart("next_offset_message_thread_id")
	b.PutInt53(f.NextOffsetMessageThreadID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("forumTopics"); err != nil {
				return fmt.Errorf("unable to decode forumTopics#2d99b4ae: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field total_count: %w", err)
			}
			f.TotalCount = value
		case "topics":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value ForumTopic
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field topics: %w", err)
				}
				f.Topics = append(f.Topics, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field topics: %w", err)
			}
		case "next_offset_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field next_offset_date: %w", err)
			}
			f.NextOffsetDate = value
		case "next_offset_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field next_offset_message_id: %w", err)
			}
			f.NextOffsetMessageID = value
		case "next_offset_message_thread_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode forumTopics#2d99b4ae: field next_offset_message_thread_id: %w", err)
			}
			f.NextOffsetMessageThreadID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (f *ForumTopics) GetTotalCount() (value int32) {
	if f == nil {
		return
	}
	return f.TotalCount
}

// GetTopics returns value of Topics field.
func (f *ForumTopics) GetTopics() (value []ForumTopic) {
	if f == nil {
		return
	}
	return f.Topics
}

// GetNextOffsetDate returns value of NextOffsetDate field.
func (f *ForumTopics) GetNextOffsetDate() (value int32) {
	if f == nil {
		return
	}
	return f.NextOffsetDate
}

// GetNextOffsetMessageID returns value of NextOffsetMessageID field.
func (f *ForumTopics) GetNextOffsetMessageID() (value int64) {
	if f == nil {
		return
	}
	return f.NextOffsetMessageID
}

// GetNextOffsetMessageThreadID returns value of NextOffsetMessageThreadID field.
func (f *ForumTopics) GetNextOffsetMessageThreadID() (value int64) {
	if f == nil {
		return
	}
	return f.NextOffsetMessageThreadID
}
