// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// CheckChatUsernameRequest represents TL type `checkChatUsername#f8e66210`.
type CheckChatUsernameRequest struct {
	// Chat identifier; must be identifier of a supergroup chat, or a channel chat, or a
	// private chat with self, or 0 if the chat is being created
	ChatID int64
	// Username to be checked
	Username string
}

// CheckChatUsernameRequestTypeID is TL type id of CheckChatUsernameRequest.
const CheckChatUsernameRequestTypeID = 0xf8e66210

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (c *CheckChatUsernameRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkChatUsername#f8e66210 as nil")
	}
	b.PutID(CheckChatUsernameRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CheckChatUsernameRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkChatUsername#f8e66210 as nil")
	}
	b.PutInt53(c.ChatID)
	b.PutString(c.Username)
	return nil
}

// Decode implements bin.Decoder.
func (c *CheckChatUsernameRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode checkChatUsername#f8e66210 to nil")
	}
	if err := b.ConsumeID(CheckChatUsernameRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode checkChatUsername#f8e66210: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CheckChatUsernameRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode checkChatUsername#f8e66210 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode checkChatUsername#f8e66210: field chat_id: %w", err)
		}
		c.ChatID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode checkChatUsername#f8e66210: field username: %w", err)
		}
		c.Username = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CheckChatUsernameRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode checkChatUsername#f8e66210 as nil")
	}
	b.ObjStart()
	b.PutID("checkChatUsername")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(c.ChatID)
	b.Comma()
	b.FieldStart("username")
	b.PutString(c.Username)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CheckChatUsernameRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode checkChatUsername#f8e66210 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("checkChatUsername"); err != nil {
				return fmt.Errorf("unable to decode checkChatUsername#f8e66210: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode checkChatUsername#f8e66210: field chat_id: %w", err)
			}
			c.ChatID = value
		case "username":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode checkChatUsername#f8e66210: field username: %w", err)
			}
			c.Username = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (c *CheckChatUsernameRequest) GetChatID() (value int64) {
	if c == nil {
		return
	}
	return c.ChatID
}

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

// CheckChatUsername invokes method checkChatUsername#f8e66210 returning error if any.
func (c *Client) CheckChatUsername(ctx context.Context, request *CheckChatUsernameRequest) (CheckChatUsernameResultClass, error) {
	var result CheckChatUsernameResultBox

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