// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// MessagesCreateChatRequest represents TL type `messages.createChat#34a818`.
// Creates a new chat.
// May also return 0-N updates of type updateGroupInvitePrivacyForbidden¹: it indicates
// we couldn't add a user to a chat because of their privacy settings; if required, an
// invite link² can be shared with the user, instead.
//
// Links:
//  1. https://core.telegram.org/constructor/updateGroupInvitePrivacyForbidden
//  2. https://core.telegram.org/api/invites
//
// See https://core.telegram.org/method/messages.createChat for reference.
type MessagesCreateChatRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// List of user IDs to be invited
	Users []InputUserClass
	// Chat name
	Title string
	// Time-to-live of all messages that will be sent in the chat: once message.date+message
	// ttl_period === time(), the message will be deleted on the server, and must be deleted
	// locally as well. You can use messages.setDefaultHistoryTTL¹ to edit this value later.
	//
	// Links:
	//  1) https://core.telegram.org/method/messages.setDefaultHistoryTTL
	//
	// Use SetTTLPeriod and GetTTLPeriod helpers.
	TTLPeriod int
}

// MessagesCreateChatRequestTypeID is TL type id of MessagesCreateChatRequest.
const MessagesCreateChatRequestTypeID = 0x34a818

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

func (c *MessagesCreateChatRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.Users == nil) {
		return false
	}
	if !(c.Title == "") {
		return false
	}
	if !(c.TTLPeriod == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesCreateChatRequest from given interface.
func (c *MessagesCreateChatRequest) FillFrom(from interface {
	GetUsers() (value []InputUserClass)
	GetTitle() (value string)
	GetTTLPeriod() (value int, ok bool)
}) {
	c.Users = from.GetUsers()
	c.Title = from.GetTitle()
	if val, ok := from.GetTTLPeriod(); ok {
		c.TTLPeriod = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*MessagesCreateChatRequest) TypeName() string {
	return "messages.createChat"
}

// TypeInfo returns info about TL type.
func (c *MessagesCreateChatRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.createChat",
		ID:   MessagesCreateChatRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Users",
			SchemaName: "users",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "TTLPeriod",
			SchemaName: "ttl_period",
			Null:       !c.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *MessagesCreateChatRequest) SetFlags() {
	if !(c.TTLPeriod == 0) {
		c.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (c *MessagesCreateChatRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode messages.createChat#34a818 as nil")
	}
	b.PutID(MessagesCreateChatRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *MessagesCreateChatRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode messages.createChat#34a818 as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.createChat#34a818: field flags: %w", err)
	}
	b.PutVectorHeader(len(c.Users))
	for idx, v := range c.Users {
		if v == nil {
			return fmt.Errorf("unable to encode messages.createChat#34a818: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.createChat#34a818: field users element with index %d: %w", idx, err)
		}
	}
	b.PutString(c.Title)
	if c.Flags.Has(0) {
		b.PutInt(c.TTLPeriod)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *MessagesCreateChatRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode messages.createChat#34a818 to nil")
	}
	if err := b.ConsumeID(MessagesCreateChatRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.createChat#34a818: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *MessagesCreateChatRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode messages.createChat#34a818 to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.createChat#34a818: field flags: %w", err)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.createChat#34a818: field users: %w", err)
		}

		if headerLen > 0 {
			c.Users = make([]InputUserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.createChat#34a818: field users: %w", err)
			}
			c.Users = append(c.Users, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.createChat#34a818: field title: %w", err)
		}
		c.Title = value
	}
	if c.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.createChat#34a818: field ttl_period: %w", err)
		}
		c.TTLPeriod = value
	}
	return nil
}

// GetUsers returns value of Users field.
func (c *MessagesCreateChatRequest) GetUsers() (value []InputUserClass) {
	if c == nil {
		return
	}
	return c.Users
}

// GetTitle returns value of Title field.
func (c *MessagesCreateChatRequest) GetTitle() (value string) {
	if c == nil {
		return
	}
	return c.Title
}

// SetTTLPeriod sets value of TTLPeriod conditional field.
func (c *MessagesCreateChatRequest) SetTTLPeriod(value int) {
	c.Flags.Set(0)
	c.TTLPeriod = value
}

// GetTTLPeriod returns value of TTLPeriod conditional field and
// boolean which is true if field was set.
func (c *MessagesCreateChatRequest) GetTTLPeriod() (value int, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(0) {
		return value, false
	}
	return c.TTLPeriod, true
}

// MapUsers returns field Users wrapped in InputUserClassArray helper.
func (c *MessagesCreateChatRequest) MapUsers() (value InputUserClassArray) {
	return InputUserClassArray(c.Users)
}

// MessagesCreateChat invokes method messages.createChat#34a818 returning error if any.
// Creates a new chat.
// May also return 0-N updates of type updateGroupInvitePrivacyForbidden¹: it indicates
// we couldn't add a user to a chat because of their privacy settings; if required, an
// invite link² can be shared with the user, instead.
//
// Links:
//  1. https://core.telegram.org/constructor/updateGroupInvitePrivacyForbidden
//  2. https://core.telegram.org/api/invites
//
// Possible errors:
//
//	500 CHAT_ID_GENERATE_FAILED: Failure while generating the chat ID.
//	400 CHAT_INVALID: Invalid chat.
//	400 CHAT_TITLE_EMPTY: No chat title provided.
//	400 INPUT_USER_DEACTIVATED: The specified user was deleted.
//	400 USERS_TOO_FEW: Not enough users (to create a chat, for example).
//	406 USER_RESTRICTED: You're spamreported, you can't create channels or chats.
//
// See https://core.telegram.org/method/messages.createChat for reference.
func (c *Client) MessagesCreateChat(ctx context.Context, request *MessagesCreateChatRequest) (UpdatesClass, error) {
	var result UpdatesBox

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