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

// ReactionsNotifySettings represents TL type `reactionsNotifySettings#71e4ea58`.
// Reaction notification settings, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/reactions#notifications-about-reactions
//
// See https://core.telegram.org/constructor/reactionsNotifySettings for reference.
type ReactionsNotifySettings struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Message reaction notification settings, if not set completely disables
	// notifications/updates about message reactions.
	//
	// Use SetMessagesNotifyFrom and GetMessagesNotifyFrom helpers.
	MessagesNotifyFrom ReactionNotificationsFromClass
	// Story reaction notification settings, if not set completely disables
	// notifications/updates about reactions to stories.
	//
	// Use SetStoriesNotifyFrom and GetStoriesNotifyFrom helpers.
	StoriesNotifyFrom ReactionNotificationsFromClass
	// PollVotesNotifyFrom field of ReactionsNotifySettings.
	//
	// Use SetPollVotesNotifyFrom and GetPollVotesNotifyFrom helpers.
	PollVotesNotifyFrom ReactionNotificationsFromClass
	// Notification sound for reactions »¹
	//
	// Links:
	//  1) https://core.telegram.org/api/ringtones
	Sound NotificationSoundClass
	// If false, push notifications »¹ about message/story reactions will only be of type
	// REACT_HIDDEN/REACT_STORY_HIDDEN, without any information about the reacted-to story or
	// the reaction itself.
	//
	// Links:
	//  1) https://core.telegram.org/api/push-updates
	ShowPreviews bool
}

// ReactionsNotifySettingsTypeID is TL type id of ReactionsNotifySettings.
const ReactionsNotifySettingsTypeID = 0x71e4ea58

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

func (r *ReactionsNotifySettings) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.MessagesNotifyFrom == nil) {
		return false
	}
	if !(r.StoriesNotifyFrom == nil) {
		return false
	}
	if !(r.PollVotesNotifyFrom == nil) {
		return false
	}
	if !(r.Sound == nil) {
		return false
	}
	if !(r.ShowPreviews == false) {
		return false
	}

	return true
}

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

// FillFrom fills ReactionsNotifySettings from given interface.
func (r *ReactionsNotifySettings) FillFrom(from interface {
	GetMessagesNotifyFrom() (value ReactionNotificationsFromClass, ok bool)
	GetStoriesNotifyFrom() (value ReactionNotificationsFromClass, ok bool)
	GetPollVotesNotifyFrom() (value ReactionNotificationsFromClass, ok bool)
	GetSound() (value NotificationSoundClass)
	GetShowPreviews() (value bool)
}) {
	if val, ok := from.GetMessagesNotifyFrom(); ok {
		r.MessagesNotifyFrom = val
	}

	if val, ok := from.GetStoriesNotifyFrom(); ok {
		r.StoriesNotifyFrom = val
	}

	if val, ok := from.GetPollVotesNotifyFrom(); ok {
		r.PollVotesNotifyFrom = val
	}

	r.Sound = from.GetSound()
	r.ShowPreviews = from.GetShowPreviews()
}

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

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

// TypeInfo returns info about TL type.
func (r *ReactionsNotifySettings) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "reactionsNotifySettings",
		ID:   ReactionsNotifySettingsTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MessagesNotifyFrom",
			SchemaName: "messages_notify_from",
			Null:       !r.Flags.Has(0),
		},
		{
			Name:       "StoriesNotifyFrom",
			SchemaName: "stories_notify_from",
			Null:       !r.Flags.Has(1),
		},
		{
			Name:       "PollVotesNotifyFrom",
			SchemaName: "poll_votes_notify_from",
			Null:       !r.Flags.Has(2),
		},
		{
			Name:       "Sound",
			SchemaName: "sound",
		},
		{
			Name:       "ShowPreviews",
			SchemaName: "show_previews",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *ReactionsNotifySettings) SetFlags() {
	if !(r.MessagesNotifyFrom == nil) {
		r.Flags.Set(0)
	}
	if !(r.StoriesNotifyFrom == nil) {
		r.Flags.Set(1)
	}
	if !(r.PollVotesNotifyFrom == nil) {
		r.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (r *ReactionsNotifySettings) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode reactionsNotifySettings#71e4ea58 as nil")
	}
	b.PutID(ReactionsNotifySettingsTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ReactionsNotifySettings) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode reactionsNotifySettings#71e4ea58 as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field flags: %w", err)
	}
	if r.Flags.Has(0) {
		if r.MessagesNotifyFrom == nil {
			return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field messages_notify_from is nil")
		}
		if err := r.MessagesNotifyFrom.Encode(b); err != nil {
			return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field messages_notify_from: %w", err)
		}
	}
	if r.Flags.Has(1) {
		if r.StoriesNotifyFrom == nil {
			return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field stories_notify_from is nil")
		}
		if err := r.StoriesNotifyFrom.Encode(b); err != nil {
			return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field stories_notify_from: %w", err)
		}
	}
	if r.Flags.Has(2) {
		if r.PollVotesNotifyFrom == nil {
			return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field poll_votes_notify_from is nil")
		}
		if err := r.PollVotesNotifyFrom.Encode(b); err != nil {
			return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field poll_votes_notify_from: %w", err)
		}
	}
	if r.Sound == nil {
		return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field sound is nil")
	}
	if err := r.Sound.Encode(b); err != nil {
		return fmt.Errorf("unable to encode reactionsNotifySettings#71e4ea58: field sound: %w", err)
	}
	b.PutBool(r.ShowPreviews)
	return nil
}

// Decode implements bin.Decoder.
func (r *ReactionsNotifySettings) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode reactionsNotifySettings#71e4ea58 to nil")
	}
	if err := b.ConsumeID(ReactionsNotifySettingsTypeID); err != nil {
		return fmt.Errorf("unable to decode reactionsNotifySettings#71e4ea58: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ReactionsNotifySettings) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode reactionsNotifySettings#71e4ea58 to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode reactionsNotifySettings#71e4ea58: field flags: %w", err)
		}
	}
	if r.Flags.Has(0) {
		value, err := DecodeReactionNotificationsFrom(b)
		if err != nil {
			return fmt.Errorf("unable to decode reactionsNotifySettings#71e4ea58: field messages_notify_from: %w", err)
		}
		r.MessagesNotifyFrom = value
	}
	if r.Flags.Has(1) {
		value, err := DecodeReactionNotificationsFrom(b)
		if err != nil {
			return fmt.Errorf("unable to decode reactionsNotifySettings#71e4ea58: field stories_notify_from: %w", err)
		}
		r.StoriesNotifyFrom = value
	}
	if r.Flags.Has(2) {
		value, err := DecodeReactionNotificationsFrom(b)
		if err != nil {
			return fmt.Errorf("unable to decode reactionsNotifySettings#71e4ea58: field poll_votes_notify_from: %w", err)
		}
		r.PollVotesNotifyFrom = value
	}
	{
		value, err := DecodeNotificationSound(b)
		if err != nil {
			return fmt.Errorf("unable to decode reactionsNotifySettings#71e4ea58: field sound: %w", err)
		}
		r.Sound = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode reactionsNotifySettings#71e4ea58: field show_previews: %w", err)
		}
		r.ShowPreviews = value
	}
	return nil
}

// SetMessagesNotifyFrom sets value of MessagesNotifyFrom conditional field.
func (r *ReactionsNotifySettings) SetMessagesNotifyFrom(value ReactionNotificationsFromClass) {
	r.Flags.Set(0)
	r.MessagesNotifyFrom = value
}

// GetMessagesNotifyFrom returns value of MessagesNotifyFrom conditional field and
// boolean which is true if field was set.
func (r *ReactionsNotifySettings) GetMessagesNotifyFrom() (value ReactionNotificationsFromClass, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(0) {
		return value, false
	}
	return r.MessagesNotifyFrom, true
}

// SetStoriesNotifyFrom sets value of StoriesNotifyFrom conditional field.
func (r *ReactionsNotifySettings) SetStoriesNotifyFrom(value ReactionNotificationsFromClass) {
	r.Flags.Set(1)
	r.StoriesNotifyFrom = value
}

// GetStoriesNotifyFrom returns value of StoriesNotifyFrom conditional field and
// boolean which is true if field was set.
func (r *ReactionsNotifySettings) GetStoriesNotifyFrom() (value ReactionNotificationsFromClass, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(1) {
		return value, false
	}
	return r.StoriesNotifyFrom, true
}

// SetPollVotesNotifyFrom sets value of PollVotesNotifyFrom conditional field.
func (r *ReactionsNotifySettings) SetPollVotesNotifyFrom(value ReactionNotificationsFromClass) {
	r.Flags.Set(2)
	r.PollVotesNotifyFrom = value
}

// GetPollVotesNotifyFrom returns value of PollVotesNotifyFrom conditional field and
// boolean which is true if field was set.
func (r *ReactionsNotifySettings) GetPollVotesNotifyFrom() (value ReactionNotificationsFromClass, ok bool) {
	if r == nil {
		return
	}
	if !r.Flags.Has(2) {
		return value, false
	}
	return r.PollVotesNotifyFrom, true
}

// GetSound returns value of Sound field.
func (r *ReactionsNotifySettings) GetSound() (value NotificationSoundClass) {
	if r == nil {
		return
	}
	return r.Sound
}

// GetShowPreviews returns value of ShowPreviews field.
func (r *ReactionsNotifySettings) GetShowPreviews() (value bool) {
	if r == nil {
		return
	}
	return r.ShowPreviews
}
