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

// DefaultHistoryTTL represents TL type `defaultHistoryTTL#43b46b20`.
// Contains info about the default value of the Time-To-Live setting, applied to all new
// chats.
//
// See https://core.telegram.org/constructor/defaultHistoryTTL for reference.
type DefaultHistoryTTL struct {
	// Time-To-Live setting applied to all new chats.
	Period int
}

// DefaultHistoryTTLTypeID is TL type id of DefaultHistoryTTL.
const DefaultHistoryTTLTypeID = 0x43b46b20

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

func (d *DefaultHistoryTTL) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Period == 0) {
		return false
	}

	return true
}

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

// FillFrom fills DefaultHistoryTTL from given interface.
func (d *DefaultHistoryTTL) FillFrom(from interface {
	GetPeriod() (value int)
}) {
	d.Period = from.GetPeriod()
}

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

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

// TypeInfo returns info about TL type.
func (d *DefaultHistoryTTL) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "defaultHistoryTTL",
		ID:   DefaultHistoryTTLTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Period",
			SchemaName: "period",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *DefaultHistoryTTL) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode defaultHistoryTTL#43b46b20 as nil")
	}
	b.PutID(DefaultHistoryTTLTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DefaultHistoryTTL) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode defaultHistoryTTL#43b46b20 as nil")
	}
	b.PutInt(d.Period)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (d *DefaultHistoryTTL) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode defaultHistoryTTL#43b46b20 to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode defaultHistoryTTL#43b46b20: field period: %w", err)
		}
		d.Period = value
	}
	return nil
}

// GetPeriod returns value of Period field.
func (d *DefaultHistoryTTL) GetPeriod() (value int) {
	if d == nil {
		return
	}
	return d.Period
}
