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

// InputPhoneContact represents TL type `inputPhoneContact#6a1dc4be`.
// Phone contact.
//
// See https://core.telegram.org/constructor/inputPhoneContact for reference.
type InputPhoneContact struct {
	// Flags field of InputPhoneContact.
	Flags bin.Fields
	// An arbitrary 64-bit integer: it should be set, for example, to an incremental number
	// when using contacts.importContacts¹, in order to retry importing only the contacts
	// that weren't imported successfully, according to the client_ids returned in contacts
	// importedContacts².retry_contacts.
	//
	// Links:
	//  1) https://core.telegram.org/method/contacts.importContacts
	//  2) https://core.telegram.org/constructor/contacts.importedContacts
	ClientID int64
	// Phone number
	Phone string
	// Contact's first name
	FirstName string
	// Contact's last name
	LastName string
	// Note field of InputPhoneContact.
	//
	// Use SetNote and GetNote helpers.
	Note TextWithEntities
}

// InputPhoneContactTypeID is TL type id of InputPhoneContact.
const InputPhoneContactTypeID = 0x6a1dc4be

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

func (i *InputPhoneContact) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Flags.Zero()) {
		return false
	}
	if !(i.ClientID == 0) {
		return false
	}
	if !(i.Phone == "") {
		return false
	}
	if !(i.FirstName == "") {
		return false
	}
	if !(i.LastName == "") {
		return false
	}
	if !(i.Note.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills InputPhoneContact from given interface.
func (i *InputPhoneContact) FillFrom(from interface {
	GetClientID() (value int64)
	GetPhone() (value string)
	GetFirstName() (value string)
	GetLastName() (value string)
	GetNote() (value TextWithEntities, ok bool)
}) {
	i.ClientID = from.GetClientID()
	i.Phone = from.GetPhone()
	i.FirstName = from.GetFirstName()
	i.LastName = from.GetLastName()
	if val, ok := from.GetNote(); ok {
		i.Note = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (i *InputPhoneContact) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputPhoneContact",
		ID:   InputPhoneContactTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ClientID",
			SchemaName: "client_id",
		},
		{
			Name:       "Phone",
			SchemaName: "phone",
		},
		{
			Name:       "FirstName",
			SchemaName: "first_name",
		},
		{
			Name:       "LastName",
			SchemaName: "last_name",
		},
		{
			Name:       "Note",
			SchemaName: "note",
			Null:       !i.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (i *InputPhoneContact) SetFlags() {
	if !(i.Note.Zero()) {
		i.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (i *InputPhoneContact) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPhoneContact#6a1dc4be as nil")
	}
	b.PutID(InputPhoneContactTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputPhoneContact) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPhoneContact#6a1dc4be as nil")
	}
	i.SetFlags()
	if err := i.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputPhoneContact#6a1dc4be: field flags: %w", err)
	}
	b.PutLong(i.ClientID)
	b.PutString(i.Phone)
	b.PutString(i.FirstName)
	b.PutString(i.LastName)
	if i.Flags.Has(0) {
		if err := i.Note.Encode(b); err != nil {
			return fmt.Errorf("unable to encode inputPhoneContact#6a1dc4be: field note: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InputPhoneContact) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPhoneContact#6a1dc4be to nil")
	}
	if err := b.ConsumeID(InputPhoneContactTypeID); err != nil {
		return fmt.Errorf("unable to decode inputPhoneContact#6a1dc4be: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputPhoneContact) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPhoneContact#6a1dc4be to nil")
	}
	{
		if err := i.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode inputPhoneContact#6a1dc4be: field flags: %w", err)
		}
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode inputPhoneContact#6a1dc4be: field client_id: %w", err)
		}
		i.ClientID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputPhoneContact#6a1dc4be: field phone: %w", err)
		}
		i.Phone = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputPhoneContact#6a1dc4be: field first_name: %w", err)
		}
		i.FirstName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inputPhoneContact#6a1dc4be: field last_name: %w", err)
		}
		i.LastName = value
	}
	if i.Flags.Has(0) {
		if err := i.Note.Decode(b); err != nil {
			return fmt.Errorf("unable to decode inputPhoneContact#6a1dc4be: field note: %w", err)
		}
	}
	return nil
}

// GetClientID returns value of ClientID field.
func (i *InputPhoneContact) GetClientID() (value int64) {
	if i == nil {
		return
	}
	return i.ClientID
}

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

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

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

// SetNote sets value of Note conditional field.
func (i *InputPhoneContact) SetNote(value TextWithEntities) {
	i.Flags.Set(0)
	i.Note = value
}

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