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

// Date represents TL type `date#ef6eb6a0`.
type Date struct {
	// Day of the month; 1-31
	Day int32
	// Month; 1-12
	Month int32
	// Year; 1-9999
	Year int32
}

// DateTypeID is TL type id of Date.
const DateTypeID = 0xef6eb6a0

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (d *Date) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "date",
		ID:   DateTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Day",
			SchemaName: "day",
		},
		{
			Name:       "Month",
			SchemaName: "month",
		},
		{
			Name:       "Year",
			SchemaName: "year",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *Date) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode date#ef6eb6a0 as nil")
	}
	b.PutID(DateTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *Date) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode date#ef6eb6a0 as nil")
	}
	b.PutInt32(d.Day)
	b.PutInt32(d.Month)
	b.PutInt32(d.Year)
	return nil
}

// Decode implements bin.Decoder.
func (d *Date) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode date#ef6eb6a0 to nil")
	}
	if err := b.ConsumeID(DateTypeID); err != nil {
		return fmt.Errorf("unable to decode date#ef6eb6a0: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *Date) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode date#ef6eb6a0 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode date#ef6eb6a0: field day: %w", err)
		}
		d.Day = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode date#ef6eb6a0: field month: %w", err)
		}
		d.Month = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode date#ef6eb6a0: field year: %w", err)
		}
		d.Year = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *Date) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode date#ef6eb6a0 as nil")
	}
	b.ObjStart()
	b.PutID("date")
	b.Comma()
	b.FieldStart("day")
	b.PutInt32(d.Day)
	b.Comma()
	b.FieldStart("month")
	b.PutInt32(d.Month)
	b.Comma()
	b.FieldStart("year")
	b.PutInt32(d.Year)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *Date) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode date#ef6eb6a0 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("date"); err != nil {
				return fmt.Errorf("unable to decode date#ef6eb6a0: %w", err)
			}
		case "day":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode date#ef6eb6a0: field day: %w", err)
			}
			d.Day = value
		case "month":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode date#ef6eb6a0: field month: %w", err)
			}
			d.Month = value
		case "year":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode date#ef6eb6a0: field year: %w", err)
			}
			d.Year = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetDay returns value of Day field.
func (d *Date) GetDay() (value int32) {
	if d == nil {
		return
	}
	return d.Day
}

// GetMonth returns value of Month field.
func (d *Date) GetMonth() (value int32) {
	if d == nil {
		return
	}
	return d.Month
}

// GetYear returns value of Year field.
func (d *Date) GetYear() (value int32) {
	if d == nil {
		return
	}
	return d.Year
}
