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

// SetAuthenticationPhoneNumberRequest represents TL type `setAuthenticationPhoneNumber#33c0d823`.
type SetAuthenticationPhoneNumberRequest struct {
	// The phone number of the user, in international format
	PhoneNumber string
	// Settings for the authentication of the user's phone number; pass null to use default
	// settings
	Settings PhoneNumberAuthenticationSettings
}

// SetAuthenticationPhoneNumberRequestTypeID is TL type id of SetAuthenticationPhoneNumberRequest.
const SetAuthenticationPhoneNumberRequestTypeID = 0x33c0d823

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

func (s *SetAuthenticationPhoneNumberRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.PhoneNumber == "") {
		return false
	}
	if !(s.Settings.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetAuthenticationPhoneNumberRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setAuthenticationPhoneNumber",
		ID:   SetAuthenticationPhoneNumberRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PhoneNumber",
			SchemaName: "phone_number",
		},
		{
			Name:       "Settings",
			SchemaName: "settings",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetAuthenticationPhoneNumberRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAuthenticationPhoneNumber#33c0d823 as nil")
	}
	b.PutID(SetAuthenticationPhoneNumberRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetAuthenticationPhoneNumberRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAuthenticationPhoneNumber#33c0d823 as nil")
	}
	b.PutString(s.PhoneNumber)
	if err := s.Settings.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setAuthenticationPhoneNumber#33c0d823: field settings: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetAuthenticationPhoneNumberRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setAuthenticationPhoneNumber#33c0d823 to nil")
	}
	if err := b.ConsumeID(SetAuthenticationPhoneNumberRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setAuthenticationPhoneNumber#33c0d823: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetAuthenticationPhoneNumberRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setAuthenticationPhoneNumber#33c0d823 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setAuthenticationPhoneNumber#33c0d823: field phone_number: %w", err)
		}
		s.PhoneNumber = value
	}
	{
		if err := s.Settings.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setAuthenticationPhoneNumber#33c0d823: field settings: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetAuthenticationPhoneNumberRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setAuthenticationPhoneNumber#33c0d823 as nil")
	}
	b.ObjStart()
	b.PutID("setAuthenticationPhoneNumber")
	b.Comma()
	b.FieldStart("phone_number")
	b.PutString(s.PhoneNumber)
	b.Comma()
	b.FieldStart("settings")
	if err := s.Settings.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setAuthenticationPhoneNumber#33c0d823: field settings: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetAuthenticationPhoneNumberRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setAuthenticationPhoneNumber#33c0d823 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setAuthenticationPhoneNumber"); err != nil {
				return fmt.Errorf("unable to decode setAuthenticationPhoneNumber#33c0d823: %w", err)
			}
		case "phone_number":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setAuthenticationPhoneNumber#33c0d823: field phone_number: %w", err)
			}
			s.PhoneNumber = value
		case "settings":
			if err := s.Settings.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setAuthenticationPhoneNumber#33c0d823: field settings: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPhoneNumber returns value of PhoneNumber field.
func (s *SetAuthenticationPhoneNumberRequest) GetPhoneNumber() (value string) {
	if s == nil {
		return
	}
	return s.PhoneNumber
}

// GetSettings returns value of Settings field.
func (s *SetAuthenticationPhoneNumberRequest) GetSettings() (value PhoneNumberAuthenticationSettings) {
	if s == nil {
		return
	}
	return s.Settings
}

// SetAuthenticationPhoneNumber invokes method setAuthenticationPhoneNumber#33c0d823 returning error if any.
func (c *Client) SetAuthenticationPhoneNumber(ctx context.Context, request *SetAuthenticationPhoneNumberRequest) error {
	var ok Ok

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