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

// AuthAcceptLoginTokenRequest represents TL type `auth.acceptLoginToken#e894ad4d`.
// Accept QR code login token, logging in the app that generated it.
// Returns info about the new session.
// For more info, see login via QR code¹.
//
// Links:
//  1. https://core.telegram.org/api/qr-login
//
// See https://core.telegram.org/method/auth.acceptLoginToken for reference.
type AuthAcceptLoginTokenRequest struct {
	// Login token embedded in QR code, for more info, see login via QR code¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/qr-login
	Token []byte
}

// AuthAcceptLoginTokenRequestTypeID is TL type id of AuthAcceptLoginTokenRequest.
const AuthAcceptLoginTokenRequestTypeID = 0xe894ad4d

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

func (a *AuthAcceptLoginTokenRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Token == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AuthAcceptLoginTokenRequest from given interface.
func (a *AuthAcceptLoginTokenRequest) FillFrom(from interface {
	GetToken() (value []byte)
}) {
	a.Token = from.GetToken()
}

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

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

// TypeInfo returns info about TL type.
func (a *AuthAcceptLoginTokenRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "auth.acceptLoginToken",
		ID:   AuthAcceptLoginTokenRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Token",
			SchemaName: "token",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AuthAcceptLoginTokenRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode auth.acceptLoginToken#e894ad4d as nil")
	}
	b.PutID(AuthAcceptLoginTokenRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AuthAcceptLoginTokenRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode auth.acceptLoginToken#e894ad4d as nil")
	}
	b.PutBytes(a.Token)
	return nil
}

// Decode implements bin.Decoder.
func (a *AuthAcceptLoginTokenRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode auth.acceptLoginToken#e894ad4d to nil")
	}
	if err := b.ConsumeID(AuthAcceptLoginTokenRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode auth.acceptLoginToken#e894ad4d: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AuthAcceptLoginTokenRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode auth.acceptLoginToken#e894ad4d to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode auth.acceptLoginToken#e894ad4d: field token: %w", err)
		}
		a.Token = value
	}
	return nil
}

// GetToken returns value of Token field.
func (a *AuthAcceptLoginTokenRequest) GetToken() (value []byte) {
	if a == nil {
		return
	}
	return a.Token
}

// AuthAcceptLoginToken invokes method auth.acceptLoginToken#e894ad4d returning error if any.
// Accept QR code login token, logging in the app that generated it.
// Returns info about the new session.
// For more info, see login via QR code¹.
//
// Links:
//  1. https://core.telegram.org/api/qr-login
//
// Possible errors:
//
//	400 AUTH_TOKEN_ALREADY_ACCEPTED: The specified auth token was already accepted.
//	400 AUTH_TOKEN_EXCEPTION: An error occurred while importing the auth token.
//	400 AUTH_TOKEN_EXPIRED: The authorization token has expired.
//	400 AUTH_TOKEN_INVALIDX: The specified auth token is invalid.
//
// See https://core.telegram.org/method/auth.acceptLoginToken for reference.
func (c *Client) AuthAcceptLoginToken(ctx context.Context, token []byte) (*Authorization, error) {
	var result Authorization

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