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

// AuthSignUpRequest represents TL type `auth.signUp#aac7b717`.
// Registers a validated phone number in the system.
//
// See https://core.telegram.org/method/auth.signUp for reference.
type AuthSignUpRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, users on Telegram that have already added phone_number to their contacts will
	// not receive signup notifications about this user.
	NoJoinedNotifications bool
	// Phone number in the international format
	PhoneNumber string
	// SMS-message ID
	PhoneCodeHash string
	// New user first name
	FirstName string
	// New user last name
	LastName string
}

// AuthSignUpRequestTypeID is TL type id of AuthSignUpRequest.
const AuthSignUpRequestTypeID = 0xaac7b717

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

func (s *AuthSignUpRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.NoJoinedNotifications == false) {
		return false
	}
	if !(s.PhoneNumber == "") {
		return false
	}
	if !(s.PhoneCodeHash == "") {
		return false
	}
	if !(s.FirstName == "") {
		return false
	}
	if !(s.LastName == "") {
		return false
	}

	return true
}

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

// FillFrom fills AuthSignUpRequest from given interface.
func (s *AuthSignUpRequest) FillFrom(from interface {
	GetNoJoinedNotifications() (value bool)
	GetPhoneNumber() (value string)
	GetPhoneCodeHash() (value string)
	GetFirstName() (value string)
	GetLastName() (value string)
}) {
	s.NoJoinedNotifications = from.GetNoJoinedNotifications()
	s.PhoneNumber = from.GetPhoneNumber()
	s.PhoneCodeHash = from.GetPhoneCodeHash()
	s.FirstName = from.GetFirstName()
	s.LastName = from.GetLastName()
}

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

// TypeName returns name of type in TL schema.
func (*AuthSignUpRequest) TypeName() string {
	return "auth.signUp"
}

// TypeInfo returns info about TL type.
func (s *AuthSignUpRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "auth.signUp",
		ID:   AuthSignUpRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "NoJoinedNotifications",
			SchemaName: "no_joined_notifications",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "PhoneNumber",
			SchemaName: "phone_number",
		},
		{
			Name:       "PhoneCodeHash",
			SchemaName: "phone_code_hash",
		},
		{
			Name:       "FirstName",
			SchemaName: "first_name",
		},
		{
			Name:       "LastName",
			SchemaName: "last_name",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *AuthSignUpRequest) SetFlags() {
	if !(s.NoJoinedNotifications == false) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *AuthSignUpRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode auth.signUp#aac7b717 as nil")
	}
	b.PutID(AuthSignUpRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *AuthSignUpRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode auth.signUp#aac7b717 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode auth.signUp#aac7b717: field flags: %w", err)
	}
	b.PutString(s.PhoneNumber)
	b.PutString(s.PhoneCodeHash)
	b.PutString(s.FirstName)
	b.PutString(s.LastName)
	return nil
}

// Decode implements bin.Decoder.
func (s *AuthSignUpRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode auth.signUp#aac7b717 to nil")
	}
	if err := b.ConsumeID(AuthSignUpRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode auth.signUp#aac7b717: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *AuthSignUpRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode auth.signUp#aac7b717 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode auth.signUp#aac7b717: field flags: %w", err)
		}
	}
	s.NoJoinedNotifications = s.Flags.Has(0)
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode auth.signUp#aac7b717: field phone_number: %w", err)
		}
		s.PhoneNumber = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode auth.signUp#aac7b717: field phone_code_hash: %w", err)
		}
		s.PhoneCodeHash = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode auth.signUp#aac7b717: field first_name: %w", err)
		}
		s.FirstName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode auth.signUp#aac7b717: field last_name: %w", err)
		}
		s.LastName = value
	}
	return nil
}

// SetNoJoinedNotifications sets value of NoJoinedNotifications conditional field.
func (s *AuthSignUpRequest) SetNoJoinedNotifications(value bool) {
	if value {
		s.Flags.Set(0)
		s.NoJoinedNotifications = true
	} else {
		s.Flags.Unset(0)
		s.NoJoinedNotifications = false
	}
}

// GetNoJoinedNotifications returns value of NoJoinedNotifications conditional field.
func (s *AuthSignUpRequest) GetNoJoinedNotifications() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

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

// GetPhoneCodeHash returns value of PhoneCodeHash field.
func (s *AuthSignUpRequest) GetPhoneCodeHash() (value string) {
	if s == nil {
		return
	}
	return s.PhoneCodeHash
}

// GetFirstName returns value of FirstName field.
func (s *AuthSignUpRequest) GetFirstName() (value string) {
	if s == nil {
		return
	}
	return s.FirstName
}

// GetLastName returns value of LastName field.
func (s *AuthSignUpRequest) GetLastName() (value string) {
	if s == nil {
		return
	}
	return s.LastName
}

// AuthSignUp invokes method auth.signUp#aac7b717 returning error if any.
// Registers a validated phone number in the system.
//
// Possible errors:
//
//	400 FIRSTNAME_INVALID: The first name is invalid.
//	400 LASTNAME_INVALID: The last name is invalid.
//	400 PHONE_CODE_EMPTY: phone_code is missing.
//	400 PHONE_CODE_EXPIRED: The phone code you provided has expired.
//	400 PHONE_CODE_INVALID: The provided phone code is invalid.
//	400 PHONE_NUMBER_FLOOD: You asked for the code too many times.
//	406 PHONE_NUMBER_INVALID: The phone number is invalid.
//	400 PHONE_NUMBER_OCCUPIED: The phone number is already in use.
//
// See https://core.telegram.org/method/auth.signUp for reference.
func (c *Client) AuthSignUp(ctx context.Context, request *AuthSignUpRequest) (AuthAuthorizationClass, error) {
	var result AuthAuthorizationBox

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