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

// ContactsAddContactRequest represents TL type `contacts.addContact#d9ba2e54`.
// Add an existing telegram user as contact.
// Use contacts.importContacts¹ to add contacts by phone number, without knowing their
// Telegram ID.
//
// Links:
//  1. https://core.telegram.org/method/contacts.importContacts
//
// See https://core.telegram.org/method/contacts.addContact for reference.
type ContactsAddContactRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Allow the other user to see our phone number?
	AddPhonePrivacyException bool
	// Telegram ID of the other user
	ID InputUserClass
	// First name
	FirstName string
	// Last name
	LastName string
	// User's phone number, may be omitted to simply add the user to the contact list,
	// without a phone number.
	Phone string
	// Note field of ContactsAddContactRequest.
	//
	// Use SetNote and GetNote helpers.
	Note TextWithEntities
}

// ContactsAddContactRequestTypeID is TL type id of ContactsAddContactRequest.
const ContactsAddContactRequestTypeID = 0xd9ba2e54

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

func (a *ContactsAddContactRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Flags.Zero()) {
		return false
	}
	if !(a.AddPhonePrivacyException == false) {
		return false
	}
	if !(a.ID == nil) {
		return false
	}
	if !(a.FirstName == "") {
		return false
	}
	if !(a.LastName == "") {
		return false
	}
	if !(a.Phone == "") {
		return false
	}
	if !(a.Note.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills ContactsAddContactRequest from given interface.
func (a *ContactsAddContactRequest) FillFrom(from interface {
	GetAddPhonePrivacyException() (value bool)
	GetID() (value InputUserClass)
	GetFirstName() (value string)
	GetLastName() (value string)
	GetPhone() (value string)
	GetNote() (value TextWithEntities, ok bool)
}) {
	a.AddPhonePrivacyException = from.GetAddPhonePrivacyException()
	a.ID = from.GetID()
	a.FirstName = from.GetFirstName()
	a.LastName = from.GetLastName()
	a.Phone = from.GetPhone()
	if val, ok := from.GetNote(); ok {
		a.Note = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*ContactsAddContactRequest) TypeName() string {
	return "contacts.addContact"
}

// TypeInfo returns info about TL type.
func (a *ContactsAddContactRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.addContact",
		ID:   ContactsAddContactRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "AddPhonePrivacyException",
			SchemaName: "add_phone_privacy_exception",
			Null:       !a.Flags.Has(0),
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "FirstName",
			SchemaName: "first_name",
		},
		{
			Name:       "LastName",
			SchemaName: "last_name",
		},
		{
			Name:       "Phone",
			SchemaName: "phone",
		},
		{
			Name:       "Note",
			SchemaName: "note",
			Null:       !a.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (a *ContactsAddContactRequest) SetFlags() {
	if !(a.AddPhonePrivacyException == false) {
		a.Flags.Set(0)
	}
	if !(a.Note.Zero()) {
		a.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (a *ContactsAddContactRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode contacts.addContact#d9ba2e54 as nil")
	}
	b.PutID(ContactsAddContactRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *ContactsAddContactRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode contacts.addContact#d9ba2e54 as nil")
	}
	a.SetFlags()
	if err := a.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode contacts.addContact#d9ba2e54: field flags: %w", err)
	}
	if a.ID == nil {
		return fmt.Errorf("unable to encode contacts.addContact#d9ba2e54: field id is nil")
	}
	if err := a.ID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode contacts.addContact#d9ba2e54: field id: %w", err)
	}
	b.PutString(a.FirstName)
	b.PutString(a.LastName)
	b.PutString(a.Phone)
	if a.Flags.Has(1) {
		if err := a.Note.Encode(b); err != nil {
			return fmt.Errorf("unable to encode contacts.addContact#d9ba2e54: field note: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *ContactsAddContactRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode contacts.addContact#d9ba2e54 to nil")
	}
	if err := b.ConsumeID(ContactsAddContactRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.addContact#d9ba2e54: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *ContactsAddContactRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode contacts.addContact#d9ba2e54 to nil")
	}
	{
		if err := a.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode contacts.addContact#d9ba2e54: field flags: %w", err)
		}
	}
	a.AddPhonePrivacyException = a.Flags.Has(0)
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode contacts.addContact#d9ba2e54: field id: %w", err)
		}
		a.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.addContact#d9ba2e54: field first_name: %w", err)
		}
		a.FirstName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.addContact#d9ba2e54: field last_name: %w", err)
		}
		a.LastName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.addContact#d9ba2e54: field phone: %w", err)
		}
		a.Phone = value
	}
	if a.Flags.Has(1) {
		if err := a.Note.Decode(b); err != nil {
			return fmt.Errorf("unable to decode contacts.addContact#d9ba2e54: field note: %w", err)
		}
	}
	return nil
}

// SetAddPhonePrivacyException sets value of AddPhonePrivacyException conditional field.
func (a *ContactsAddContactRequest) SetAddPhonePrivacyException(value bool) {
	if value {
		a.Flags.Set(0)
		a.AddPhonePrivacyException = true
	} else {
		a.Flags.Unset(0)
		a.AddPhonePrivacyException = false
	}
}

// GetAddPhonePrivacyException returns value of AddPhonePrivacyException conditional field.
func (a *ContactsAddContactRequest) GetAddPhonePrivacyException() (value bool) {
	if a == nil {
		return
	}
	return a.Flags.Has(0)
}

// GetID returns value of ID field.
func (a *ContactsAddContactRequest) GetID() (value InputUserClass) {
	if a == nil {
		return
	}
	return a.ID
}

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

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

// GetPhone returns value of Phone field.
func (a *ContactsAddContactRequest) GetPhone() (value string) {
	if a == nil {
		return
	}
	return a.Phone
}

// SetNote sets value of Note conditional field.
func (a *ContactsAddContactRequest) SetNote(value TextWithEntities) {
	a.Flags.Set(1)
	a.Note = value
}

// GetNote returns value of Note conditional field and
// boolean which is true if field was set.
func (a *ContactsAddContactRequest) GetNote() (value TextWithEntities, ok bool) {
	if a == nil {
		return
	}
	if !a.Flags.Has(1) {
		return value, false
	}
	return a.Note, true
}

// ContactsAddContact invokes method contacts.addContact#d9ba2e54 returning error if any.
// Add an existing telegram user as contact.
// Use contacts.importContacts¹ to add contacts by phone number, without knowing their
// Telegram ID.
//
// Links:
//  1. https://core.telegram.org/method/contacts.importContacts
//
// Possible errors:
//
//	400 CHANNEL_PRIVATE: You haven't joined this channel/supergroup.
//	400 CONTACT_ID_INVALID: The provided contact ID is invalid.
//	400 CONTACT_NAME_EMPTY: Contact name empty.
//	400 MSG_ID_INVALID: Invalid message ID provided.
//
// See https://core.telegram.org/method/contacts.addContact for reference.
func (c *Client) ContactsAddContact(ctx context.Context, request *ContactsAddContactRequest) (UpdatesClass, error) {
	var result UpdatesBox

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