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

// ContactsContactBirthdays represents TL type `contacts.contactBirthdays#114ff30d`.
// Birthday information of our contacts.
//
// See https://core.telegram.org/constructor/contacts.contactBirthdays for reference.
type ContactsContactBirthdays struct {
	// Birthday info
	Contacts []ContactBirthday
	// User information
	Users []UserClass
}

// ContactsContactBirthdaysTypeID is TL type id of ContactsContactBirthdays.
const ContactsContactBirthdaysTypeID = 0x114ff30d

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

func (c *ContactsContactBirthdays) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Contacts == nil) {
		return false
	}
	if !(c.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ContactsContactBirthdays from given interface.
func (c *ContactsContactBirthdays) FillFrom(from interface {
	GetContacts() (value []ContactBirthday)
	GetUsers() (value []UserClass)
}) {
	c.Contacts = from.GetContacts()
	c.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*ContactsContactBirthdays) TypeName() string {
	return "contacts.contactBirthdays"
}

// TypeInfo returns info about TL type.
func (c *ContactsContactBirthdays) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.contactBirthdays",
		ID:   ContactsContactBirthdaysTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Contacts",
			SchemaName: "contacts",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ContactsContactBirthdays) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode contacts.contactBirthdays#114ff30d as nil")
	}
	b.PutID(ContactsContactBirthdaysTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ContactsContactBirthdays) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode contacts.contactBirthdays#114ff30d as nil")
	}
	b.PutVectorHeader(len(c.Contacts))
	for idx, v := range c.Contacts {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode contacts.contactBirthdays#114ff30d: field contacts element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(c.Users))
	for idx, v := range c.Users {
		if v == nil {
			return fmt.Errorf("unable to encode contacts.contactBirthdays#114ff30d: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode contacts.contactBirthdays#114ff30d: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ContactsContactBirthdays) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode contacts.contactBirthdays#114ff30d to nil")
	}
	if err := b.ConsumeID(ContactsContactBirthdaysTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.contactBirthdays#114ff30d: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ContactsContactBirthdays) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode contacts.contactBirthdays#114ff30d to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.contactBirthdays#114ff30d: field contacts: %w", err)
		}

		if headerLen > 0 {
			c.Contacts = make([]ContactBirthday, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ContactBirthday
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode contacts.contactBirthdays#114ff30d: field contacts: %w", err)
			}
			c.Contacts = append(c.Contacts, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.contactBirthdays#114ff30d: field users: %w", err)
		}

		if headerLen > 0 {
			c.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 contacts.contactBirthdays#114ff30d: field users: %w", err)
			}
			c.Users = append(c.Users, value)
		}
	}
	return nil
}

// GetContacts returns value of Contacts field.
func (c *ContactsContactBirthdays) GetContacts() (value []ContactBirthday) {
	if c == nil {
		return
	}
	return c.Contacts
}

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

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