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

// AuthFinishPasskeyLoginRequest represents TL type `auth.finishPasskeyLogin#9857ad07`.
//
// See https://core.telegram.org/method/auth.finishPasskeyLogin for reference.
type AuthFinishPasskeyLoginRequest struct {
	// Flags field of AuthFinishPasskeyLoginRequest.
	Flags bin.Fields
	// Credential field of AuthFinishPasskeyLoginRequest.
	Credential InputPasskeyCredentialClass
	// FromDCID field of AuthFinishPasskeyLoginRequest.
	//
	// Use SetFromDCID and GetFromDCID helpers.
	FromDCID int
	// FromAuthKeyID field of AuthFinishPasskeyLoginRequest.
	//
	// Use SetFromAuthKeyID and GetFromAuthKeyID helpers.
	FromAuthKeyID int64
}

// AuthFinishPasskeyLoginRequestTypeID is TL type id of AuthFinishPasskeyLoginRequest.
const AuthFinishPasskeyLoginRequestTypeID = 0x9857ad07

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

func (f *AuthFinishPasskeyLoginRequest) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Flags.Zero()) {
		return false
	}
	if !(f.Credential == nil) {
		return false
	}
	if !(f.FromDCID == 0) {
		return false
	}
	if !(f.FromAuthKeyID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills AuthFinishPasskeyLoginRequest from given interface.
func (f *AuthFinishPasskeyLoginRequest) FillFrom(from interface {
	GetCredential() (value InputPasskeyCredentialClass)
	GetFromDCID() (value int, ok bool)
	GetFromAuthKeyID() (value int64, ok bool)
}) {
	f.Credential = from.GetCredential()
	if val, ok := from.GetFromDCID(); ok {
		f.FromDCID = val
	}

	if val, ok := from.GetFromAuthKeyID(); ok {
		f.FromAuthKeyID = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (f *AuthFinishPasskeyLoginRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "auth.finishPasskeyLogin",
		ID:   AuthFinishPasskeyLoginRequestTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Credential",
			SchemaName: "credential",
		},
		{
			Name:       "FromDCID",
			SchemaName: "from_dc_id",
			Null:       !f.Flags.Has(0),
		},
		{
			Name:       "FromAuthKeyID",
			SchemaName: "from_auth_key_id",
			Null:       !f.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (f *AuthFinishPasskeyLoginRequest) SetFlags() {
	if !(f.FromDCID == 0) {
		f.Flags.Set(0)
	}
	if !(f.FromAuthKeyID == 0) {
		f.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (f *AuthFinishPasskeyLoginRequest) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode auth.finishPasskeyLogin#9857ad07 as nil")
	}
	b.PutID(AuthFinishPasskeyLoginRequestTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *AuthFinishPasskeyLoginRequest) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode auth.finishPasskeyLogin#9857ad07 as nil")
	}
	f.SetFlags()
	if err := f.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode auth.finishPasskeyLogin#9857ad07: field flags: %w", err)
	}
	if f.Credential == nil {
		return fmt.Errorf("unable to encode auth.finishPasskeyLogin#9857ad07: field credential is nil")
	}
	if err := f.Credential.Encode(b); err != nil {
		return fmt.Errorf("unable to encode auth.finishPasskeyLogin#9857ad07: field credential: %w", err)
	}
	if f.Flags.Has(0) {
		b.PutInt(f.FromDCID)
	}
	if f.Flags.Has(0) {
		b.PutLong(f.FromAuthKeyID)
	}
	return nil
}

// Decode implements bin.Decoder.
func (f *AuthFinishPasskeyLoginRequest) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode auth.finishPasskeyLogin#9857ad07 to nil")
	}
	if err := b.ConsumeID(AuthFinishPasskeyLoginRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode auth.finishPasskeyLogin#9857ad07: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *AuthFinishPasskeyLoginRequest) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode auth.finishPasskeyLogin#9857ad07 to nil")
	}
	{
		if err := f.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode auth.finishPasskeyLogin#9857ad07: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPasskeyCredential(b)
		if err != nil {
			return fmt.Errorf("unable to decode auth.finishPasskeyLogin#9857ad07: field credential: %w", err)
		}
		f.Credential = value
	}
	if f.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode auth.finishPasskeyLogin#9857ad07: field from_dc_id: %w", err)
		}
		f.FromDCID = value
	}
	if f.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode auth.finishPasskeyLogin#9857ad07: field from_auth_key_id: %w", err)
		}
		f.FromAuthKeyID = value
	}
	return nil
}

// GetCredential returns value of Credential field.
func (f *AuthFinishPasskeyLoginRequest) GetCredential() (value InputPasskeyCredentialClass) {
	if f == nil {
		return
	}
	return f.Credential
}

// SetFromDCID sets value of FromDCID conditional field.
func (f *AuthFinishPasskeyLoginRequest) SetFromDCID(value int) {
	f.Flags.Set(0)
	f.FromDCID = value
}

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

// SetFromAuthKeyID sets value of FromAuthKeyID conditional field.
func (f *AuthFinishPasskeyLoginRequest) SetFromAuthKeyID(value int64) {
	f.Flags.Set(0)
	f.FromAuthKeyID = value
}

// GetFromAuthKeyID returns value of FromAuthKeyID conditional field and
// boolean which is true if field was set.
func (f *AuthFinishPasskeyLoginRequest) GetFromAuthKeyID() (value int64, ok bool) {
	if f == nil {
		return
	}
	if !f.Flags.Has(0) {
		return value, false
	}
	return f.FromAuthKeyID, true
}

// AuthFinishPasskeyLogin invokes method auth.finishPasskeyLogin#9857ad07 returning error if any.
//
// See https://core.telegram.org/method/auth.finishPasskeyLogin for reference.
func (c *Client) AuthFinishPasskeyLogin(ctx context.Context, request *AuthFinishPasskeyLoginRequest) (AuthAuthorizationClass, error) {
	var result AuthAuthorizationBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return result.Authorization, nil
}
