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

// EditChatFolderInviteLinkRequest represents TL type `editChatFolderInviteLink#5cc415a8`.
type EditChatFolderInviteLinkRequest struct {
	// Chat folder identifier
	ChatFolderID int32
	// Invite link to be edited
	InviteLink string
	// New name of the link; 0-32 characters
	Name string
	// New 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 editing
	ChatIDs []int64
}

// EditChatFolderInviteLinkRequestTypeID is TL type id of EditChatFolderInviteLinkRequest.
const EditChatFolderInviteLinkRequestTypeID = 0x5cc415a8

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EditChatFolderInviteLinkRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "editChatFolderInviteLink",
		ID:   EditChatFolderInviteLinkRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolderID",
			SchemaName: "chat_folder_id",
		},
		{
			Name:       "InviteLink",
			SchemaName: "invite_link",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "ChatIDs",
			SchemaName: "chat_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EditChatFolderInviteLinkRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editChatFolderInviteLink#5cc415a8 as nil")
	}
	b.PutID(EditChatFolderInviteLinkRequestTypeID)
	return e.EncodeBare(b)
}

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

// Decode implements bin.Decoder.
func (e *EditChatFolderInviteLinkRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editChatFolderInviteLink#5cc415a8 to nil")
	}
	if err := b.ConsumeID(EditChatFolderInviteLinkRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode editChatFolderInviteLink#5cc415a8: %w", err)
	}
	return e.DecodeBare(b)
}

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

		if headerLen > 0 {
			e.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 editChatFolderInviteLink#5cc415a8: field chat_ids: %w", err)
			}
			e.ChatIDs = append(e.ChatIDs, value)
		}
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EditChatFolderInviteLinkRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode editChatFolderInviteLink#5cc415a8 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("editChatFolderInviteLink"); err != nil {
				return fmt.Errorf("unable to decode editChatFolderInviteLink#5cc415a8: %w", err)
			}
		case "chat_folder_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode editChatFolderInviteLink#5cc415a8: field chat_folder_id: %w", err)
			}
			e.ChatFolderID = value
		case "invite_link":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode editChatFolderInviteLink#5cc415a8: field invite_link: %w", err)
			}
			e.InviteLink = value
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode editChatFolderInviteLink#5cc415a8: field name: %w", err)
			}
			e.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 editChatFolderInviteLink#5cc415a8: field chat_ids: %w", err)
				}
				e.ChatIDs = append(e.ChatIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode editChatFolderInviteLink#5cc415a8: field chat_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetInviteLink returns value of InviteLink field.
func (e *EditChatFolderInviteLinkRequest) GetInviteLink() (value string) {
	if e == nil {
		return
	}
	return e.InviteLink
}

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

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

// EditChatFolderInviteLink invokes method editChatFolderInviteLink#5cc415a8 returning error if any.
func (c *Client) EditChatFolderInviteLink(ctx context.Context, request *EditChatFolderInviteLinkRequest) (*ChatFolderInviteLink, error) {
	var result ChatFolderInviteLink

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