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

// GetAllPassportElementsRequest represents TL type `getAllPassportElements#86782aeb`.
type GetAllPassportElementsRequest struct {
	// The 2-step verification password of the current user
	Password string
}

// GetAllPassportElementsRequestTypeID is TL type id of GetAllPassportElementsRequest.
const GetAllPassportElementsRequestTypeID = 0x86782aeb

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

func (g *GetAllPassportElementsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Password == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetAllPassportElementsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getAllPassportElements",
		ID:   GetAllPassportElementsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Password",
			SchemaName: "password",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetAllPassportElementsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getAllPassportElements#86782aeb as nil")
	}
	b.PutID(GetAllPassportElementsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetAllPassportElementsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getAllPassportElements#86782aeb as nil")
	}
	b.PutString(g.Password)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetAllPassportElementsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getAllPassportElements#86782aeb to nil")
	}
	if err := b.ConsumeID(GetAllPassportElementsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getAllPassportElements#86782aeb: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetAllPassportElementsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getAllPassportElements#86782aeb to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getAllPassportElements#86782aeb: field password: %w", err)
		}
		g.Password = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetAllPassportElementsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getAllPassportElements#86782aeb as nil")
	}
	b.ObjStart()
	b.PutID("getAllPassportElements")
	b.Comma()
	b.FieldStart("password")
	b.PutString(g.Password)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetAllPassportElementsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getAllPassportElements#86782aeb to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getAllPassportElements"); err != nil {
				return fmt.Errorf("unable to decode getAllPassportElements#86782aeb: %w", err)
			}
		case "password":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getAllPassportElements#86782aeb: field password: %w", err)
			}
			g.Password = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPassword returns value of Password field.
func (g *GetAllPassportElementsRequest) GetPassword() (value string) {
	if g == nil {
		return
	}
	return g.Password
}

// GetAllPassportElements invokes method getAllPassportElements#86782aeb returning error if any.
func (c *Client) GetAllPassportElements(ctx context.Context, password string) (*PassportElements, error) {
	var result PassportElements

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