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

// CreateChatInviteLinkRequest represents TL type `createChatInviteLink#1126a341`.
type CreateChatInviteLinkRequest struct {
	// Chat identifier
	ChatID int64
	// 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
}

// CreateChatInviteLinkRequestTypeID is TL type id of CreateChatInviteLinkRequest.
const CreateChatInviteLinkRequestTypeID = 0x1126a341

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreateChatInviteLinkRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createChatInviteLink",
		ID:   CreateChatInviteLinkRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			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 (c *CreateChatInviteLinkRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatInviteLink#1126a341 as nil")
	}
	b.PutID(CreateChatInviteLinkRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreateChatInviteLinkRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatInviteLink#1126a341 as nil")
	}
	b.PutInt53(c.ChatID)
	b.PutString(c.Name)
	b.PutInt32(c.ExpirationDate)
	b.PutInt32(c.MemberLimit)
	b.PutBool(c.CreatesJoinRequest)
	return nil
}

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

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

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

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

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

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

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

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

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

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

// CreateChatInviteLink invokes method createChatInviteLink#1126a341 returning error if any.
func (c *Client) CreateChatInviteLink(ctx context.Context, request *CreateChatInviteLinkRequest) (*ChatInviteLink, error) {
	var result ChatInviteLink

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