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

// SetLoginEmailAddressRequest represents TL type `setLoginEmailAddress#37bb43d4`.
type SetLoginEmailAddressRequest struct {
	// New login email address
	NewLoginEmailAddress string
}

// SetLoginEmailAddressRequestTypeID is TL type id of SetLoginEmailAddressRequest.
const SetLoginEmailAddressRequestTypeID = 0x37bb43d4

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

func (s *SetLoginEmailAddressRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.NewLoginEmailAddress == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetLoginEmailAddressRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setLoginEmailAddress",
		ID:   SetLoginEmailAddressRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "NewLoginEmailAddress",
			SchemaName: "new_login_email_address",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetLoginEmailAddressRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setLoginEmailAddress#37bb43d4 as nil")
	}
	b.PutID(SetLoginEmailAddressRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetLoginEmailAddressRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setLoginEmailAddress#37bb43d4 as nil")
	}
	b.PutString(s.NewLoginEmailAddress)
	return nil
}

// Decode implements bin.Decoder.
func (s *SetLoginEmailAddressRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setLoginEmailAddress#37bb43d4 to nil")
	}
	if err := b.ConsumeID(SetLoginEmailAddressRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setLoginEmailAddress#37bb43d4: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetLoginEmailAddressRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setLoginEmailAddress#37bb43d4 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setLoginEmailAddress#37bb43d4: field new_login_email_address: %w", err)
		}
		s.NewLoginEmailAddress = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetLoginEmailAddressRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setLoginEmailAddress#37bb43d4 as nil")
	}
	b.ObjStart()
	b.PutID("setLoginEmailAddress")
	b.Comma()
	b.FieldStart("new_login_email_address")
	b.PutString(s.NewLoginEmailAddress)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetLoginEmailAddressRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setLoginEmailAddress#37bb43d4 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setLoginEmailAddress"); err != nil {
				return fmt.Errorf("unable to decode setLoginEmailAddress#37bb43d4: %w", err)
			}
		case "new_login_email_address":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setLoginEmailAddress#37bb43d4: field new_login_email_address: %w", err)
			}
			s.NewLoginEmailAddress = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetNewLoginEmailAddress returns value of NewLoginEmailAddress field.
func (s *SetLoginEmailAddressRequest) GetNewLoginEmailAddress() (value string) {
	if s == nil {
		return
	}
	return s.NewLoginEmailAddress
}

// SetLoginEmailAddress invokes method setLoginEmailAddress#37bb43d4 returning error if any.
func (c *Client) SetLoginEmailAddress(ctx context.Context, newloginemailaddress string) (*EmailAddressAuthenticationCodeInfo, error) {
	var result EmailAddressAuthenticationCodeInfo

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