// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// ConfirmQrCodeAuthenticationRequest represents TL type `confirmQrCodeAuthentication#e993a72d`.
type ConfirmQrCodeAuthenticationRequest struct {
	// A link from a QR code. The link must be scanned by the in-app camera
	Link string
}

// ConfirmQrCodeAuthenticationRequestTypeID is TL type id of ConfirmQrCodeAuthenticationRequest.
const ConfirmQrCodeAuthenticationRequestTypeID = 0xe993a72d

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

func (c *ConfirmQrCodeAuthenticationRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Link == "") {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*ConfirmQrCodeAuthenticationRequest) TypeName() string {
	return "confirmQrCodeAuthentication"
}

// TypeInfo returns info about TL type.
func (c *ConfirmQrCodeAuthenticationRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "confirmQrCodeAuthentication",
		ID:   ConfirmQrCodeAuthenticationRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Link",
			SchemaName: "link",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ConfirmQrCodeAuthenticationRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode confirmQrCodeAuthentication#e993a72d as nil")
	}
	b.PutID(ConfirmQrCodeAuthenticationRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ConfirmQrCodeAuthenticationRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode confirmQrCodeAuthentication#e993a72d as nil")
	}
	b.PutString(c.Link)
	return nil
}

// Decode implements bin.Decoder.
func (c *ConfirmQrCodeAuthenticationRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode confirmQrCodeAuthentication#e993a72d to nil")
	}
	if err := b.ConsumeID(ConfirmQrCodeAuthenticationRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode confirmQrCodeAuthentication#e993a72d: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ConfirmQrCodeAuthenticationRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode confirmQrCodeAuthentication#e993a72d to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode confirmQrCodeAuthentication#e993a72d: field link: %w", err)
		}
		c.Link = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ConfirmQrCodeAuthenticationRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode confirmQrCodeAuthentication#e993a72d as nil")
	}
	b.ObjStart()
	b.PutID("confirmQrCodeAuthentication")
	b.Comma()
	b.FieldStart("link")
	b.PutString(c.Link)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ConfirmQrCodeAuthenticationRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode confirmQrCodeAuthentication#e993a72d to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("confirmQrCodeAuthentication"); err != nil {
				return fmt.Errorf("unable to decode confirmQrCodeAuthentication#e993a72d: %w", err)
			}
		case "link":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode confirmQrCodeAuthentication#e993a72d: field link: %w", err)
			}
			c.Link = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetLink returns value of Link field.
func (c *ConfirmQrCodeAuthenticationRequest) GetLink() (value string) {
	if c == nil {
		return
	}
	return c.Link
}

// ConfirmQrCodeAuthentication invokes method confirmQrCodeAuthentication#e993a72d returning error if any.
func (c *Client) ConfirmQrCodeAuthentication(ctx context.Context, link string) (*Session, error) {
	var result Session

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