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

// MessagesReadPollVotesRequest represents TL type `messages.readPollVotes#1720b4d8`.
//
// See https://core.telegram.org/method/messages.readPollVotes for reference.
type MessagesReadPollVotesRequest struct {
	// Flags field of MessagesReadPollVotesRequest.
	Flags bin.Fields
	// Peer field of MessagesReadPollVotesRequest.
	Peer InputPeerClass
	// TopMsgID field of MessagesReadPollVotesRequest.
	//
	// Use SetTopMsgID and GetTopMsgID helpers.
	TopMsgID int
}

// MessagesReadPollVotesRequestTypeID is TL type id of MessagesReadPollVotesRequest.
const MessagesReadPollVotesRequestTypeID = 0x1720b4d8

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

func (r *MessagesReadPollVotesRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.TopMsgID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesReadPollVotesRequest from given interface.
func (r *MessagesReadPollVotesRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetTopMsgID() (value int, ok bool)
}) {
	r.Peer = from.GetPeer()
	if val, ok := from.GetTopMsgID(); ok {
		r.TopMsgID = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (r *MessagesReadPollVotesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.readPollVotes",
		ID:   MessagesReadPollVotesRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "TopMsgID",
			SchemaName: "top_msg_id",
			Null:       !r.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *MessagesReadPollVotesRequest) SetFlags() {
	if !(r.TopMsgID == 0) {
		r.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (r *MessagesReadPollVotesRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.readPollVotes#1720b4d8 as nil")
	}
	b.PutID(MessagesReadPollVotesRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesReadPollVotesRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.readPollVotes#1720b4d8 as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.readPollVotes#1720b4d8: field flags: %w", err)
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode messages.readPollVotes#1720b4d8: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.readPollVotes#1720b4d8: field peer: %w", err)
	}
	if r.Flags.Has(0) {
		b.PutInt(r.TopMsgID)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (r *MessagesReadPollVotesRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.readPollVotes#1720b4d8 to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.readPollVotes#1720b4d8: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.readPollVotes#1720b4d8: field peer: %w", err)
		}
		r.Peer = value
	}
	if r.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.readPollVotes#1720b4d8: field top_msg_id: %w", err)
		}
		r.TopMsgID = value
	}
	return nil
}

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

// SetTopMsgID sets value of TopMsgID conditional field.
func (r *MessagesReadPollVotesRequest) SetTopMsgID(value int) {
	r.Flags.Set(0)
	r.TopMsgID = value
}

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

// MessagesReadPollVotes invokes method messages.readPollVotes#1720b4d8 returning error if any.
//
// See https://core.telegram.org/method/messages.readPollVotes for reference.
func (c *Client) MessagesReadPollVotes(ctx context.Context, request *MessagesReadPollVotesRequest) (*MessagesAffectedHistory, error) {
	var result MessagesAffectedHistory

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