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

// CreateNewSupergroupChatRequest represents TL type `createNewSupergroupChat#2fecf6c6`.
type CreateNewSupergroupChatRequest struct {
	// Title of the new chat; 1-128 characters
	Title string
	// Pass true to create a forum supergroup chat
	IsForum bool
	// Pass true to create a channel chat; ignored if a forum is created
	IsChannel bool
	// Creates a new supergroup or channel and sends a corresponding
	// messageSupergroupChatCreate. Returns the newly created chat
	Description string
	// Chat location if a location-based supergroup is being created; pass null to create an
	// ordinary supergroup chat
	Location ChatLocation
	// 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
	// Pass true to create a supergroup for importing messages using importMessages
	ForImport bool
}

// CreateNewSupergroupChatRequestTypeID is TL type id of CreateNewSupergroupChatRequest.
const CreateNewSupergroupChatRequestTypeID = 0x2fecf6c6

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

func (c *CreateNewSupergroupChatRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Title == "") {
		return false
	}
	if !(c.IsForum == false) {
		return false
	}
	if !(c.IsChannel == false) {
		return false
	}
	if !(c.Description == "") {
		return false
	}
	if !(c.Location.Zero()) {
		return false
	}
	if !(c.MessageAutoDeleteTime == 0) {
		return false
	}
	if !(c.ForImport == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreateNewSupergroupChatRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createNewSupergroupChat",
		ID:   CreateNewSupergroupChatRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "IsForum",
			SchemaName: "is_forum",
		},
		{
			Name:       "IsChannel",
			SchemaName: "is_channel",
		},
		{
			Name:       "Description",
			SchemaName: "description",
		},
		{
			Name:       "Location",
			SchemaName: "location",
		},
		{
			Name:       "MessageAutoDeleteTime",
			SchemaName: "message_auto_delete_time",
		},
		{
			Name:       "ForImport",
			SchemaName: "for_import",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CreateNewSupergroupChatRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createNewSupergroupChat#2fecf6c6 as nil")
	}
	b.PutID(CreateNewSupergroupChatRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreateNewSupergroupChatRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createNewSupergroupChat#2fecf6c6 as nil")
	}
	b.PutString(c.Title)
	b.PutBool(c.IsForum)
	b.PutBool(c.IsChannel)
	b.PutString(c.Description)
	if err := c.Location.Encode(b); err != nil {
		return fmt.Errorf("unable to encode createNewSupergroupChat#2fecf6c6: field location: %w", err)
	}
	b.PutInt32(c.MessageAutoDeleteTime)
	b.PutBool(c.ForImport)
	return nil
}

// Decode implements bin.Decoder.
func (c *CreateNewSupergroupChatRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createNewSupergroupChat#2fecf6c6 to nil")
	}
	if err := b.ConsumeID(CreateNewSupergroupChatRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CreateNewSupergroupChatRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createNewSupergroupChat#2fecf6c6 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field title: %w", err)
		}
		c.Title = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field is_forum: %w", err)
		}
		c.IsForum = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field is_channel: %w", err)
		}
		c.IsChannel = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field description: %w", err)
		}
		c.Description = value
	}
	{
		if err := c.Location.Decode(b); err != nil {
			return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field location: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field message_auto_delete_time: %w", err)
		}
		c.MessageAutoDeleteTime = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field for_import: %w", err)
		}
		c.ForImport = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CreateNewSupergroupChatRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode createNewSupergroupChat#2fecf6c6 as nil")
	}
	b.ObjStart()
	b.PutID("createNewSupergroupChat")
	b.Comma()
	b.FieldStart("title")
	b.PutString(c.Title)
	b.Comma()
	b.FieldStart("is_forum")
	b.PutBool(c.IsForum)
	b.Comma()
	b.FieldStart("is_channel")
	b.PutBool(c.IsChannel)
	b.Comma()
	b.FieldStart("description")
	b.PutString(c.Description)
	b.Comma()
	b.FieldStart("location")
	if err := c.Location.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode createNewSupergroupChat#2fecf6c6: field location: %w", err)
	}
	b.Comma()
	b.FieldStart("message_auto_delete_time")
	b.PutInt32(c.MessageAutoDeleteTime)
	b.Comma()
	b.FieldStart("for_import")
	b.PutBool(c.ForImport)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CreateNewSupergroupChatRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode createNewSupergroupChat#2fecf6c6 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("createNewSupergroupChat"); err != nil {
				return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: %w", err)
			}
		case "title":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field title: %w", err)
			}
			c.Title = value
		case "is_forum":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field is_forum: %w", err)
			}
			c.IsForum = value
		case "is_channel":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field is_channel: %w", err)
			}
			c.IsChannel = value
		case "description":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field description: %w", err)
			}
			c.Description = value
		case "location":
			if err := c.Location.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field location: %w", err)
			}
		case "message_auto_delete_time":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field message_auto_delete_time: %w", err)
			}
			c.MessageAutoDeleteTime = value
		case "for_import":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode createNewSupergroupChat#2fecf6c6: field for_import: %w", err)
			}
			c.ForImport = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetIsForum returns value of IsForum field.
func (c *CreateNewSupergroupChatRequest) GetIsForum() (value bool) {
	if c == nil {
		return
	}
	return c.IsForum
}

// GetIsChannel returns value of IsChannel field.
func (c *CreateNewSupergroupChatRequest) GetIsChannel() (value bool) {
	if c == nil {
		return
	}
	return c.IsChannel
}

// GetDescription returns value of Description field.
func (c *CreateNewSupergroupChatRequest) GetDescription() (value string) {
	if c == nil {
		return
	}
	return c.Description
}

// GetLocation returns value of Location field.
func (c *CreateNewSupergroupChatRequest) GetLocation() (value ChatLocation) {
	if c == nil {
		return
	}
	return c.Location
}

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

// GetForImport returns value of ForImport field.
func (c *CreateNewSupergroupChatRequest) GetForImport() (value bool) {
	if c == nil {
		return
	}
	return c.ForImport
}

// CreateNewSupergroupChat invokes method createNewSupergroupChat#2fecf6c6 returning error if any.
func (c *Client) CreateNewSupergroupChat(ctx context.Context, request *CreateNewSupergroupChatRequest) (*Chat, error) {
	var result Chat

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