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

// ChannelsToggleViewForumAsMessagesRequest represents TL type `channels.toggleViewForumAsMessages#9738bb15`.
// Users may also choose to display messages from all topics of a forum¹ as if they were
// sent to a normal group, using a "View as messages" setting in the local client: this
// setting only affects the current account, and is synced to other logged in sessions
// using this method.
// Invoking this method will update the value of the view_forum_as_messages flag of
// channelFull¹ or dialog² and emit an updateChannelViewForumAsMessages³.
//
// Links:
//  1. https://core.telegram.org/api/forum
//  2. https://core.telegram.org/constructor/channelFull
//  3. https://core.telegram.org/constructor/dialog
//  4. https://core.telegram.org/constructor/updateChannelViewForumAsMessages
//
// See https://core.telegram.org/method/channels.toggleViewForumAsMessages for reference.
type ChannelsToggleViewForumAsMessagesRequest struct {
	// The forum
	Channel InputChannelClass
	// The new value of the view_forum_as_messages flag.
	Enabled bool
}

// ChannelsToggleViewForumAsMessagesRequestTypeID is TL type id of ChannelsToggleViewForumAsMessagesRequest.
const ChannelsToggleViewForumAsMessagesRequestTypeID = 0x9738bb15

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

func (t *ChannelsToggleViewForumAsMessagesRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Channel == nil) {
		return false
	}
	if !(t.Enabled == false) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsToggleViewForumAsMessagesRequest from given interface.
func (t *ChannelsToggleViewForumAsMessagesRequest) FillFrom(from interface {
	GetChannel() (value InputChannelClass)
	GetEnabled() (value bool)
}) {
	t.Channel = from.GetChannel()
	t.Enabled = from.GetEnabled()
}

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

// TypeName returns name of type in TL schema.
func (*ChannelsToggleViewForumAsMessagesRequest) TypeName() string {
	return "channels.toggleViewForumAsMessages"
}

// TypeInfo returns info about TL type.
func (t *ChannelsToggleViewForumAsMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.toggleViewForumAsMessages",
		ID:   ChannelsToggleViewForumAsMessagesRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
		{
			Name:       "Enabled",
			SchemaName: "enabled",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ChannelsToggleViewForumAsMessagesRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode channels.toggleViewForumAsMessages#9738bb15 as nil")
	}
	b.PutID(ChannelsToggleViewForumAsMessagesRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ChannelsToggleViewForumAsMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode channels.toggleViewForumAsMessages#9738bb15 as nil")
	}
	if t.Channel == nil {
		return fmt.Errorf("unable to encode channels.toggleViewForumAsMessages#9738bb15: field channel is nil")
	}
	if err := t.Channel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.toggleViewForumAsMessages#9738bb15: field channel: %w", err)
	}
	b.PutBool(t.Enabled)
	return nil
}

// Decode implements bin.Decoder.
func (t *ChannelsToggleViewForumAsMessagesRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode channels.toggleViewForumAsMessages#9738bb15 to nil")
	}
	if err := b.ConsumeID(ChannelsToggleViewForumAsMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.toggleViewForumAsMessages#9738bb15: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ChannelsToggleViewForumAsMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode channels.toggleViewForumAsMessages#9738bb15 to nil")
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.toggleViewForumAsMessages#9738bb15: field channel: %w", err)
		}
		t.Channel = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode channels.toggleViewForumAsMessages#9738bb15: field enabled: %w", err)
		}
		t.Enabled = value
	}
	return nil
}

// GetChannel returns value of Channel field.
func (t *ChannelsToggleViewForumAsMessagesRequest) GetChannel() (value InputChannelClass) {
	if t == nil {
		return
	}
	return t.Channel
}

// GetEnabled returns value of Enabled field.
func (t *ChannelsToggleViewForumAsMessagesRequest) GetEnabled() (value bool) {
	if t == nil {
		return
	}
	return t.Enabled
}

// GetChannelAsNotEmpty returns mapped value of Channel field.
func (t *ChannelsToggleViewForumAsMessagesRequest) GetChannelAsNotEmpty() (NotEmptyInputChannel, bool) {
	return t.Channel.AsNotEmpty()
}

// ChannelsToggleViewForumAsMessages invokes method channels.toggleViewForumAsMessages#9738bb15 returning error if any.
// Users may also choose to display messages from all topics of a forum¹ as if they were
// sent to a normal group, using a "View as messages" setting in the local client: this
// setting only affects the current account, and is synced to other logged in sessions
// using this method.
// Invoking this method will update the value of the view_forum_as_messages flag of
// channelFull¹ or dialog² and emit an updateChannelViewForumAsMessages³.
//
// Links:
//  1. https://core.telegram.org/api/forum
//  2. https://core.telegram.org/constructor/channelFull
//  3. https://core.telegram.org/constructor/dialog
//  4. https://core.telegram.org/constructor/updateChannelViewForumAsMessages
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//
// See https://core.telegram.org/method/channels.toggleViewForumAsMessages for reference.
func (c *Client) ChannelsToggleViewForumAsMessages(ctx context.Context, request *ChannelsToggleViewForumAsMessagesRequest) (UpdatesClass, error) {
	var result UpdatesBox

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