// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// ContactsResolveUsernameRequest represents TL type `contacts.resolveUsername#f93ccba3`.
// Resolve a @username to get peer info
//
// See https://core.telegram.org/method/contacts.resolveUsername for reference.
type ContactsResolveUsernameRequest struct {
	// @username to resolve
	Username string
}

// ContactsResolveUsernameRequestTypeID is TL type id of ContactsResolveUsernameRequest.
const ContactsResolveUsernameRequestTypeID = 0xf93ccba3

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

func (r *ContactsResolveUsernameRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Username == "") {
		return false
	}

	return true
}

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

// FillFrom fills ContactsResolveUsernameRequest from given interface.
func (r *ContactsResolveUsernameRequest) FillFrom(from interface {
	GetUsername() (value string)
}) {
	r.Username = from.GetUsername()
}

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

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

// TypeInfo returns info about TL type.
func (r *ContactsResolveUsernameRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.resolveUsername",
		ID:   ContactsResolveUsernameRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Username",
			SchemaName: "username",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ContactsResolveUsernameRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode contacts.resolveUsername#f93ccba3 as nil")
	}
	b.PutID(ContactsResolveUsernameRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ContactsResolveUsernameRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode contacts.resolveUsername#f93ccba3 as nil")
	}
	b.PutString(r.Username)
	return nil
}

// Decode implements bin.Decoder.
func (r *ContactsResolveUsernameRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode contacts.resolveUsername#f93ccba3 to nil")
	}
	if err := b.ConsumeID(ContactsResolveUsernameRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.resolveUsername#f93ccba3: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ContactsResolveUsernameRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode contacts.resolveUsername#f93ccba3 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.resolveUsername#f93ccba3: field username: %w", err)
		}
		r.Username = value
	}
	return nil
}

// GetUsername returns value of Username field.
func (r *ContactsResolveUsernameRequest) GetUsername() (value string) {
	if r == nil {
		return
	}
	return r.Username
}

// ContactsResolveUsername invokes method contacts.resolveUsername#f93ccba3 returning error if any.
// Resolve a @username to get peer info
//
// Possible errors:
//
//	400 CONNECTION_LAYER_INVALID: Layer invalid.
//	400 USERNAME_INVALID: The provided username is not valid.
//	400 USERNAME_NOT_OCCUPIED: The provided username is not occupied.
//
// See https://core.telegram.org/method/contacts.resolveUsername for reference.
// Can be used by bots.
func (c *Client) ContactsResolveUsername(ctx context.Context, username string) (*ContactsResolvedPeer, error) {
	var result ContactsResolvedPeer

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