// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesSearchResultsCalendar represents TL type `messages.searchResultsCalendar#147ee23c`.
// Information about found messages sent on a specific day
//
// See https://core.telegram.org/constructor/messages.searchResultsCalendar for reference.
type MessagesSearchResultsCalendar struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, indicates that the results may be inexact
	Inexact bool
	// Total number of results matching query
	Count int
	// Starting timestamp of attached messages
	MinDate int
	// Ending timestamp of attached messages
	MinMsgID int
	// Indicates the absolute position of messages[0] within the total result set with count
	// count. This is useful, for example, if we need to display a progress/total counter
	// (like photo 134 of 200, for all media in a chat, we could simply use photo
	// ${offset_id_offset} of ${count}.
	//
	// Use SetOffsetIDOffset and GetOffsetIDOffset helpers.
	OffsetIDOffset int
	// Used to split the messages by days: multiple SearchResultsCalendarPeriod¹
	// constructors are returned, each containing information about the first, last and total
	// number of messages matching the filter that were sent on a specific day.  This
	// information can be easily used to split the returned messages by day.
	//
	// Links:
	//  1) https://core.telegram.org/type/SearchResultsCalendarPeriod
	Periods []SearchResultsCalendarPeriod
	// Messages
	Messages []MessageClass
	// Mentioned chats
	Chats []ChatClass
	// Mentioned users
	Users []UserClass
}

// MessagesSearchResultsCalendarTypeID is TL type id of MessagesSearchResultsCalendar.
const MessagesSearchResultsCalendarTypeID = 0x147ee23c

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

func (s *MessagesSearchResultsCalendar) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Inexact == false) {
		return false
	}
	if !(s.Count == 0) {
		return false
	}
	if !(s.MinDate == 0) {
		return false
	}
	if !(s.MinMsgID == 0) {
		return false
	}
	if !(s.OffsetIDOffset == 0) {
		return false
	}
	if !(s.Periods == nil) {
		return false
	}
	if !(s.Messages == nil) {
		return false
	}
	if !(s.Chats == nil) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSearchResultsCalendar from given interface.
func (s *MessagesSearchResultsCalendar) FillFrom(from interface {
	GetInexact() (value bool)
	GetCount() (value int)
	GetMinDate() (value int)
	GetMinMsgID() (value int)
	GetOffsetIDOffset() (value int, ok bool)
	GetPeriods() (value []SearchResultsCalendarPeriod)
	GetMessages() (value []MessageClass)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	s.Inexact = from.GetInexact()
	s.Count = from.GetCount()
	s.MinDate = from.GetMinDate()
	s.MinMsgID = from.GetMinMsgID()
	if val, ok := from.GetOffsetIDOffset(); ok {
		s.OffsetIDOffset = val
	}

	s.Periods = from.GetPeriods()
	s.Messages = from.GetMessages()
	s.Chats = from.GetChats()
	s.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesSearchResultsCalendar) TypeName() string {
	return "messages.searchResultsCalendar"
}

// TypeInfo returns info about TL type.
func (s *MessagesSearchResultsCalendar) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.searchResultsCalendar",
		ID:   MessagesSearchResultsCalendarTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Inexact",
			SchemaName: "inexact",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Count",
			SchemaName: "count",
		},
		{
			Name:       "MinDate",
			SchemaName: "min_date",
		},
		{
			Name:       "MinMsgID",
			SchemaName: "min_msg_id",
		},
		{
			Name:       "OffsetIDOffset",
			SchemaName: "offset_id_offset",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "Periods",
			SchemaName: "periods",
		},
		{
			Name:       "Messages",
			SchemaName: "messages",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *MessagesSearchResultsCalendar) SetFlags() {
	if !(s.Inexact == false) {
		s.Flags.Set(0)
	}
	if !(s.OffsetIDOffset == 0) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *MessagesSearchResultsCalendar) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.searchResultsCalendar#147ee23c as nil")
	}
	b.PutID(MessagesSearchResultsCalendarTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSearchResultsCalendar) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.searchResultsCalendar#147ee23c as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.searchResultsCalendar#147ee23c: field flags: %w", err)
	}
	b.PutInt(s.Count)
	b.PutInt(s.MinDate)
	b.PutInt(s.MinMsgID)
	if s.Flags.Has(1) {
		b.PutInt(s.OffsetIDOffset)
	}
	b.PutVectorHeader(len(s.Periods))
	for idx, v := range s.Periods {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.searchResultsCalendar#147ee23c: field periods element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Messages))
	for idx, v := range s.Messages {
		if v == nil {
			return fmt.Errorf("unable to encode messages.searchResultsCalendar#147ee23c: field messages element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.searchResultsCalendar#147ee23c: field messages element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Chats))
	for idx, v := range s.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode messages.searchResultsCalendar#147ee23c: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.searchResultsCalendar#147ee23c: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(s.Users))
	for idx, v := range s.Users {
		if v == nil {
			return fmt.Errorf("unable to encode messages.searchResultsCalendar#147ee23c: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.searchResultsCalendar#147ee23c: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSearchResultsCalendar) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.searchResultsCalendar#147ee23c to nil")
	}
	if err := b.ConsumeID(MessagesSearchResultsCalendarTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSearchResultsCalendar) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.searchResultsCalendar#147ee23c to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field flags: %w", err)
		}
	}
	s.Inexact = s.Flags.Has(0)
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field count: %w", err)
		}
		s.Count = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field min_date: %w", err)
		}
		s.MinDate = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field min_msg_id: %w", err)
		}
		s.MinMsgID = value
	}
	if s.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field offset_id_offset: %w", err)
		}
		s.OffsetIDOffset = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field periods: %w", err)
		}

		if headerLen > 0 {
			s.Periods = make([]SearchResultsCalendarPeriod, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value SearchResultsCalendarPeriod
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field periods: %w", err)
			}
			s.Periods = append(s.Periods, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field messages: %w", err)
		}

		if headerLen > 0 {
			s.Messages = make([]MessageClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeMessage(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field messages: %w", err)
			}
			s.Messages = append(s.Messages, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field chats: %w", err)
		}

		if headerLen > 0 {
			s.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field chats: %w", err)
			}
			s.Chats = append(s.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field users: %w", err)
		}

		if headerLen > 0 {
			s.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.searchResultsCalendar#147ee23c: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	return nil
}

// SetInexact sets value of Inexact conditional field.
func (s *MessagesSearchResultsCalendar) SetInexact(value bool) {
	if value {
		s.Flags.Set(0)
		s.Inexact = true
	} else {
		s.Flags.Unset(0)
		s.Inexact = false
	}
}

// GetInexact returns value of Inexact conditional field.
func (s *MessagesSearchResultsCalendar) GetInexact() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

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

// GetMinDate returns value of MinDate field.
func (s *MessagesSearchResultsCalendar) GetMinDate() (value int) {
	if s == nil {
		return
	}
	return s.MinDate
}

// GetMinMsgID returns value of MinMsgID field.
func (s *MessagesSearchResultsCalendar) GetMinMsgID() (value int) {
	if s == nil {
		return
	}
	return s.MinMsgID
}

// SetOffsetIDOffset sets value of OffsetIDOffset conditional field.
func (s *MessagesSearchResultsCalendar) SetOffsetIDOffset(value int) {
	s.Flags.Set(1)
	s.OffsetIDOffset = value
}

// GetOffsetIDOffset returns value of OffsetIDOffset conditional field and
// boolean which is true if field was set.
func (s *MessagesSearchResultsCalendar) GetOffsetIDOffset() (value int, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.OffsetIDOffset, true
}

// GetPeriods returns value of Periods field.
func (s *MessagesSearchResultsCalendar) GetPeriods() (value []SearchResultsCalendarPeriod) {
	if s == nil {
		return
	}
	return s.Periods
}

// GetMessages returns value of Messages field.
func (s *MessagesSearchResultsCalendar) GetMessages() (value []MessageClass) {
	if s == nil {
		return
	}
	return s.Messages
}

// GetChats returns value of Chats field.
func (s *MessagesSearchResultsCalendar) GetChats() (value []ChatClass) {
	if s == nil {
		return
	}
	return s.Chats
}

// GetUsers returns value of Users field.
func (s *MessagesSearchResultsCalendar) GetUsers() (value []UserClass) {
	if s == nil {
		return
	}
	return s.Users
}

// MapMessages returns field Messages wrapped in MessageClassArray helper.
func (s *MessagesSearchResultsCalendar) MapMessages() (value MessageClassArray) {
	return MessageClassArray(s.Messages)
}

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (s *MessagesSearchResultsCalendar) MapChats() (value ChatClassArray) {
	return ChatClassArray(s.Chats)
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (s *MessagesSearchResultsCalendar) MapUsers() (value UserClassArray) {
	return UserClassArray(s.Users)
}
