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

// MessagesReportReadMetricsRequest represents TL type `messages.reportReadMetrics#4067c5e6`.
//
// See https://core.telegram.org/method/messages.reportReadMetrics for reference.
type MessagesReportReadMetricsRequest struct {
	// Peer field of MessagesReportReadMetricsRequest.
	Peer InputPeerClass
	// Metrics field of MessagesReportReadMetricsRequest.
	Metrics []InputMessageReadMetric
}

// MessagesReportReadMetricsRequestTypeID is TL type id of MessagesReportReadMetricsRequest.
const MessagesReportReadMetricsRequestTypeID = 0x4067c5e6

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

func (r *MessagesReportReadMetricsRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.Metrics == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesReportReadMetricsRequest from given interface.
func (r *MessagesReportReadMetricsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetMetrics() (value []InputMessageReadMetric)
}) {
	r.Peer = from.GetPeer()
	r.Metrics = from.GetMetrics()
}

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

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

// TypeInfo returns info about TL type.
func (r *MessagesReportReadMetricsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.reportReadMetrics",
		ID:   MessagesReportReadMetricsRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Metrics",
			SchemaName: "metrics",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *MessagesReportReadMetricsRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.reportReadMetrics#4067c5e6 as nil")
	}
	b.PutID(MessagesReportReadMetricsRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesReportReadMetricsRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.reportReadMetrics#4067c5e6 as nil")
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode messages.reportReadMetrics#4067c5e6: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.reportReadMetrics#4067c5e6: field peer: %w", err)
	}
	b.PutVectorHeader(len(r.Metrics))
	for idx, v := range r.Metrics {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.reportReadMetrics#4067c5e6: field metrics element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *MessagesReportReadMetricsRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.reportReadMetrics#4067c5e6 to nil")
	}
	if err := b.ConsumeID(MessagesReportReadMetricsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.reportReadMetrics#4067c5e6: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *MessagesReportReadMetricsRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.reportReadMetrics#4067c5e6 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.reportReadMetrics#4067c5e6: field peer: %w", err)
		}
		r.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.reportReadMetrics#4067c5e6: field metrics: %w", err)
		}

		if headerLen > 0 {
			r.Metrics = make([]InputMessageReadMetric, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value InputMessageReadMetric
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode messages.reportReadMetrics#4067c5e6: field metrics: %w", err)
			}
			r.Metrics = append(r.Metrics, value)
		}
	}
	return nil
}

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

// GetMetrics returns value of Metrics field.
func (r *MessagesReportReadMetricsRequest) GetMetrics() (value []InputMessageReadMetric) {
	if r == nil {
		return
	}
	return r.Metrics
}

// MessagesReportReadMetrics invokes method messages.reportReadMetrics#4067c5e6 returning error if any.
//
// See https://core.telegram.org/method/messages.reportReadMetrics for reference.
func (c *Client) MessagesReportReadMetrics(ctx context.Context, request *MessagesReportReadMetricsRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
