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

// ChannelsGetMessageAuthorRequest represents TL type `channels.getMessageAuthor#ece2a0e6`.
// Can only be invoked by non-bot admins of a monoforum »¹, obtains the original sender
// of a message sent by other monoforum admins to the monoforum, on behalf of the channel
// associated to the monoforum.
//
// Links:
//  1. https://core.telegram.org/api/monoforum
//
// See https://core.telegram.org/method/channels.getMessageAuthor for reference.
type ChannelsGetMessageAuthorRequest struct {
	// ID of the monoforum.
	Channel InputChannelClass
	// ID of the message sent by a monoforum admin.
	ID int
}

// ChannelsGetMessageAuthorRequestTypeID is TL type id of ChannelsGetMessageAuthorRequest.
const ChannelsGetMessageAuthorRequestTypeID = 0xece2a0e6

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

func (g *ChannelsGetMessageAuthorRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Channel == nil) {
		return false
	}
	if !(g.ID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsGetMessageAuthorRequest from given interface.
func (g *ChannelsGetMessageAuthorRequest) FillFrom(from interface {
	GetChannel() (value InputChannelClass)
	GetID() (value int)
}) {
	g.Channel = from.GetChannel()
	g.ID = from.GetID()
}

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

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

// TypeInfo returns info about TL type.
func (g *ChannelsGetMessageAuthorRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.getMessageAuthor",
		ID:   ChannelsGetMessageAuthorRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *ChannelsGetMessageAuthorRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode channels.getMessageAuthor#ece2a0e6 as nil")
	}
	b.PutID(ChannelsGetMessageAuthorRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *ChannelsGetMessageAuthorRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode channels.getMessageAuthor#ece2a0e6 as nil")
	}
	if g.Channel == nil {
		return fmt.Errorf("unable to encode channels.getMessageAuthor#ece2a0e6: field channel is nil")
	}
	if err := g.Channel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.getMessageAuthor#ece2a0e6: field channel: %w", err)
	}
	b.PutInt(g.ID)
	return nil
}

// Decode implements bin.Decoder.
func (g *ChannelsGetMessageAuthorRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode channels.getMessageAuthor#ece2a0e6 to nil")
	}
	if err := b.ConsumeID(ChannelsGetMessageAuthorRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.getMessageAuthor#ece2a0e6: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *ChannelsGetMessageAuthorRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode channels.getMessageAuthor#ece2a0e6 to nil")
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.getMessageAuthor#ece2a0e6: field channel: %w", err)
		}
		g.Channel = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode channels.getMessageAuthor#ece2a0e6: field id: %w", err)
		}
		g.ID = value
	}
	return nil
}

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

// GetID returns value of ID field.
func (g *ChannelsGetMessageAuthorRequest) GetID() (value int) {
	if g == nil {
		return
	}
	return g.ID
}

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

// ChannelsGetMessageAuthor invokes method channels.getMessageAuthor#ece2a0e6 returning error if any.
// Can only be invoked by non-bot admins of a monoforum »¹, obtains the original sender
// of a message sent by other monoforum admins to the monoforum, on behalf of the channel
// associated to the monoforum.
//
// Links:
//  1. https://core.telegram.org/api/monoforum
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//
// See https://core.telegram.org/method/channels.getMessageAuthor for reference.
func (c *Client) ChannelsGetMessageAuthor(ctx context.Context, request *ChannelsGetMessageAuthorRequest) (UserClass, error) {
	var result UserBox

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