// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesCreateForumTopicRequest represents TL type `messages.createForumTopic#2f98c3d5`.
//
// See https://core.telegram.org/method/messages.createForumTopic for reference.
type MessagesCreateForumTopicRequest struct {
	// Flags field of MessagesCreateForumTopicRequest.
	Flags bin.Fields
	// TitleMissing field of MessagesCreateForumTopicRequest.
	TitleMissing bool
	// Peer field of MessagesCreateForumTopicRequest.
	Peer InputPeerClass
	// Title field of MessagesCreateForumTopicRequest.
	Title string
	// IconColor field of MessagesCreateForumTopicRequest.
	//
	// Use SetIconColor and GetIconColor helpers.
	IconColor int
	// IconEmojiID field of MessagesCreateForumTopicRequest.
	//
	// Use SetIconEmojiID and GetIconEmojiID helpers.
	IconEmojiID int64
	// RandomID field of MessagesCreateForumTopicRequest.
	RandomID int64
	// SendAs field of MessagesCreateForumTopicRequest.
	//
	// Use SetSendAs and GetSendAs helpers.
	SendAs InputPeerClass
}

// MessagesCreateForumTopicRequestTypeID is TL type id of MessagesCreateForumTopicRequest.
const MessagesCreateForumTopicRequestTypeID = 0x2f98c3d5

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

func (c *MessagesCreateForumTopicRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.TitleMissing == false) {
		return false
	}
	if !(c.Peer == nil) {
		return false
	}
	if !(c.Title == "") {
		return false
	}
	if !(c.IconColor == 0) {
		return false
	}
	if !(c.IconEmojiID == 0) {
		return false
	}
	if !(c.RandomID == 0) {
		return false
	}
	if !(c.SendAs == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesCreateForumTopicRequest from given interface.
func (c *MessagesCreateForumTopicRequest) FillFrom(from interface {
	GetTitleMissing() (value bool)
	GetPeer() (value InputPeerClass)
	GetTitle() (value string)
	GetIconColor() (value int, ok bool)
	GetIconEmojiID() (value int64, ok bool)
	GetRandomID() (value int64)
	GetSendAs() (value InputPeerClass, ok bool)
}) {
	c.TitleMissing = from.GetTitleMissing()
	c.Peer = from.GetPeer()
	c.Title = from.GetTitle()
	if val, ok := from.GetIconColor(); ok {
		c.IconColor = val
	}

	if val, ok := from.GetIconEmojiID(); ok {
		c.IconEmojiID = val
	}

	c.RandomID = from.GetRandomID()
	if val, ok := from.GetSendAs(); ok {
		c.SendAs = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (c *MessagesCreateForumTopicRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.createForumTopic",
		ID:   MessagesCreateForumTopicRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TitleMissing",
			SchemaName: "title_missing",
			Null:       !c.Flags.Has(4),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "IconColor",
			SchemaName: "icon_color",
			Null:       !c.Flags.Has(0),
		},
		{
			Name:       "IconEmojiID",
			SchemaName: "icon_emoji_id",
			Null:       !c.Flags.Has(3),
		},
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
		{
			Name:       "SendAs",
			SchemaName: "send_as",
			Null:       !c.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *MessagesCreateForumTopicRequest) SetFlags() {
	if !(c.TitleMissing == false) {
		c.Flags.Set(4)
	}
	if !(c.IconColor == 0) {
		c.Flags.Set(0)
	}
	if !(c.IconEmojiID == 0) {
		c.Flags.Set(3)
	}
	if !(c.SendAs == nil) {
		c.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (c *MessagesCreateForumTopicRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode messages.createForumTopic#2f98c3d5 as nil")
	}
	b.PutID(MessagesCreateForumTopicRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *MessagesCreateForumTopicRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode messages.createForumTopic#2f98c3d5 as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.createForumTopic#2f98c3d5: field flags: %w", err)
	}
	if c.Peer == nil {
		return fmt.Errorf("unable to encode messages.createForumTopic#2f98c3d5: field peer is nil")
	}
	if err := c.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.createForumTopic#2f98c3d5: field peer: %w", err)
	}
	b.PutString(c.Title)
	if c.Flags.Has(0) {
		b.PutInt(c.IconColor)
	}
	if c.Flags.Has(3) {
		b.PutLong(c.IconEmojiID)
	}
	b.PutLong(c.RandomID)
	if c.Flags.Has(2) {
		if c.SendAs == nil {
			return fmt.Errorf("unable to encode messages.createForumTopic#2f98c3d5: field send_as is nil")
		}
		if err := c.SendAs.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.createForumTopic#2f98c3d5: field send_as: %w", err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *MessagesCreateForumTopicRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode messages.createForumTopic#2f98c3d5 to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.createForumTopic#2f98c3d5: field flags: %w", err)
		}
	}
	c.TitleMissing = c.Flags.Has(4)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.createForumTopic#2f98c3d5: field peer: %w", err)
		}
		c.Peer = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.createForumTopic#2f98c3d5: 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.createForumTopic#2f98c3d5: field icon_color: %w", err)
		}
		c.IconColor = value
	}
	if c.Flags.Has(3) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.createForumTopic#2f98c3d5: field icon_emoji_id: %w", err)
		}
		c.IconEmojiID = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.createForumTopic#2f98c3d5: field random_id: %w", err)
		}
		c.RandomID = value
	}
	if c.Flags.Has(2) {
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.createForumTopic#2f98c3d5: field send_as: %w", err)
		}
		c.SendAs = value
	}
	return nil
}

// SetTitleMissing sets value of TitleMissing conditional field.
func (c *MessagesCreateForumTopicRequest) SetTitleMissing(value bool) {
	if value {
		c.Flags.Set(4)
		c.TitleMissing = true
	} else {
		c.Flags.Unset(4)
		c.TitleMissing = false
	}
}

// GetTitleMissing returns value of TitleMissing conditional field.
func (c *MessagesCreateForumTopicRequest) GetTitleMissing() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(4)
}

// GetPeer returns value of Peer field.
func (c *MessagesCreateForumTopicRequest) GetPeer() (value InputPeerClass) {
	if c == nil {
		return
	}
	return c.Peer
}

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

// SetIconColor sets value of IconColor conditional field.
func (c *MessagesCreateForumTopicRequest) SetIconColor(value int) {
	c.Flags.Set(0)
	c.IconColor = value
}

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

// SetIconEmojiID sets value of IconEmojiID conditional field.
func (c *MessagesCreateForumTopicRequest) SetIconEmojiID(value int64) {
	c.Flags.Set(3)
	c.IconEmojiID = value
}

// GetIconEmojiID returns value of IconEmojiID conditional field and
// boolean which is true if field was set.
func (c *MessagesCreateForumTopicRequest) GetIconEmojiID() (value int64, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(3) {
		return value, false
	}
	return c.IconEmojiID, true
}

// GetRandomID returns value of RandomID field.
func (c *MessagesCreateForumTopicRequest) GetRandomID() (value int64) {
	if c == nil {
		return
	}
	return c.RandomID
}

// SetSendAs sets value of SendAs conditional field.
func (c *MessagesCreateForumTopicRequest) SetSendAs(value InputPeerClass) {
	c.Flags.Set(2)
	c.SendAs = value
}

// GetSendAs returns value of SendAs conditional field and
// boolean which is true if field was set.
func (c *MessagesCreateForumTopicRequest) GetSendAs() (value InputPeerClass, ok bool) {
	if c == nil {
		return
	}
	if !c.Flags.Has(2) {
		return value, false
	}
	return c.SendAs, true
}

// MessagesCreateForumTopic invokes method messages.createForumTopic#2f98c3d5 returning error if any.
//
// See https://core.telegram.org/method/messages.createForumTopic for reference.
func (c *Client) MessagesCreateForumTopic(ctx context.Context, request *MessagesCreateForumTopicRequest) (UpdatesClass, error) {
	var result UpdatesBox

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