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

// CreateNewBasicGroupChatRequest represents TL type `createNewBasicGroupChat#8b9517af`.
type CreateNewBasicGroupChatRequest struct {
	// Identifiers of users to be added to the basic group; may be empty to create a basic
	// group without other members
	UserIDs []int64
	// Title of the new basic group; 1-128 characters
	Title string
	// Message auto-delete time value, in seconds; must be from 0 up to 365 * 86400 and be
	// divisible by 86400. If 0, then messages aren't deleted automatically
	MessageAutoDeleteTime int32
}

// CreateNewBasicGroupChatRequestTypeID is TL type id of CreateNewBasicGroupChatRequest.
const CreateNewBasicGroupChatRequestTypeID = 0x8b9517af

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

func (c *CreateNewBasicGroupChatRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.UserIDs == nil) {
		return false
	}
	if !(c.Title == "") {
		return false
	}
	if !(c.MessageAutoDeleteTime == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreateNewBasicGroupChatRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createNewBasicGroupChat",
		ID:   CreateNewBasicGroupChatRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserIDs",
			SchemaName: "user_ids",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "MessageAutoDeleteTime",
			SchemaName: "message_auto_delete_time",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CreateNewBasicGroupChatRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createNewBasicGroupChat#8b9517af as nil")
	}
	b.PutID(CreateNewBasicGroupChatRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreateNewBasicGroupChatRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createNewBasicGroupChat#8b9517af as nil")
	}
	b.PutInt(len(c.UserIDs))
	for _, v := range c.UserIDs {
		b.PutInt53(v)
	}
	b.PutString(c.Title)
	b.PutInt32(c.MessageAutoDeleteTime)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CreateNewBasicGroupChatRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createNewBasicGroupChat#8b9517af to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode createNewBasicGroupChat#8b9517af: field user_ids: %w", err)
		}

		if headerLen > 0 {
			c.UserIDs = 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 createNewBasicGroupChat#8b9517af: field user_ids: %w", err)
			}
			c.UserIDs = append(c.UserIDs, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode createNewBasicGroupChat#8b9517af: field title: %w", err)
		}
		c.Title = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode createNewBasicGroupChat#8b9517af: field message_auto_delete_time: %w", err)
		}
		c.MessageAutoDeleteTime = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CreateNewBasicGroupChatRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode createNewBasicGroupChat#8b9517af as nil")
	}
	b.ObjStart()
	b.PutID("createNewBasicGroupChat")
	b.Comma()
	b.FieldStart("user_ids")
	b.ArrStart()
	for _, v := range c.UserIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("title")
	b.PutString(c.Title)
	b.Comma()
	b.FieldStart("message_auto_delete_time")
	b.PutInt32(c.MessageAutoDeleteTime)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("createNewBasicGroupChat"); err != nil {
				return fmt.Errorf("unable to decode createNewBasicGroupChat#8b9517af: %w", err)
			}
		case "user_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode createNewBasicGroupChat#8b9517af: field user_ids: %w", err)
				}
				c.UserIDs = append(c.UserIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode createNewBasicGroupChat#8b9517af: field user_ids: %w", err)
			}
		case "title":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode createNewBasicGroupChat#8b9517af: field title: %w", err)
			}
			c.Title = value
		case "message_auto_delete_time":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode createNewBasicGroupChat#8b9517af: field message_auto_delete_time: %w", err)
			}
			c.MessageAutoDeleteTime = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserIDs returns value of UserIDs field.
func (c *CreateNewBasicGroupChatRequest) GetUserIDs() (value []int64) {
	if c == nil {
		return
	}
	return c.UserIDs
}

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

// GetMessageAutoDeleteTime returns value of MessageAutoDeleteTime field.
func (c *CreateNewBasicGroupChatRequest) GetMessageAutoDeleteTime() (value int32) {
	if c == nil {
		return
	}
	return c.MessageAutoDeleteTime
}

// CreateNewBasicGroupChat invokes method createNewBasicGroupChat#8b9517af returning error if any.
func (c *Client) CreateNewBasicGroupChat(ctx context.Context, request *CreateNewBasicGroupChatRequest) (*CreatedBasicGroupChat, error) {
	var result CreatedBasicGroupChat

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