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

// CreateChatFolderInviteLinkRequest represents TL type `createChatFolderInviteLink#fba5e198`.
type CreateChatFolderInviteLinkRequest struct {
	// Chat folder identifier
	ChatFolderID int32
	// Name of the link; 0-32 characters
	Name string
	// Identifiers of chats to be accessible by the invite link. Use
	// getChatsForChatFolderInviteLink to get suitable chats. Basic groups will be
	// automatically converted to supergroups before link creation
	ChatIDs []int64
}

// CreateChatFolderInviteLinkRequestTypeID is TL type id of CreateChatFolderInviteLinkRequest.
const CreateChatFolderInviteLinkRequestTypeID = 0xfba5e198

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

func (c *CreateChatFolderInviteLinkRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ChatFolderID == 0) {
		return false
	}
	if !(c.Name == "") {
		return false
	}
	if !(c.ChatIDs == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreateChatFolderInviteLinkRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createChatFolderInviteLink",
		ID:   CreateChatFolderInviteLinkRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolderID",
			SchemaName: "chat_folder_id",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "ChatIDs",
			SchemaName: "chat_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CreateChatFolderInviteLinkRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatFolderInviteLink#fba5e198 as nil")
	}
	b.PutID(CreateChatFolderInviteLinkRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreateChatFolderInviteLinkRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatFolderInviteLink#fba5e198 as nil")
	}
	b.PutInt32(c.ChatFolderID)
	b.PutString(c.Name)
	b.PutInt(len(c.ChatIDs))
	for _, v := range c.ChatIDs {
		b.PutInt53(v)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CreateChatFolderInviteLinkRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createChatFolderInviteLink#fba5e198 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: field chat_folder_id: %w", err)
		}
		c.ChatFolderID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: field name: %w", err)
		}
		c.Name = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: field chat_ids: %w", err)
		}

		if headerLen > 0 {
			c.ChatIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: field chat_ids: %w", err)
			}
			c.ChatIDs = append(c.ChatIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CreateChatFolderInviteLinkRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatFolderInviteLink#fba5e198 as nil")
	}
	b.ObjStart()
	b.PutID("createChatFolderInviteLink")
	b.Comma()
	b.FieldStart("chat_folder_id")
	b.PutInt32(c.ChatFolderID)
	b.Comma()
	b.FieldStart("name")
	b.PutString(c.Name)
	b.Comma()
	b.FieldStart("chat_ids")
	b.ArrStart()
	for _, v := range c.ChatIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("createChatFolderInviteLink"); err != nil {
				return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: %w", err)
			}
		case "chat_folder_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: field chat_folder_id: %w", err)
			}
			c.ChatFolderID = value
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: field name: %w", err)
			}
			c.Name = value
		case "chat_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: field chat_ids: %w", err)
				}
				c.ChatIDs = append(c.ChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode createChatFolderInviteLink#fba5e198: field chat_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatFolderID returns value of ChatFolderID field.
func (c *CreateChatFolderInviteLinkRequest) GetChatFolderID() (value int32) {
	if c == nil {
		return
	}
	return c.ChatFolderID
}

// GetName returns value of Name field.
func (c *CreateChatFolderInviteLinkRequest) GetName() (value string) {
	if c == nil {
		return
	}
	return c.Name
}

// GetChatIDs returns value of ChatIDs field.
func (c *CreateChatFolderInviteLinkRequest) GetChatIDs() (value []int64) {
	if c == nil {
		return
	}
	return c.ChatIDs
}

// CreateChatFolderInviteLink invokes method createChatFolderInviteLink#fba5e198 returning error if any.
func (c *Client) CreateChatFolderInviteLink(ctx context.Context, request *CreateChatFolderInviteLinkRequest) (*ChatFolderInviteLink, error) {
	var result ChatFolderInviteLink

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