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

// MessagesEditForumTopicRequest represents TL type `messages.editForumTopic#cecc1134`.
//
// See https://core.telegram.org/method/messages.editForumTopic for reference.
type MessagesEditForumTopicRequest struct {
	// Flags field of MessagesEditForumTopicRequest.
	Flags bin.Fields
	// Peer field of MessagesEditForumTopicRequest.
	Peer InputPeerClass
	// TopicID field of MessagesEditForumTopicRequest.
	TopicID int
	// Title field of MessagesEditForumTopicRequest.
	//
	// Use SetTitle and GetTitle helpers.
	Title string
	// IconEmojiID field of MessagesEditForumTopicRequest.
	//
	// Use SetIconEmojiID and GetIconEmojiID helpers.
	IconEmojiID int64
	// Closed field of MessagesEditForumTopicRequest.
	//
	// Use SetClosed and GetClosed helpers.
	Closed bool
	// Hidden field of MessagesEditForumTopicRequest.
	//
	// Use SetHidden and GetHidden helpers.
	Hidden bool
}

// MessagesEditForumTopicRequestTypeID is TL type id of MessagesEditForumTopicRequest.
const MessagesEditForumTopicRequestTypeID = 0xcecc1134

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

func (e *MessagesEditForumTopicRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.Flags.Zero()) {
		return false
	}
	if !(e.Peer == nil) {
		return false
	}
	if !(e.TopicID == 0) {
		return false
	}
	if !(e.Title == "") {
		return false
	}
	if !(e.IconEmojiID == 0) {
		return false
	}
	if !(e.Closed == false) {
		return false
	}
	if !(e.Hidden == false) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesEditForumTopicRequest from given interface.
func (e *MessagesEditForumTopicRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetTopicID() (value int)
	GetTitle() (value string, ok bool)
	GetIconEmojiID() (value int64, ok bool)
	GetClosed() (value bool, ok bool)
	GetHidden() (value bool, ok bool)
}) {
	e.Peer = from.GetPeer()
	e.TopicID = from.GetTopicID()
	if val, ok := from.GetTitle(); ok {
		e.Title = val
	}

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

	if val, ok := from.GetClosed(); ok {
		e.Closed = val
	}

	if val, ok := from.GetHidden(); ok {
		e.Hidden = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (e *MessagesEditForumTopicRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.editForumTopic",
		ID:   MessagesEditForumTopicRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "TopicID",
			SchemaName: "topic_id",
		},
		{
			Name:       "Title",
			SchemaName: "title",
			Null:       !e.Flags.Has(0),
		},
		{
			Name:       "IconEmojiID",
			SchemaName: "icon_emoji_id",
			Null:       !e.Flags.Has(1),
		},
		{
			Name:       "Closed",
			SchemaName: "closed",
			Null:       !e.Flags.Has(2),
		},
		{
			Name:       "Hidden",
			SchemaName: "hidden",
			Null:       !e.Flags.Has(3),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (e *MessagesEditForumTopicRequest) SetFlags() {
	if !(e.Title == "") {
		e.Flags.Set(0)
	}
	if !(e.IconEmojiID == 0) {
		e.Flags.Set(1)
	}
	if !(e.Closed == false) {
		e.Flags.Set(2)
	}
	if !(e.Hidden == false) {
		e.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (e *MessagesEditForumTopicRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode messages.editForumTopic#cecc1134 as nil")
	}
	b.PutID(MessagesEditForumTopicRequestTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *MessagesEditForumTopicRequest) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode messages.editForumTopic#cecc1134 as nil")
	}
	e.SetFlags()
	if err := e.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.editForumTopic#cecc1134: field flags: %w", err)
	}
	if e.Peer == nil {
		return fmt.Errorf("unable to encode messages.editForumTopic#cecc1134: field peer is nil")
	}
	if err := e.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.editForumTopic#cecc1134: field peer: %w", err)
	}
	b.PutInt(e.TopicID)
	if e.Flags.Has(0) {
		b.PutString(e.Title)
	}
	if e.Flags.Has(1) {
		b.PutLong(e.IconEmojiID)
	}
	if e.Flags.Has(2) {
		b.PutBool(e.Closed)
	}
	if e.Flags.Has(3) {
		b.PutBool(e.Hidden)
	}
	return nil
}

// Decode implements bin.Decoder.
func (e *MessagesEditForumTopicRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode messages.editForumTopic#cecc1134 to nil")
	}
	if err := b.ConsumeID(MessagesEditForumTopicRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.editForumTopic#cecc1134: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *MessagesEditForumTopicRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode messages.editForumTopic#cecc1134 to nil")
	}
	{
		if err := e.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.editForumTopic#cecc1134: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.editForumTopic#cecc1134: field peer: %w", err)
		}
		e.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.editForumTopic#cecc1134: field topic_id: %w", err)
		}
		e.TopicID = value
	}
	if e.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.editForumTopic#cecc1134: field title: %w", err)
		}
		e.Title = value
	}
	if e.Flags.Has(1) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.editForumTopic#cecc1134: field icon_emoji_id: %w", err)
		}
		e.IconEmojiID = value
	}
	if e.Flags.Has(2) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messages.editForumTopic#cecc1134: field closed: %w", err)
		}
		e.Closed = value
	}
	if e.Flags.Has(3) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messages.editForumTopic#cecc1134: field hidden: %w", err)
		}
		e.Hidden = value
	}
	return nil
}

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

// GetTopicID returns value of TopicID field.
func (e *MessagesEditForumTopicRequest) GetTopicID() (value int) {
	if e == nil {
		return
	}
	return e.TopicID
}

// SetTitle sets value of Title conditional field.
func (e *MessagesEditForumTopicRequest) SetTitle(value string) {
	e.Flags.Set(0)
	e.Title = value
}

// GetTitle returns value of Title conditional field and
// boolean which is true if field was set.
func (e *MessagesEditForumTopicRequest) GetTitle() (value string, ok bool) {
	if e == nil {
		return
	}
	if !e.Flags.Has(0) {
		return value, false
	}
	return e.Title, true
}

// SetIconEmojiID sets value of IconEmojiID conditional field.
func (e *MessagesEditForumTopicRequest) SetIconEmojiID(value int64) {
	e.Flags.Set(1)
	e.IconEmojiID = value
}

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

// SetClosed sets value of Closed conditional field.
func (e *MessagesEditForumTopicRequest) SetClosed(value bool) {
	e.Flags.Set(2)
	e.Closed = value
}

// GetClosed returns value of Closed conditional field and
// boolean which is true if field was set.
func (e *MessagesEditForumTopicRequest) GetClosed() (value bool, ok bool) {
	if e == nil {
		return
	}
	if !e.Flags.Has(2) {
		return value, false
	}
	return e.Closed, true
}

// SetHidden sets value of Hidden conditional field.
func (e *MessagesEditForumTopicRequest) SetHidden(value bool) {
	e.Flags.Set(3)
	e.Hidden = value
}

// GetHidden returns value of Hidden conditional field and
// boolean which is true if field was set.
func (e *MessagesEditForumTopicRequest) GetHidden() (value bool, ok bool) {
	if e == nil {
		return
	}
	if !e.Flags.Has(3) {
		return value, false
	}
	return e.Hidden, true
}

// MessagesEditForumTopic invokes method messages.editForumTopic#cecc1134 returning error if any.
//
// See https://core.telegram.org/method/messages.editForumTopic for reference.
func (c *Client) MessagesEditForumTopic(ctx context.Context, request *MessagesEditForumTopicRequest) (UpdatesClass, error) {
	var result UpdatesBox

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