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

// EditChatInviteLinkRequest represents TL type `editChatInviteLink#4eb23d7c`.
type EditChatInviteLinkRequest struct {
	// Chat identifier
	ChatID int64
	// Invite link to be edited
	InviteLink string
	// Invite link name; 0-32 characters
	Name string
	// Point in time (Unix timestamp) when the link will expire; pass 0 if never
	ExpirationDate int32
	// The maximum number of chat members that can join the chat via the link simultaneously;
	// 0-99999; pass 0 if not limited
	MemberLimit int32
	// Pass true if users joining the chat via the link need to be approved by chat
	// administrators. In this case, member_limit must be 0
	CreatesJoinRequest bool
}

// EditChatInviteLinkRequestTypeID is TL type id of EditChatInviteLinkRequest.
const EditChatInviteLinkRequestTypeID = 0x4eb23d7c

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

func (e *EditChatInviteLinkRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.ChatID == 0) {
		return false
	}
	if !(e.InviteLink == "") {
		return false
	}
	if !(e.Name == "") {
		return false
	}
	if !(e.ExpirationDate == 0) {
		return false
	}
	if !(e.MemberLimit == 0) {
		return false
	}
	if !(e.CreatesJoinRequest == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EditChatInviteLinkRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "editChatInviteLink",
		ID:   EditChatInviteLinkRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "InviteLink",
			SchemaName: "invite_link",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "ExpirationDate",
			SchemaName: "expiration_date",
		},
		{
			Name:       "MemberLimit",
			SchemaName: "member_limit",
		},
		{
			Name:       "CreatesJoinRequest",
			SchemaName: "creates_join_request",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EditChatInviteLinkRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editChatInviteLink#4eb23d7c as nil")
	}
	b.PutID(EditChatInviteLinkRequestTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EditChatInviteLinkRequest) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editChatInviteLink#4eb23d7c as nil")
	}
	b.PutInt53(e.ChatID)
	b.PutString(e.InviteLink)
	b.PutString(e.Name)
	b.PutInt32(e.ExpirationDate)
	b.PutInt32(e.MemberLimit)
	b.PutBool(e.CreatesJoinRequest)
	return nil
}

// Decode implements bin.Decoder.
func (e *EditChatInviteLinkRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editChatInviteLink#4eb23d7c to nil")
	}
	if err := b.ConsumeID(EditChatInviteLinkRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EditChatInviteLinkRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editChatInviteLink#4eb23d7c to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field chat_id: %w", err)
		}
		e.ChatID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field invite_link: %w", err)
		}
		e.InviteLink = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field name: %w", err)
		}
		e.Name = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field expiration_date: %w", err)
		}
		e.ExpirationDate = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field member_limit: %w", err)
		}
		e.MemberLimit = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field creates_join_request: %w", err)
		}
		e.CreatesJoinRequest = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EditChatInviteLinkRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode editChatInviteLink#4eb23d7c as nil")
	}
	b.ObjStart()
	b.PutID("editChatInviteLink")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(e.ChatID)
	b.Comma()
	b.FieldStart("invite_link")
	b.PutString(e.InviteLink)
	b.Comma()
	b.FieldStart("name")
	b.PutString(e.Name)
	b.Comma()
	b.FieldStart("expiration_date")
	b.PutInt32(e.ExpirationDate)
	b.Comma()
	b.FieldStart("member_limit")
	b.PutInt32(e.MemberLimit)
	b.Comma()
	b.FieldStart("creates_join_request")
	b.PutBool(e.CreatesJoinRequest)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EditChatInviteLinkRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode editChatInviteLink#4eb23d7c to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("editChatInviteLink"); err != nil {
				return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field chat_id: %w", err)
			}
			e.ChatID = value
		case "invite_link":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field invite_link: %w", err)
			}
			e.InviteLink = value
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field name: %w", err)
			}
			e.Name = value
		case "expiration_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field expiration_date: %w", err)
			}
			e.ExpirationDate = value
		case "member_limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field member_limit: %w", err)
			}
			e.MemberLimit = value
		case "creates_join_request":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode editChatInviteLink#4eb23d7c: field creates_join_request: %w", err)
			}
			e.CreatesJoinRequest = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

// GetExpirationDate returns value of ExpirationDate field.
func (e *EditChatInviteLinkRequest) GetExpirationDate() (value int32) {
	if e == nil {
		return
	}
	return e.ExpirationDate
}

// GetMemberLimit returns value of MemberLimit field.
func (e *EditChatInviteLinkRequest) GetMemberLimit() (value int32) {
	if e == nil {
		return
	}
	return e.MemberLimit
}

// GetCreatesJoinRequest returns value of CreatesJoinRequest field.
func (e *EditChatInviteLinkRequest) GetCreatesJoinRequest() (value bool) {
	if e == nil {
		return
	}
	return e.CreatesJoinRequest
}

// EditChatInviteLink invokes method editChatInviteLink#4eb23d7c returning error if any.
func (c *Client) EditChatInviteLink(ctx context.Context, request *EditChatInviteLinkRequest) (*ChatInviteLink, error) {
	var result ChatInviteLink

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