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

// BotsCheckUsernameRequest represents TL type `bots.checkUsername#87f2219b`.
//
// See https://core.telegram.org/method/bots.checkUsername for reference.
type BotsCheckUsernameRequest struct {
	// Username field of BotsCheckUsernameRequest.
	Username string
}

// BotsCheckUsernameRequestTypeID is TL type id of BotsCheckUsernameRequest.
const BotsCheckUsernameRequestTypeID = 0x87f2219b

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

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

	return true
}

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

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

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

// TypeName returns name of type in TL schema.
func (*BotsCheckUsernameRequest) TypeName() string {
	return "bots.checkUsername"
}

// TypeInfo returns info about TL type.
func (c *BotsCheckUsernameRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.checkUsername",
		ID:   BotsCheckUsernameRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Username",
			SchemaName: "username",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *BotsCheckUsernameRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode bots.checkUsername#87f2219b as nil")
	}
	b.PutID(BotsCheckUsernameRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *BotsCheckUsernameRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode bots.checkUsername#87f2219b as nil")
	}
	b.PutString(c.Username)
	return nil
}

// Decode implements bin.Decoder.
func (c *BotsCheckUsernameRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode bots.checkUsername#87f2219b to nil")
	}
	if err := b.ConsumeID(BotsCheckUsernameRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.checkUsername#87f2219b: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *BotsCheckUsernameRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode bots.checkUsername#87f2219b to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.checkUsername#87f2219b: field username: %w", err)
		}
		c.Username = value
	}
	return nil
}

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

// BotsCheckUsername invokes method bots.checkUsername#87f2219b returning error if any.
//
// See https://core.telegram.org/method/bots.checkUsername for reference.
func (c *Client) BotsCheckUsername(ctx context.Context, username string) (bool, error) {
	var result BoolBox

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