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

// MessageCalendar represents TL type `messageCalendar#2582bbd7`.
type MessageCalendar struct {
	// Total number of found messages
	TotalCount int32
	// Information about messages sent
	Days []MessageCalendarDay
}

// MessageCalendarTypeID is TL type id of MessageCalendar.
const MessageCalendarTypeID = 0x2582bbd7

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

func (m *MessageCalendar) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.TotalCount == 0) {
		return false
	}
	if !(m.Days == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageCalendar) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageCalendar",
		ID:   MessageCalendarTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Days",
			SchemaName: "days",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageCalendar) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageCalendar#2582bbd7 as nil")
	}
	b.PutID(MessageCalendarTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageCalendar) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageCalendar#2582bbd7 as nil")
	}
	b.PutInt32(m.TotalCount)
	b.PutInt(len(m.Days))
	for idx, v := range m.Days {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare messageCalendar#2582bbd7: field days element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageCalendar) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageCalendar#2582bbd7 to nil")
	}
	if err := b.ConsumeID(MessageCalendarTypeID); err != nil {
		return fmt.Errorf("unable to decode messageCalendar#2582bbd7: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageCalendar) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageCalendar#2582bbd7 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode messageCalendar#2582bbd7: field total_count: %w", err)
		}
		m.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messageCalendar#2582bbd7: field days: %w", err)
		}

		if headerLen > 0 {
			m.Days = make([]MessageCalendarDay, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value MessageCalendarDay
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare messageCalendar#2582bbd7: field days: %w", err)
			}
			m.Days = append(m.Days, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageCalendar) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageCalendar#2582bbd7 as nil")
	}
	b.ObjStart()
	b.PutID("messageCalendar")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(m.TotalCount)
	b.Comma()
	b.FieldStart("days")
	b.ArrStart()
	for idx, v := range m.Days {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode messageCalendar#2582bbd7: field days element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageCalendar) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageCalendar#2582bbd7 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageCalendar"); err != nil {
				return fmt.Errorf("unable to decode messageCalendar#2582bbd7: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode messageCalendar#2582bbd7: field total_count: %w", err)
			}
			m.TotalCount = value
		case "days":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value MessageCalendarDay
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode messageCalendar#2582bbd7: field days: %w", err)
				}
				m.Days = append(m.Days, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode messageCalendar#2582bbd7: field days: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetDays returns value of Days field.
func (m *MessageCalendar) GetDays() (value []MessageCalendarDay) {
	if m == nil {
		return
	}
	return m.Days
}
