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

// StopPollRequest represents TL type `stopPoll#62e80aad`.
type StopPollRequest struct {
	// Identifier of the chat to which the poll belongs
	ChatID int64
	// Identifier of the message containing the poll. Use messageProperties.can_be_edited to
	// check whether the poll can be stopped
	MessageID int64
	// The new message reply markup; pass null if none; for bots only
	ReplyMarkup ReplyMarkupClass
}

// StopPollRequestTypeID is TL type id of StopPollRequest.
const StopPollRequestTypeID = 0x62e80aad

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StopPollRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stopPoll",
		ID:   StopPollRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "ReplyMarkup",
			SchemaName: "reply_markup",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StopPollRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stopPoll#62e80aad as nil")
	}
	b.PutID(StopPollRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StopPollRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stopPoll#62e80aad as nil")
	}
	b.PutInt53(s.ChatID)
	b.PutInt53(s.MessageID)
	if s.ReplyMarkup == nil {
		return fmt.Errorf("unable to encode stopPoll#62e80aad: field reply_markup is nil")
	}
	if err := s.ReplyMarkup.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stopPoll#62e80aad: field reply_markup: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StopPollRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stopPoll#62e80aad to nil")
	}
	if err := b.ConsumeID(StopPollRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stopPoll#62e80aad: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StopPollRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stopPoll#62e80aad to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode stopPoll#62e80aad: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode stopPoll#62e80aad: field message_id: %w", err)
		}
		s.MessageID = value
	}
	{
		value, err := DecodeReplyMarkup(b)
		if err != nil {
			return fmt.Errorf("unable to decode stopPoll#62e80aad: field reply_markup: %w", err)
		}
		s.ReplyMarkup = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StopPollRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode stopPoll#62e80aad as nil")
	}
	b.ObjStart()
	b.PutID("stopPoll")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(s.MessageID)
	b.Comma()
	b.FieldStart("reply_markup")
	if s.ReplyMarkup == nil {
		return fmt.Errorf("unable to encode stopPoll#62e80aad: field reply_markup is nil")
	}
	if err := s.ReplyMarkup.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode stopPoll#62e80aad: field reply_markup: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StopPollRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode stopPoll#62e80aad to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("stopPoll"); err != nil {
				return fmt.Errorf("unable to decode stopPoll#62e80aad: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode stopPoll#62e80aad: field chat_id: %w", err)
			}
			s.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode stopPoll#62e80aad: field message_id: %w", err)
			}
			s.MessageID = value
		case "reply_markup":
			value, err := DecodeTDLibJSONReplyMarkup(b)
			if err != nil {
				return fmt.Errorf("unable to decode stopPoll#62e80aad: field reply_markup: %w", err)
			}
			s.ReplyMarkup = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// GetReplyMarkup returns value of ReplyMarkup field.
func (s *StopPollRequest) GetReplyMarkup() (value ReplyMarkupClass) {
	if s == nil {
		return
	}
	return s.ReplyMarkup
}

// StopPoll invokes method stopPoll#62e80aad returning error if any.
func (c *Client) StopPoll(ctx context.Context, request *StopPollRequest) error {
	var ok Ok

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