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

// MessagesSetChatAvailableReactionsRequest represents TL type `messages.setChatAvailableReactions#864b2581`.
// Change the set of message reactions »¹ that can be used in a certain group,
// supergroup or channel
//
// Links:
//  1. https://core.telegram.org/api/reactions
//
// See https://core.telegram.org/method/messages.setChatAvailableReactions for reference.
type MessagesSetChatAvailableReactionsRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Group where to apply changes
	Peer InputPeerClass
	// Allowed reaction emojis
	AvailableReactions ChatReactionsClass
	// This flag may be used to impose a custom limit of unique reactions (i.e. a
	// customizable version of appConfig.reactions_uniq_max¹); this field and the other info
	// set by the method will then be available to users in channelFull² and chatFull³. If
	// this flag is not set, the previously configured reactions_limit will not be altered.
	//
	// Links:
	//  1) https://core.telegram.org/api/config#reactions-uniq-max
	//  2) https://core.telegram.org/constructor/channelFull
	//  3) https://core.telegram.org/constructor/chatFull
	//
	// Use SetReactionsLimit and GetReactionsLimit helpers.
	ReactionsLimit int
	// If this flag is set and a Bool¹ is passed, the method will enable or disable paid
	// message reactions »². If this flag is not set, the previously stored setting will
	// not be changed.
	//
	// Links:
	//  1) https://core.telegram.org/type/Bool
	//  2) https://core.telegram.org/api/reactions#paid-reactions
	//
	// Use SetPaidEnabled and GetPaidEnabled helpers.
	PaidEnabled bool
}

// MessagesSetChatAvailableReactionsRequestTypeID is TL type id of MessagesSetChatAvailableReactionsRequest.
const MessagesSetChatAvailableReactionsRequestTypeID = 0x864b2581

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

func (s *MessagesSetChatAvailableReactionsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.AvailableReactions == nil) {
		return false
	}
	if !(s.ReactionsLimit == 0) {
		return false
	}
	if !(s.PaidEnabled == false) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSetChatAvailableReactionsRequest from given interface.
func (s *MessagesSetChatAvailableReactionsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetAvailableReactions() (value ChatReactionsClass)
	GetReactionsLimit() (value int, ok bool)
	GetPaidEnabled() (value bool, ok bool)
}) {
	s.Peer = from.GetPeer()
	s.AvailableReactions = from.GetAvailableReactions()
	if val, ok := from.GetReactionsLimit(); ok {
		s.ReactionsLimit = val
	}

	if val, ok := from.GetPaidEnabled(); ok {
		s.PaidEnabled = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *MessagesSetChatAvailableReactionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.setChatAvailableReactions",
		ID:   MessagesSetChatAvailableReactionsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "AvailableReactions",
			SchemaName: "available_reactions",
		},
		{
			Name:       "ReactionsLimit",
			SchemaName: "reactions_limit",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "PaidEnabled",
			SchemaName: "paid_enabled",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *MessagesSetChatAvailableReactionsRequest) SetFlags() {
	if !(s.ReactionsLimit == 0) {
		s.Flags.Set(0)
	}
	if !(s.PaidEnabled == false) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *MessagesSetChatAvailableReactionsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.setChatAvailableReactions#864b2581 as nil")
	}
	b.PutID(MessagesSetChatAvailableReactionsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSetChatAvailableReactionsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.setChatAvailableReactions#864b2581 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.setChatAvailableReactions#864b2581: field flags: %w", err)
	}
	if s.Peer == nil {
		return fmt.Errorf("unable to encode messages.setChatAvailableReactions#864b2581: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.setChatAvailableReactions#864b2581: field peer: %w", err)
	}
	if s.AvailableReactions == nil {
		return fmt.Errorf("unable to encode messages.setChatAvailableReactions#864b2581: field available_reactions is nil")
	}
	if err := s.AvailableReactions.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.setChatAvailableReactions#864b2581: field available_reactions: %w", err)
	}
	if s.Flags.Has(0) {
		b.PutInt(s.ReactionsLimit)
	}
	if s.Flags.Has(1) {
		b.PutBool(s.PaidEnabled)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSetChatAvailableReactionsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.setChatAvailableReactions#864b2581 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.setChatAvailableReactions#864b2581: field flags: %w", err)
		}
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.setChatAvailableReactions#864b2581: field peer: %w", err)
		}
		s.Peer = value
	}
	{
		value, err := DecodeChatReactions(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.setChatAvailableReactions#864b2581: field available_reactions: %w", err)
		}
		s.AvailableReactions = value
	}
	if s.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.setChatAvailableReactions#864b2581: field reactions_limit: %w", err)
		}
		s.ReactionsLimit = value
	}
	if s.Flags.Has(1) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messages.setChatAvailableReactions#864b2581: field paid_enabled: %w", err)
		}
		s.PaidEnabled = value
	}
	return nil
}

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

// GetAvailableReactions returns value of AvailableReactions field.
func (s *MessagesSetChatAvailableReactionsRequest) GetAvailableReactions() (value ChatReactionsClass) {
	if s == nil {
		return
	}
	return s.AvailableReactions
}

// SetReactionsLimit sets value of ReactionsLimit conditional field.
func (s *MessagesSetChatAvailableReactionsRequest) SetReactionsLimit(value int) {
	s.Flags.Set(0)
	s.ReactionsLimit = value
}

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

// SetPaidEnabled sets value of PaidEnabled conditional field.
func (s *MessagesSetChatAvailableReactionsRequest) SetPaidEnabled(value bool) {
	s.Flags.Set(1)
	s.PaidEnabled = value
}

// GetPaidEnabled returns value of PaidEnabled conditional field and
// boolean which is true if field was set.
func (s *MessagesSetChatAvailableReactionsRequest) GetPaidEnabled() (value bool, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.PaidEnabled, true
}

// MessagesSetChatAvailableReactions invokes method messages.setChatAvailableReactions#864b2581 returning error if any.
// Change the set of message reactions »¹ that can be used in a certain group,
// supergroup or channel
//
// Links:
//  1. https://core.telegram.org/api/reactions
//
// Possible errors:
//
//	400 CHAT_ADMIN_REQUIRED: You must be an admin in this chat to do this.
//	400 CHAT_NOT_MODIFIED: No changes were made to chat information because the new information you passed is identical to the current information.
//	400 DOCUMENT_INVALID: The specified document is invalid.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 REACTION_INVALID: The specified reaction is invalid.
//
// See https://core.telegram.org/method/messages.setChatAvailableReactions for reference.
func (c *Client) MessagesSetChatAvailableReactions(ctx context.Context, request *MessagesSetChatAvailableReactionsRequest) (UpdatesClass, error) {
	var result UpdatesBox

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