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

// HelpTermsOfServiceUpdateEmpty represents TL type `help.termsOfServiceUpdateEmpty#e3309f7f`.
// No changes were made to telegram's terms of service
//
// See https://core.telegram.org/constructor/help.termsOfServiceUpdateEmpty for reference.
type HelpTermsOfServiceUpdateEmpty struct {
	// New TOS updates will have to be queried using help.getTermsOfServiceUpdate¹ in
	// expires seconds
	//
	// Links:
	//  1) https://core.telegram.org/method/help.getTermsOfServiceUpdate
	Expires int
}

// HelpTermsOfServiceUpdateEmptyTypeID is TL type id of HelpTermsOfServiceUpdateEmpty.
const HelpTermsOfServiceUpdateEmptyTypeID = 0xe3309f7f

// construct implements constructor of HelpTermsOfServiceUpdateClass.
func (t HelpTermsOfServiceUpdateEmpty) construct() HelpTermsOfServiceUpdateClass { return &t }

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

	_ HelpTermsOfServiceUpdateClass = &HelpTermsOfServiceUpdateEmpty{}
)

func (t *HelpTermsOfServiceUpdateEmpty) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Expires == 0) {
		return false
	}

	return true
}

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

// FillFrom fills HelpTermsOfServiceUpdateEmpty from given interface.
func (t *HelpTermsOfServiceUpdateEmpty) FillFrom(from interface {
	GetExpires() (value int)
}) {
	t.Expires = from.GetExpires()
}

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

// TypeName returns name of type in TL schema.
func (*HelpTermsOfServiceUpdateEmpty) TypeName() string {
	return "help.termsOfServiceUpdateEmpty"
}

// TypeInfo returns info about TL type.
func (t *HelpTermsOfServiceUpdateEmpty) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "help.termsOfServiceUpdateEmpty",
		ID:   HelpTermsOfServiceUpdateEmptyTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Expires",
			SchemaName: "expires",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *HelpTermsOfServiceUpdateEmpty) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode help.termsOfServiceUpdateEmpty#e3309f7f as nil")
	}
	b.PutID(HelpTermsOfServiceUpdateEmptyTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *HelpTermsOfServiceUpdateEmpty) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode help.termsOfServiceUpdateEmpty#e3309f7f as nil")
	}
	b.PutInt(t.Expires)
	return nil
}

// Decode implements bin.Decoder.
func (t *HelpTermsOfServiceUpdateEmpty) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode help.termsOfServiceUpdateEmpty#e3309f7f to nil")
	}
	if err := b.ConsumeID(HelpTermsOfServiceUpdateEmptyTypeID); err != nil {
		return fmt.Errorf("unable to decode help.termsOfServiceUpdateEmpty#e3309f7f: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *HelpTermsOfServiceUpdateEmpty) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode help.termsOfServiceUpdateEmpty#e3309f7f to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode help.termsOfServiceUpdateEmpty#e3309f7f: field expires: %w", err)
		}
		t.Expires = value
	}
	return nil
}

// GetExpires returns value of Expires field.
func (t *HelpTermsOfServiceUpdateEmpty) GetExpires() (value int) {
	if t == nil {
		return
	}
	return t.Expires
}

// HelpTermsOfServiceUpdate represents TL type `help.termsOfServiceUpdate#28ecf961`.
// Info about an update of telegram's terms of service. If the terms of service are
// declined, then the account.deleteAccount¹ method should be called with the reason
// "Decline ToS update"
//
// Links:
//  1. https://core.telegram.org/method/account.deleteAccount
//
// See https://core.telegram.org/constructor/help.termsOfServiceUpdate for reference.
type HelpTermsOfServiceUpdate struct {
	// New TOS updates will have to be queried using help.getTermsOfServiceUpdate¹ in
	// expires seconds
	//
	// Links:
	//  1) https://core.telegram.org/method/help.getTermsOfServiceUpdate
	Expires int
	// New terms of service
	TermsOfService HelpTermsOfService
}

// HelpTermsOfServiceUpdateTypeID is TL type id of HelpTermsOfServiceUpdate.
const HelpTermsOfServiceUpdateTypeID = 0x28ecf961

// construct implements constructor of HelpTermsOfServiceUpdateClass.
func (t HelpTermsOfServiceUpdate) construct() HelpTermsOfServiceUpdateClass { return &t }

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

	_ HelpTermsOfServiceUpdateClass = &HelpTermsOfServiceUpdate{}
)

func (t *HelpTermsOfServiceUpdate) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Expires == 0) {
		return false
	}
	if !(t.TermsOfService.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills HelpTermsOfServiceUpdate from given interface.
func (t *HelpTermsOfServiceUpdate) FillFrom(from interface {
	GetExpires() (value int)
	GetTermsOfService() (value HelpTermsOfService)
}) {
	t.Expires = from.GetExpires()
	t.TermsOfService = from.GetTermsOfService()
}

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

// TypeName returns name of type in TL schema.
func (*HelpTermsOfServiceUpdate) TypeName() string {
	return "help.termsOfServiceUpdate"
}

// TypeInfo returns info about TL type.
func (t *HelpTermsOfServiceUpdate) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "help.termsOfServiceUpdate",
		ID:   HelpTermsOfServiceUpdateTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Expires",
			SchemaName: "expires",
		},
		{
			Name:       "TermsOfService",
			SchemaName: "terms_of_service",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *HelpTermsOfServiceUpdate) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode help.termsOfServiceUpdate#28ecf961 as nil")
	}
	b.PutID(HelpTermsOfServiceUpdateTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *HelpTermsOfServiceUpdate) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode help.termsOfServiceUpdate#28ecf961 as nil")
	}
	b.PutInt(t.Expires)
	if err := t.TermsOfService.Encode(b); err != nil {
		return fmt.Errorf("unable to encode help.termsOfServiceUpdate#28ecf961: field terms_of_service: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *HelpTermsOfServiceUpdate) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode help.termsOfServiceUpdate#28ecf961 to nil")
	}
	if err := b.ConsumeID(HelpTermsOfServiceUpdateTypeID); err != nil {
		return fmt.Errorf("unable to decode help.termsOfServiceUpdate#28ecf961: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *HelpTermsOfServiceUpdate) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode help.termsOfServiceUpdate#28ecf961 to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode help.termsOfServiceUpdate#28ecf961: field expires: %w", err)
		}
		t.Expires = value
	}
	{
		if err := t.TermsOfService.Decode(b); err != nil {
			return fmt.Errorf("unable to decode help.termsOfServiceUpdate#28ecf961: field terms_of_service: %w", err)
		}
	}
	return nil
}

// GetExpires returns value of Expires field.
func (t *HelpTermsOfServiceUpdate) GetExpires() (value int) {
	if t == nil {
		return
	}
	return t.Expires
}

// GetTermsOfService returns value of TermsOfService field.
func (t *HelpTermsOfServiceUpdate) GetTermsOfService() (value HelpTermsOfService) {
	if t == nil {
		return
	}
	return t.TermsOfService
}

// HelpTermsOfServiceUpdateClassName is schema name of HelpTermsOfServiceUpdateClass.
const HelpTermsOfServiceUpdateClassName = "help.TermsOfServiceUpdate"

// HelpTermsOfServiceUpdateClass represents help.TermsOfServiceUpdate generic type.
//
// See https://core.telegram.org/type/help.TermsOfServiceUpdate for reference.
//
// Example:
//
//	g, err := tg.DecodeHelpTermsOfServiceUpdate(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.HelpTermsOfServiceUpdateEmpty: // help.termsOfServiceUpdateEmpty#e3309f7f
//	case *tg.HelpTermsOfServiceUpdate: // help.termsOfServiceUpdate#28ecf961
//	default: panic(v)
//	}
type HelpTermsOfServiceUpdateClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() HelpTermsOfServiceUpdateClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// New TOS updates will have to be queried using help.getTermsOfServiceUpdate¹ in
	// expires seconds
	//
	// Links:
	//  1) https://core.telegram.org/method/help.getTermsOfServiceUpdate
	GetExpires() (value int)

	// AsNotEmpty tries to map HelpTermsOfServiceUpdateClass to HelpTermsOfServiceUpdate.
	AsNotEmpty() (*HelpTermsOfServiceUpdate, bool)
}

// AsNotEmpty tries to map HelpTermsOfServiceUpdateEmpty to HelpTermsOfServiceUpdate.
func (t *HelpTermsOfServiceUpdateEmpty) AsNotEmpty() (*HelpTermsOfServiceUpdate, bool) {
	return nil, false
}

// AsNotEmpty tries to map HelpTermsOfServiceUpdate to HelpTermsOfServiceUpdate.
func (t *HelpTermsOfServiceUpdate) AsNotEmpty() (*HelpTermsOfServiceUpdate, bool) {
	return t, true
}

// DecodeHelpTermsOfServiceUpdate implements binary de-serialization for HelpTermsOfServiceUpdateClass.
func DecodeHelpTermsOfServiceUpdate(buf *bin.Buffer) (HelpTermsOfServiceUpdateClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case HelpTermsOfServiceUpdateEmptyTypeID:
		// Decoding help.termsOfServiceUpdateEmpty#e3309f7f.
		v := HelpTermsOfServiceUpdateEmpty{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode HelpTermsOfServiceUpdateClass: %w", err)
		}
		return &v, nil
	case HelpTermsOfServiceUpdateTypeID:
		// Decoding help.termsOfServiceUpdate#28ecf961.
		v := HelpTermsOfServiceUpdate{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode HelpTermsOfServiceUpdateClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode HelpTermsOfServiceUpdateClass: %w", bin.NewUnexpectedID(id))
	}
}

// HelpTermsOfServiceUpdate boxes the HelpTermsOfServiceUpdateClass providing a helper.
type HelpTermsOfServiceUpdateBox struct {
	TermsOfServiceUpdate HelpTermsOfServiceUpdateClass
}

// Decode implements bin.Decoder for HelpTermsOfServiceUpdateBox.
func (b *HelpTermsOfServiceUpdateBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode HelpTermsOfServiceUpdateBox to nil")
	}
	v, err := DecodeHelpTermsOfServiceUpdate(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.TermsOfServiceUpdate = v
	return nil
}

// Encode implements bin.Encode for HelpTermsOfServiceUpdateBox.
func (b *HelpTermsOfServiceUpdateBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.TermsOfServiceUpdate == nil {
		return fmt.Errorf("unable to encode HelpTermsOfServiceUpdateClass as nil")
	}
	return b.TermsOfServiceUpdate.Encode(buf)
}
