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

// ContactsUpdateContactNoteRequest represents TL type `contacts.updateContactNote#139f63fb`.
//
// See https://core.telegram.org/method/contacts.updateContactNote for reference.
type ContactsUpdateContactNoteRequest struct {
	// ID field of ContactsUpdateContactNoteRequest.
	ID InputUserClass
	// Note field of ContactsUpdateContactNoteRequest.
	Note TextWithEntities
}

// ContactsUpdateContactNoteRequestTypeID is TL type id of ContactsUpdateContactNoteRequest.
const ContactsUpdateContactNoteRequestTypeID = 0x139f63fb

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

func (u *ContactsUpdateContactNoteRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.ID == nil) {
		return false
	}
	if !(u.Note.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills ContactsUpdateContactNoteRequest from given interface.
func (u *ContactsUpdateContactNoteRequest) FillFrom(from interface {
	GetID() (value InputUserClass)
	GetNote() (value TextWithEntities)
}) {
	u.ID = from.GetID()
	u.Note = from.GetNote()
}

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

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

// TypeInfo returns info about TL type.
func (u *ContactsUpdateContactNoteRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.updateContactNote",
		ID:   ContactsUpdateContactNoteRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Note",
			SchemaName: "note",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (u *ContactsUpdateContactNoteRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode contacts.updateContactNote#139f63fb as nil")
	}
	b.PutID(ContactsUpdateContactNoteRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *ContactsUpdateContactNoteRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode contacts.updateContactNote#139f63fb as nil")
	}
	if u.ID == nil {
		return fmt.Errorf("unable to encode contacts.updateContactNote#139f63fb: field id is nil")
	}
	if err := u.ID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode contacts.updateContactNote#139f63fb: field id: %w", err)
	}
	if err := u.Note.Encode(b); err != nil {
		return fmt.Errorf("unable to encode contacts.updateContactNote#139f63fb: field note: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *ContactsUpdateContactNoteRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode contacts.updateContactNote#139f63fb to nil")
	}
	if err := b.ConsumeID(ContactsUpdateContactNoteRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.updateContactNote#139f63fb: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *ContactsUpdateContactNoteRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode contacts.updateContactNote#139f63fb to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode contacts.updateContactNote#139f63fb: field id: %w", err)
		}
		u.ID = value
	}
	{
		if err := u.Note.Decode(b); err != nil {
			return fmt.Errorf("unable to decode contacts.updateContactNote#139f63fb: field note: %w", err)
		}
	}
	return nil
}

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

// GetNote returns value of Note field.
func (u *ContactsUpdateContactNoteRequest) GetNote() (value TextWithEntities) {
	if u == nil {
		return
	}
	return u.Note
}

// ContactsUpdateContactNote invokes method contacts.updateContactNote#139f63fb returning error if any.
//
// See https://core.telegram.org/method/contacts.updateContactNote for reference.
func (c *Client) ContactsUpdateContactNote(ctx context.Context, request *ContactsUpdateContactNoteRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
