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

// MessagesSetBotCallbackAnswerRequest represents TL type `messages.setBotCallbackAnswer#d58f130a`.
// Set the callback answer to a user button press (bots only)
//
// See https://core.telegram.org/method/messages.setBotCallbackAnswer for reference.
type MessagesSetBotCallbackAnswerRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to show the message as a popup instead of a toast notification
	Alert bool
	// Query ID
	QueryID int64
	// Popup to show
	//
	// Use SetMessage and GetMessage helpers.
	Message string
	// URL to open
	//
	// Use SetURL and GetURL helpers.
	URL string
	// Cache validity
	CacheTime int
}

// MessagesSetBotCallbackAnswerRequestTypeID is TL type id of MessagesSetBotCallbackAnswerRequest.
const MessagesSetBotCallbackAnswerRequestTypeID = 0xd58f130a

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

func (s *MessagesSetBotCallbackAnswerRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Alert == false) {
		return false
	}
	if !(s.QueryID == 0) {
		return false
	}
	if !(s.Message == "") {
		return false
	}
	if !(s.URL == "") {
		return false
	}
	if !(s.CacheTime == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSetBotCallbackAnswerRequest from given interface.
func (s *MessagesSetBotCallbackAnswerRequest) FillFrom(from interface {
	GetAlert() (value bool)
	GetQueryID() (value int64)
	GetMessage() (value string, ok bool)
	GetURL() (value string, ok bool)
	GetCacheTime() (value int)
}) {
	s.Alert = from.GetAlert()
	s.QueryID = from.GetQueryID()
	if val, ok := from.GetMessage(); ok {
		s.Message = val
	}

	if val, ok := from.GetURL(); ok {
		s.URL = val
	}

	s.CacheTime = from.GetCacheTime()
}

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

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

// TypeInfo returns info about TL type.
func (s *MessagesSetBotCallbackAnswerRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.setBotCallbackAnswer",
		ID:   MessagesSetBotCallbackAnswerRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Alert",
			SchemaName: "alert",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "QueryID",
			SchemaName: "query_id",
		},
		{
			Name:       "Message",
			SchemaName: "message",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "URL",
			SchemaName: "url",
			Null:       !s.Flags.Has(2),
		},
		{
			Name:       "CacheTime",
			SchemaName: "cache_time",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *MessagesSetBotCallbackAnswerRequest) SetFlags() {
	if !(s.Alert == false) {
		s.Flags.Set(1)
	}
	if !(s.Message == "") {
		s.Flags.Set(0)
	}
	if !(s.URL == "") {
		s.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (s *MessagesSetBotCallbackAnswerRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.setBotCallbackAnswer#d58f130a as nil")
	}
	b.PutID(MessagesSetBotCallbackAnswerRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSetBotCallbackAnswerRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.setBotCallbackAnswer#d58f130a as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.setBotCallbackAnswer#d58f130a: field flags: %w", err)
	}
	b.PutLong(s.QueryID)
	if s.Flags.Has(0) {
		b.PutString(s.Message)
	}
	if s.Flags.Has(2) {
		b.PutString(s.URL)
	}
	b.PutInt(s.CacheTime)
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSetBotCallbackAnswerRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.setBotCallbackAnswer#d58f130a to nil")
	}
	if err := b.ConsumeID(MessagesSetBotCallbackAnswerRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.setBotCallbackAnswer#d58f130a: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSetBotCallbackAnswerRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.setBotCallbackAnswer#d58f130a to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.setBotCallbackAnswer#d58f130a: field flags: %w", err)
		}
	}
	s.Alert = s.Flags.Has(1)
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.setBotCallbackAnswer#d58f130a: field query_id: %w", err)
		}
		s.QueryID = value
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.setBotCallbackAnswer#d58f130a: field message: %w", err)
		}
		s.Message = value
	}
	if s.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.setBotCallbackAnswer#d58f130a: field url: %w", err)
		}
		s.URL = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.setBotCallbackAnswer#d58f130a: field cache_time: %w", err)
		}
		s.CacheTime = value
	}
	return nil
}

// SetAlert sets value of Alert conditional field.
func (s *MessagesSetBotCallbackAnswerRequest) SetAlert(value bool) {
	if value {
		s.Flags.Set(1)
		s.Alert = true
	} else {
		s.Flags.Unset(1)
		s.Alert = false
	}
}

// GetAlert returns value of Alert conditional field.
func (s *MessagesSetBotCallbackAnswerRequest) GetAlert() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(1)
}

// GetQueryID returns value of QueryID field.
func (s *MessagesSetBotCallbackAnswerRequest) GetQueryID() (value int64) {
	if s == nil {
		return
	}
	return s.QueryID
}

// SetMessage sets value of Message conditional field.
func (s *MessagesSetBotCallbackAnswerRequest) SetMessage(value string) {
	s.Flags.Set(0)
	s.Message = value
}

// GetMessage returns value of Message conditional field and
// boolean which is true if field was set.
func (s *MessagesSetBotCallbackAnswerRequest) GetMessage() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Message, true
}

// SetURL sets value of URL conditional field.
func (s *MessagesSetBotCallbackAnswerRequest) SetURL(value string) {
	s.Flags.Set(2)
	s.URL = value
}

// GetURL returns value of URL conditional field and
// boolean which is true if field was set.
func (s *MessagesSetBotCallbackAnswerRequest) GetURL() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(2) {
		return value, false
	}
	return s.URL, true
}

// GetCacheTime returns value of CacheTime field.
func (s *MessagesSetBotCallbackAnswerRequest) GetCacheTime() (value int) {
	if s == nil {
		return
	}
	return s.CacheTime
}

// MessagesSetBotCallbackAnswer invokes method messages.setBotCallbackAnswer#d58f130a returning error if any.
// Set the callback answer to a user button press (bots only)
//
// Possible errors:
//
//	400 MESSAGE_TOO_LONG: The provided message is too long.
//	400 QUERY_ID_INVALID: The query ID is invalid.
//	400 URL_INVALID: Invalid URL provided.
//	400 USER_BOT_REQUIRED: This method can only be called by a bot.
//
// See https://core.telegram.org/method/messages.setBotCallbackAnswer for reference.
func (c *Client) MessagesSetBotCallbackAnswer(ctx context.Context, request *MessagesSetBotCallbackAnswerRequest) (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
}
