// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// AuthLoggedOut represents TL type `auth.loggedOut#c3a2835f`.
// Future auth token »¹ to be used on subsequent authorizations
//
// Links:
//  1. https://core.telegram.org/api/auth#future-auth-tokens
//
// See https://core.telegram.org/constructor/auth.loggedOut for reference.
type AuthLoggedOut struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Future auth token »¹ to be used on subsequent authorizations
	//
	// Links:
	//  1) https://core.telegram.org/api/auth#future-auth-tokens
	//
	// Use SetFutureAuthToken and GetFutureAuthToken helpers.
	FutureAuthToken []byte
}

// AuthLoggedOutTypeID is TL type id of AuthLoggedOut.
const AuthLoggedOutTypeID = 0xc3a2835f

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

func (l *AuthLoggedOut) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.Flags.Zero()) {
		return false
	}
	if !(l.FutureAuthToken == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AuthLoggedOut from given interface.
func (l *AuthLoggedOut) FillFrom(from interface {
	GetFutureAuthToken() (value []byte, ok bool)
}) {
	if val, ok := from.GetFutureAuthToken(); ok {
		l.FutureAuthToken = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*AuthLoggedOut) TypeName() string {
	return "auth.loggedOut"
}

// TypeInfo returns info about TL type.
func (l *AuthLoggedOut) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "auth.loggedOut",
		ID:   AuthLoggedOutTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "FutureAuthToken",
			SchemaName: "future_auth_token",
			Null:       !l.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (l *AuthLoggedOut) SetFlags() {
	if !(l.FutureAuthToken == nil) {
		l.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (l *AuthLoggedOut) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode auth.loggedOut#c3a2835f as nil")
	}
	b.PutID(AuthLoggedOutTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *AuthLoggedOut) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode auth.loggedOut#c3a2835f as nil")
	}
	l.SetFlags()
	if err := l.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode auth.loggedOut#c3a2835f: field flags: %w", err)
	}
	if l.Flags.Has(0) {
		b.PutBytes(l.FutureAuthToken)
	}
	return nil
}

// Decode implements bin.Decoder.
func (l *AuthLoggedOut) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode auth.loggedOut#c3a2835f to nil")
	}
	if err := b.ConsumeID(AuthLoggedOutTypeID); err != nil {
		return fmt.Errorf("unable to decode auth.loggedOut#c3a2835f: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *AuthLoggedOut) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode auth.loggedOut#c3a2835f to nil")
	}
	{
		if err := l.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode auth.loggedOut#c3a2835f: field flags: %w", err)
		}
	}
	if l.Flags.Has(0) {
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode auth.loggedOut#c3a2835f: field future_auth_token: %w", err)
		}
		l.FutureAuthToken = value
	}
	return nil
}

// SetFutureAuthToken sets value of FutureAuthToken conditional field.
func (l *AuthLoggedOut) SetFutureAuthToken(value []byte) {
	l.Flags.Set(0)
	l.FutureAuthToken = value
}

// GetFutureAuthToken returns value of FutureAuthToken conditional field and
// boolean which is true if field was set.
func (l *AuthLoggedOut) GetFutureAuthToken() (value []byte, ok bool) {
	if l == nil {
		return
	}
	if !l.Flags.Has(0) {
		return value, false
	}
	return l.FutureAuthToken, true
}
