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

// MessagesAcceptEncryptionRequest represents TL type `messages.acceptEncryption#3dbc0415`.
// Confirms creation of a secret chat
//
// See https://core.telegram.org/method/messages.acceptEncryption for reference.
type MessagesAcceptEncryptionRequest struct {
	// Secret chat ID
	Peer InputEncryptedChat
	// B = g ^ b mod p, see Wikipedia¹
	//
	// Links:
	//  1) https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
	GB []byte
	// 64-bit fingerprint of the received key
	KeyFingerprint int64
}

// MessagesAcceptEncryptionRequestTypeID is TL type id of MessagesAcceptEncryptionRequest.
const MessagesAcceptEncryptionRequestTypeID = 0x3dbc0415

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

func (a *MessagesAcceptEncryptionRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Peer.Zero()) {
		return false
	}
	if !(a.GB == nil) {
		return false
	}
	if !(a.KeyFingerprint == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesAcceptEncryptionRequest from given interface.
func (a *MessagesAcceptEncryptionRequest) FillFrom(from interface {
	GetPeer() (value InputEncryptedChat)
	GetGB() (value []byte)
	GetKeyFingerprint() (value int64)
}) {
	a.Peer = from.GetPeer()
	a.GB = from.GetGB()
	a.KeyFingerprint = from.GetKeyFingerprint()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesAcceptEncryptionRequest) TypeName() string {
	return "messages.acceptEncryption"
}

// TypeInfo returns info about TL type.
func (a *MessagesAcceptEncryptionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.acceptEncryption",
		ID:   MessagesAcceptEncryptionRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "GB",
			SchemaName: "g_b",
		},
		{
			Name:       "KeyFingerprint",
			SchemaName: "key_fingerprint",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *MessagesAcceptEncryptionRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.acceptEncryption#3dbc0415 as nil")
	}
	b.PutID(MessagesAcceptEncryptionRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *MessagesAcceptEncryptionRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.acceptEncryption#3dbc0415 as nil")
	}
	if err := a.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.acceptEncryption#3dbc0415: field peer: %w", err)
	}
	b.PutBytes(a.GB)
	b.PutLong(a.KeyFingerprint)
	return nil
}

// Decode implements bin.Decoder.
func (a *MessagesAcceptEncryptionRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.acceptEncryption#3dbc0415 to nil")
	}
	if err := b.ConsumeID(MessagesAcceptEncryptionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.acceptEncryption#3dbc0415: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *MessagesAcceptEncryptionRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.acceptEncryption#3dbc0415 to nil")
	}
	{
		if err := a.Peer.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.acceptEncryption#3dbc0415: field peer: %w", err)
		}
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode messages.acceptEncryption#3dbc0415: field g_b: %w", err)
		}
		a.GB = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.acceptEncryption#3dbc0415: field key_fingerprint: %w", err)
		}
		a.KeyFingerprint = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (a *MessagesAcceptEncryptionRequest) GetPeer() (value InputEncryptedChat) {
	if a == nil {
		return
	}
	return a.Peer
}

// GetGB returns value of GB field.
func (a *MessagesAcceptEncryptionRequest) GetGB() (value []byte) {
	if a == nil {
		return
	}
	return a.GB
}

// GetKeyFingerprint returns value of KeyFingerprint field.
func (a *MessagesAcceptEncryptionRequest) GetKeyFingerprint() (value int64) {
	if a == nil {
		return
	}
	return a.KeyFingerprint
}

// MessagesAcceptEncryption invokes method messages.acceptEncryption#3dbc0415 returning error if any.
// Confirms creation of a secret chat
//
// Possible errors:
//
//	400 CHAT_ID_INVALID: The provided chat id is invalid.
//	400 ENCRYPTION_ALREADY_ACCEPTED: Secret chat already accepted.
//	400 ENCRYPTION_ALREADY_DECLINED: The secret chat was already declined.
//
// See https://core.telegram.org/method/messages.acceptEncryption for reference.
func (c *Client) MessagesAcceptEncryption(ctx context.Context, request *MessagesAcceptEncryptionRequest) (EncryptedChatClass, error) {
	var result EncryptedChatBox

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