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

// AccountAcceptAuthorizationRequest represents TL type `account.acceptAuthorization#f3ed4c73`.
// Sends a Telegram Passport authorization form, effectively sharing data with the
// service
//
// See https://core.telegram.org/method/account.acceptAuthorization for reference.
type AccountAcceptAuthorizationRequest struct {
	// Bot ID
	BotID int64
	// Telegram Passport element types requested by the service
	Scope string
	// Service's public key
	PublicKey string
	// Types of values sent and their hashes
	ValueHashes []SecureValueHash
	// Encrypted values
	Credentials SecureCredentialsEncrypted
}

// AccountAcceptAuthorizationRequestTypeID is TL type id of AccountAcceptAuthorizationRequest.
const AccountAcceptAuthorizationRequestTypeID = 0xf3ed4c73

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

func (a *AccountAcceptAuthorizationRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.BotID == 0) {
		return false
	}
	if !(a.Scope == "") {
		return false
	}
	if !(a.PublicKey == "") {
		return false
	}
	if !(a.ValueHashes == nil) {
		return false
	}
	if !(a.Credentials.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills AccountAcceptAuthorizationRequest from given interface.
func (a *AccountAcceptAuthorizationRequest) FillFrom(from interface {
	GetBotID() (value int64)
	GetScope() (value string)
	GetPublicKey() (value string)
	GetValueHashes() (value []SecureValueHash)
	GetCredentials() (value SecureCredentialsEncrypted)
}) {
	a.BotID = from.GetBotID()
	a.Scope = from.GetScope()
	a.PublicKey = from.GetPublicKey()
	a.ValueHashes = from.GetValueHashes()
	a.Credentials = from.GetCredentials()
}

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

// TypeName returns name of type in TL schema.
func (*AccountAcceptAuthorizationRequest) TypeName() string {
	return "account.acceptAuthorization"
}

// TypeInfo returns info about TL type.
func (a *AccountAcceptAuthorizationRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.acceptAuthorization",
		ID:   AccountAcceptAuthorizationRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotID",
			SchemaName: "bot_id",
		},
		{
			Name:       "Scope",
			SchemaName: "scope",
		},
		{
			Name:       "PublicKey",
			SchemaName: "public_key",
		},
		{
			Name:       "ValueHashes",
			SchemaName: "value_hashes",
		},
		{
			Name:       "Credentials",
			SchemaName: "credentials",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AccountAcceptAuthorizationRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode account.acceptAuthorization#f3ed4c73 as nil")
	}
	b.PutID(AccountAcceptAuthorizationRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AccountAcceptAuthorizationRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode account.acceptAuthorization#f3ed4c73 as nil")
	}
	b.PutLong(a.BotID)
	b.PutString(a.Scope)
	b.PutString(a.PublicKey)
	b.PutVectorHeader(len(a.ValueHashes))
	for idx, v := range a.ValueHashes {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.acceptAuthorization#f3ed4c73: field value_hashes element with index %d: %w", idx, err)
		}
	}
	if err := a.Credentials.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.acceptAuthorization#f3ed4c73: field credentials: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AccountAcceptAuthorizationRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode account.acceptAuthorization#f3ed4c73 to nil")
	}
	if err := b.ConsumeID(AccountAcceptAuthorizationRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.acceptAuthorization#f3ed4c73: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AccountAcceptAuthorizationRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode account.acceptAuthorization#f3ed4c73 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode account.acceptAuthorization#f3ed4c73: field bot_id: %w", err)
		}
		a.BotID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode account.acceptAuthorization#f3ed4c73: field scope: %w", err)
		}
		a.Scope = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode account.acceptAuthorization#f3ed4c73: field public_key: %w", err)
		}
		a.PublicKey = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.acceptAuthorization#f3ed4c73: field value_hashes: %w", err)
		}

		if headerLen > 0 {
			a.ValueHashes = make([]SecureValueHash, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value SecureValueHash
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode account.acceptAuthorization#f3ed4c73: field value_hashes: %w", err)
			}
			a.ValueHashes = append(a.ValueHashes, value)
		}
	}
	{
		if err := a.Credentials.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.acceptAuthorization#f3ed4c73: field credentials: %w", err)
		}
	}
	return nil
}

// GetBotID returns value of BotID field.
func (a *AccountAcceptAuthorizationRequest) GetBotID() (value int64) {
	if a == nil {
		return
	}
	return a.BotID
}

// GetScope returns value of Scope field.
func (a *AccountAcceptAuthorizationRequest) GetScope() (value string) {
	if a == nil {
		return
	}
	return a.Scope
}

// GetPublicKey returns value of PublicKey field.
func (a *AccountAcceptAuthorizationRequest) GetPublicKey() (value string) {
	if a == nil {
		return
	}
	return a.PublicKey
}

// GetValueHashes returns value of ValueHashes field.
func (a *AccountAcceptAuthorizationRequest) GetValueHashes() (value []SecureValueHash) {
	if a == nil {
		return
	}
	return a.ValueHashes
}

// GetCredentials returns value of Credentials field.
func (a *AccountAcceptAuthorizationRequest) GetCredentials() (value SecureCredentialsEncrypted) {
	if a == nil {
		return
	}
	return a.Credentials
}

// AccountAcceptAuthorization invokes method account.acceptAuthorization#f3ed4c73 returning error if any.
// Sends a Telegram Passport authorization form, effectively sharing data with the
// service
//
// See https://core.telegram.org/method/account.acceptAuthorization for reference.
func (c *Client) AccountAcceptAuthorization(ctx context.Context, request *AccountAcceptAuthorizationRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
