// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// SetPollAnswerRequest represents TL type `setPollAnswer#5303b916`.
type SetPollAnswerRequest struct {
	// Identifier of the chat to which the poll belongs
	ChatID int64
	// Identifier of the message containing the poll
	MessageID int64
	// 0-based identifiers of answer options, chosen by the user. User can choose more than 1
	// answer option only is the poll allows multiple answers
	OptionIDs []int32
}

// SetPollAnswerRequestTypeID is TL type id of SetPollAnswerRequest.
const SetPollAnswerRequestTypeID = 0x5303b916

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

func (s *SetPollAnswerRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.MessageID == 0) {
		return false
	}
	if !(s.OptionIDs == nil) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*SetPollAnswerRequest) TypeName() string {
	return "setPollAnswer"
}

// TypeInfo returns info about TL type.
func (s *SetPollAnswerRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setPollAnswer",
		ID:   SetPollAnswerRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "OptionIDs",
			SchemaName: "option_ids",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetPollAnswerRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPollAnswer#5303b916 as nil")
	}
	b.PutID(SetPollAnswerRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetPollAnswerRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setPollAnswer#5303b916 as nil")
	}
	b.PutInt53(s.ChatID)
	b.PutInt53(s.MessageID)
	b.PutInt(len(s.OptionIDs))
	for _, v := range s.OptionIDs {
		b.PutInt32(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetPollAnswerRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPollAnswer#5303b916 to nil")
	}
	if err := b.ConsumeID(SetPollAnswerRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setPollAnswer#5303b916: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetPollAnswerRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setPollAnswer#5303b916 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setPollAnswer#5303b916: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setPollAnswer#5303b916: field message_id: %w", err)
		}
		s.MessageID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode setPollAnswer#5303b916: field option_ids: %w", err)
		}

		if headerLen > 0 {
			s.OptionIDs = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode setPollAnswer#5303b916: field option_ids: %w", err)
			}
			s.OptionIDs = append(s.OptionIDs, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetPollAnswerRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setPollAnswer#5303b916 as nil")
	}
	b.ObjStart()
	b.PutID("setPollAnswer")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(s.MessageID)
	b.Comma()
	b.FieldStart("option_ids")
	b.ArrStart()
	for _, v := range s.OptionIDs {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetPollAnswerRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setPollAnswer#5303b916 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setPollAnswer"); err != nil {
				return fmt.Errorf("unable to decode setPollAnswer#5303b916: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setPollAnswer#5303b916: field chat_id: %w", err)
			}
			s.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setPollAnswer#5303b916: field message_id: %w", err)
			}
			s.MessageID = value
		case "option_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode setPollAnswer#5303b916: field option_ids: %w", err)
				}
				s.OptionIDs = append(s.OptionIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode setPollAnswer#5303b916: field option_ids: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (s *SetPollAnswerRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

// GetMessageID returns value of MessageID field.
func (s *SetPollAnswerRequest) GetMessageID() (value int64) {
	if s == nil {
		return
	}
	return s.MessageID
}

// GetOptionIDs returns value of OptionIDs field.
func (s *SetPollAnswerRequest) GetOptionIDs() (value []int32) {
	if s == nil {
		return
	}
	return s.OptionIDs
}

// SetPollAnswer invokes method setPollAnswer#5303b916 returning error if any.
func (c *Client) SetPollAnswer(ctx context.Context, request *SetPollAnswerRequest) error {
	var ok Ok

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