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

// MessagesAddPollAnswerRequest represents TL type `messages.addPollAnswer#19bc4b6d`.
//
// See https://core.telegram.org/method/messages.addPollAnswer for reference.
type MessagesAddPollAnswerRequest struct {
	// Peer field of MessagesAddPollAnswerRequest.
	Peer InputPeerClass
	// MsgID field of MessagesAddPollAnswerRequest.
	MsgID int
	// Answer field of MessagesAddPollAnswerRequest.
	Answer PollAnswerClass
}

// MessagesAddPollAnswerRequestTypeID is TL type id of MessagesAddPollAnswerRequest.
const MessagesAddPollAnswerRequestTypeID = 0x19bc4b6d

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

func (a *MessagesAddPollAnswerRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Peer == nil) {
		return false
	}
	if !(a.MsgID == 0) {
		return false
	}
	if !(a.Answer == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesAddPollAnswerRequest from given interface.
func (a *MessagesAddPollAnswerRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetMsgID() (value int)
	GetAnswer() (value PollAnswerClass)
}) {
	a.Peer = from.GetPeer()
	a.MsgID = from.GetMsgID()
	a.Answer = from.GetAnswer()
}

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

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

// TypeInfo returns info about TL type.
func (a *MessagesAddPollAnswerRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.addPollAnswer",
		ID:   MessagesAddPollAnswerRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "MsgID",
			SchemaName: "msg_id",
		},
		{
			Name:       "Answer",
			SchemaName: "answer",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *MessagesAddPollAnswerRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.addPollAnswer#19bc4b6d as nil")
	}
	b.PutID(MessagesAddPollAnswerRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *MessagesAddPollAnswerRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.addPollAnswer#19bc4b6d as nil")
	}
	if a.Peer == nil {
		return fmt.Errorf("unable to encode messages.addPollAnswer#19bc4b6d: field peer is nil")
	}
	if err := a.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.addPollAnswer#19bc4b6d: field peer: %w", err)
	}
	b.PutInt(a.MsgID)
	if a.Answer == nil {
		return fmt.Errorf("unable to encode messages.addPollAnswer#19bc4b6d: field answer is nil")
	}
	if err := a.Answer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.addPollAnswer#19bc4b6d: field answer: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *MessagesAddPollAnswerRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.addPollAnswer#19bc4b6d to nil")
	}
	if err := b.ConsumeID(MessagesAddPollAnswerRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.addPollAnswer#19bc4b6d: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *MessagesAddPollAnswerRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.addPollAnswer#19bc4b6d to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.addPollAnswer#19bc4b6d: field peer: %w", err)
		}
		a.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.addPollAnswer#19bc4b6d: field msg_id: %w", err)
		}
		a.MsgID = value
	}
	{
		value, err := DecodePollAnswer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.addPollAnswer#19bc4b6d: field answer: %w", err)
		}
		a.Answer = value
	}
	return nil
}

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

// GetMsgID returns value of MsgID field.
func (a *MessagesAddPollAnswerRequest) GetMsgID() (value int) {
	if a == nil {
		return
	}
	return a.MsgID
}

// GetAnswer returns value of Answer field.
func (a *MessagesAddPollAnswerRequest) GetAnswer() (value PollAnswerClass) {
	if a == nil {
		return
	}
	return a.Answer
}

// MessagesAddPollAnswer invokes method messages.addPollAnswer#19bc4b6d returning error if any.
//
// See https://core.telegram.org/method/messages.addPollAnswer for reference.
func (c *Client) MessagesAddPollAnswer(ctx context.Context, request *MessagesAddPollAnswerRequest) (UpdatesClass, error) {
	var result UpdatesBox

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