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

// Birthday represents TL type `birthday#6c8e1e06`.
// Birthday¹ information for a user.
// Also used to invite users to gift Telegram Premium subscriptions »¹ to other users
// with birthdays within a +1/-1 day time range, related to the current day.
//
// Links:
//  1. https://core.telegram.org/api/profile#birthday
//  2. https://core.telegram.org/api/premium#gifting-telegram-premium
//
// See https://core.telegram.org/constructor/birthday for reference.
type Birthday struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Birth day
	Day int
	// Birth month
	Month int
	// (Optional) birth year.
	//
	// Use SetYear and GetYear helpers.
	Year int
}

// BirthdayTypeID is TL type id of Birthday.
const BirthdayTypeID = 0x6c8e1e06

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

func (b *Birthday) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.Day == 0) {
		return false
	}
	if !(b.Month == 0) {
		return false
	}
	if !(b.Year == 0) {
		return false
	}

	return true
}

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

// FillFrom fills Birthday from given interface.
func (b *Birthday) FillFrom(from interface {
	GetDay() (value int)
	GetMonth() (value int)
	GetYear() (value int, ok bool)
}) {
	b.Day = from.GetDay()
	b.Month = from.GetMonth()
	if val, ok := from.GetYear(); ok {
		b.Year = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (b *Birthday) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "birthday",
		ID:   BirthdayTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Day",
			SchemaName: "day",
		},
		{
			Name:       "Month",
			SchemaName: "month",
		},
		{
			Name:       "Year",
			SchemaName: "year",
			Null:       !b.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *Birthday) SetFlags() {
	if !(b.Year == 0) {
		b.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (b *Birthday) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode birthday#6c8e1e06 as nil")
	}
	buf.PutID(BirthdayTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *Birthday) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode birthday#6c8e1e06 as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode birthday#6c8e1e06: field flags: %w", err)
	}
	buf.PutInt(b.Day)
	buf.PutInt(b.Month)
	if b.Flags.Has(0) {
		buf.PutInt(b.Year)
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *Birthday) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode birthday#6c8e1e06 to nil")
	}
	if err := buf.ConsumeID(BirthdayTypeID); err != nil {
		return fmt.Errorf("unable to decode birthday#6c8e1e06: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *Birthday) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode birthday#6c8e1e06 to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode birthday#6c8e1e06: field flags: %w", err)
		}
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode birthday#6c8e1e06: field day: %w", err)
		}
		b.Day = value
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode birthday#6c8e1e06: field month: %w", err)
		}
		b.Month = value
	}
	if b.Flags.Has(0) {
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode birthday#6c8e1e06: field year: %w", err)
		}
		b.Year = value
	}
	return nil
}

// GetDay returns value of Day field.
func (b *Birthday) GetDay() (value int) {
	if b == nil {
		return
	}
	return b.Day
}

// GetMonth returns value of Month field.
func (b *Birthday) GetMonth() (value int) {
	if b == nil {
		return
	}
	return b.Month
}

// SetYear sets value of Year conditional field.
func (b *Birthday) SetYear(value int) {
	b.Flags.Set(0)
	b.Year = value
}

// GetYear returns value of Year conditional field and
// boolean which is true if field was set.
func (b *Birthday) GetYear() (value int, ok bool) {
	if b == nil {
		return
	}
	if !b.Flags.Has(0) {
		return value, false
	}
	return b.Year, true
}
