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

// GlobalPrivacySettings represents TL type `globalPrivacySettings#fe41b34f`.
// Global privacy settings
//
// See https://core.telegram.org/constructor/globalPrivacySettings for reference.
type GlobalPrivacySettings struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to archive and mute new chats from non-contacts
	ArchiveAndMuteNewNoncontactPeers bool
	// Whether unmuted chats will be kept in the Archive chat list when they get a new
	// message.
	KeepArchivedUnmuted bool
	// Whether unmuted chats that are always included or pinned in a folder¹, will be kept
	// in the Archive chat list when they get a new message. Ignored if keep_archived_unmuted
	// is set.
	//
	// Links:
	//  1) https://core.telegram.org/api/folders
	KeepArchivedFolders bool
	// If this flag is set, the inputPrivacyKeyStatusTimestamp¹ key will also apply to the
	// ability to use messages.getOutboxReadDate² on messages sent to us. Meaning, users
	// that cannot see our exact last online date due to the current value of the
	// inputPrivacyKeyStatusTimestamp³ key will receive a 403 USER_PRIVACY_RESTRICTED error
	// when invoking messages.getOutboxReadDate⁴ to fetch the exact read date of a message
	// they sent to us. The userFull⁵.read_dates_private flag will be set for users that
	// have this flag enabled.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/inputPrivacyKeyStatusTimestamp
	//  2) https://core.telegram.org/method/messages.getOutboxReadDate
	//  3) https://core.telegram.org/constructor/inputPrivacyKeyStatusTimestamp
	//  4) https://core.telegram.org/method/messages.getOutboxReadDate
	//  5) https://core.telegram.org/constructor/userFull
	HideReadMarks bool
	// See here for more info on this flag »¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/privacy#require-premium-for-new-non-contact-users
	NewNoncontactPeersRequirePremium bool
	// Enables or disables our userFull¹.display_gifts_button flag: if the userFull²
	// display_gifts_button flag of both us and another user is set, a gift button should
	// always be displayed in the text field in private chats with the other user: once
	// clicked, the gift UI should be displayed, offering the user options to gift Telegram
	// Premium »³ subscriptions or Telegram Gifts »⁴.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/userFull
	//  2) https://core.telegram.org/constructor/userFull
	//  3) https://core.telegram.org/api/premium#gifting-telegram-premium
	//  4) https://core.telegram.org/api/gifts
	DisplayGiftsButton bool
	// If configured, specifies the number of stars¹ users must pay us to send us a message,
	// see here »² for more info on paid messages.
	//
	// Links:
	//  1) https://core.telegram.org/api/stars
	//  2) https://core.telegram.org/api/paid-messages
	//
	// Use SetNoncontactPeersPaidStars and GetNoncontactPeersPaidStars helpers.
	NoncontactPeersPaidStars int64
	// Disallows the reception of specific gift types.
	//
	// Use SetDisallowedGifts and GetDisallowedGifts helpers.
	DisallowedGifts DisallowedGiftsSettings
}

// GlobalPrivacySettingsTypeID is TL type id of GlobalPrivacySettings.
const GlobalPrivacySettingsTypeID = 0xfe41b34f

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

func (g *GlobalPrivacySettings) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.ArchiveAndMuteNewNoncontactPeers == false) {
		return false
	}
	if !(g.KeepArchivedUnmuted == false) {
		return false
	}
	if !(g.KeepArchivedFolders == false) {
		return false
	}
	if !(g.HideReadMarks == false) {
		return false
	}
	if !(g.NewNoncontactPeersRequirePremium == false) {
		return false
	}
	if !(g.DisplayGiftsButton == false) {
		return false
	}
	if !(g.NoncontactPeersPaidStars == 0) {
		return false
	}
	if !(g.DisallowedGifts.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills GlobalPrivacySettings from given interface.
func (g *GlobalPrivacySettings) FillFrom(from interface {
	GetArchiveAndMuteNewNoncontactPeers() (value bool)
	GetKeepArchivedUnmuted() (value bool)
	GetKeepArchivedFolders() (value bool)
	GetHideReadMarks() (value bool)
	GetNewNoncontactPeersRequirePremium() (value bool)
	GetDisplayGiftsButton() (value bool)
	GetNoncontactPeersPaidStars() (value int64, ok bool)
	GetDisallowedGifts() (value DisallowedGiftsSettings, ok bool)
}) {
	g.ArchiveAndMuteNewNoncontactPeers = from.GetArchiveAndMuteNewNoncontactPeers()
	g.KeepArchivedUnmuted = from.GetKeepArchivedUnmuted()
	g.KeepArchivedFolders = from.GetKeepArchivedFolders()
	g.HideReadMarks = from.GetHideReadMarks()
	g.NewNoncontactPeersRequirePremium = from.GetNewNoncontactPeersRequirePremium()
	g.DisplayGiftsButton = from.GetDisplayGiftsButton()
	if val, ok := from.GetNoncontactPeersPaidStars(); ok {
		g.NoncontactPeersPaidStars = val
	}

	if val, ok := from.GetDisallowedGifts(); ok {
		g.DisallowedGifts = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (g *GlobalPrivacySettings) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "globalPrivacySettings",
		ID:   GlobalPrivacySettingsTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ArchiveAndMuteNewNoncontactPeers",
			SchemaName: "archive_and_mute_new_noncontact_peers",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "KeepArchivedUnmuted",
			SchemaName: "keep_archived_unmuted",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "KeepArchivedFolders",
			SchemaName: "keep_archived_folders",
			Null:       !g.Flags.Has(2),
		},
		{
			Name:       "HideReadMarks",
			SchemaName: "hide_read_marks",
			Null:       !g.Flags.Has(3),
		},
		{
			Name:       "NewNoncontactPeersRequirePremium",
			SchemaName: "new_noncontact_peers_require_premium",
			Null:       !g.Flags.Has(4),
		},
		{
			Name:       "DisplayGiftsButton",
			SchemaName: "display_gifts_button",
			Null:       !g.Flags.Has(7),
		},
		{
			Name:       "NoncontactPeersPaidStars",
			SchemaName: "noncontact_peers_paid_stars",
			Null:       !g.Flags.Has(5),
		},
		{
			Name:       "DisallowedGifts",
			SchemaName: "disallowed_gifts",
			Null:       !g.Flags.Has(6),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *GlobalPrivacySettings) SetFlags() {
	if !(g.ArchiveAndMuteNewNoncontactPeers == false) {
		g.Flags.Set(0)
	}
	if !(g.KeepArchivedUnmuted == false) {
		g.Flags.Set(1)
	}
	if !(g.KeepArchivedFolders == false) {
		g.Flags.Set(2)
	}
	if !(g.HideReadMarks == false) {
		g.Flags.Set(3)
	}
	if !(g.NewNoncontactPeersRequirePremium == false) {
		g.Flags.Set(4)
	}
	if !(g.DisplayGiftsButton == false) {
		g.Flags.Set(7)
	}
	if !(g.NoncontactPeersPaidStars == 0) {
		g.Flags.Set(5)
	}
	if !(g.DisallowedGifts.Zero()) {
		g.Flags.Set(6)
	}
}

// Encode implements bin.Encoder.
func (g *GlobalPrivacySettings) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode globalPrivacySettings#fe41b34f as nil")
	}
	b.PutID(GlobalPrivacySettingsTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GlobalPrivacySettings) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode globalPrivacySettings#fe41b34f as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode globalPrivacySettings#fe41b34f: field flags: %w", err)
	}
	if g.Flags.Has(5) {
		b.PutLong(g.NoncontactPeersPaidStars)
	}
	if g.Flags.Has(6) {
		if err := g.DisallowedGifts.Encode(b); err != nil {
			return fmt.Errorf("unable to encode globalPrivacySettings#fe41b34f: field disallowed_gifts: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GlobalPrivacySettings) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode globalPrivacySettings#fe41b34f to nil")
	}
	if err := b.ConsumeID(GlobalPrivacySettingsTypeID); err != nil {
		return fmt.Errorf("unable to decode globalPrivacySettings#fe41b34f: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GlobalPrivacySettings) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode globalPrivacySettings#fe41b34f to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode globalPrivacySettings#fe41b34f: field flags: %w", err)
		}
	}
	g.ArchiveAndMuteNewNoncontactPeers = g.Flags.Has(0)
	g.KeepArchivedUnmuted = g.Flags.Has(1)
	g.KeepArchivedFolders = g.Flags.Has(2)
	g.HideReadMarks = g.Flags.Has(3)
	g.NewNoncontactPeersRequirePremium = g.Flags.Has(4)
	g.DisplayGiftsButton = g.Flags.Has(7)
	if g.Flags.Has(5) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode globalPrivacySettings#fe41b34f: field noncontact_peers_paid_stars: %w", err)
		}
		g.NoncontactPeersPaidStars = value
	}
	if g.Flags.Has(6) {
		if err := g.DisallowedGifts.Decode(b); err != nil {
			return fmt.Errorf("unable to decode globalPrivacySettings#fe41b34f: field disallowed_gifts: %w", err)
		}
	}
	return nil
}

// SetArchiveAndMuteNewNoncontactPeers sets value of ArchiveAndMuteNewNoncontactPeers conditional field.
func (g *GlobalPrivacySettings) SetArchiveAndMuteNewNoncontactPeers(value bool) {
	if value {
		g.Flags.Set(0)
		g.ArchiveAndMuteNewNoncontactPeers = true
	} else {
		g.Flags.Unset(0)
		g.ArchiveAndMuteNewNoncontactPeers = false
	}
}

// GetArchiveAndMuteNewNoncontactPeers returns value of ArchiveAndMuteNewNoncontactPeers conditional field.
func (g *GlobalPrivacySettings) GetArchiveAndMuteNewNoncontactPeers() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

// SetKeepArchivedUnmuted sets value of KeepArchivedUnmuted conditional field.
func (g *GlobalPrivacySettings) SetKeepArchivedUnmuted(value bool) {
	if value {
		g.Flags.Set(1)
		g.KeepArchivedUnmuted = true
	} else {
		g.Flags.Unset(1)
		g.KeepArchivedUnmuted = false
	}
}

// GetKeepArchivedUnmuted returns value of KeepArchivedUnmuted conditional field.
func (g *GlobalPrivacySettings) GetKeepArchivedUnmuted() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(1)
}

// SetKeepArchivedFolders sets value of KeepArchivedFolders conditional field.
func (g *GlobalPrivacySettings) SetKeepArchivedFolders(value bool) {
	if value {
		g.Flags.Set(2)
		g.KeepArchivedFolders = true
	} else {
		g.Flags.Unset(2)
		g.KeepArchivedFolders = false
	}
}

// GetKeepArchivedFolders returns value of KeepArchivedFolders conditional field.
func (g *GlobalPrivacySettings) GetKeepArchivedFolders() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(2)
}

// SetHideReadMarks sets value of HideReadMarks conditional field.
func (g *GlobalPrivacySettings) SetHideReadMarks(value bool) {
	if value {
		g.Flags.Set(3)
		g.HideReadMarks = true
	} else {
		g.Flags.Unset(3)
		g.HideReadMarks = false
	}
}

// GetHideReadMarks returns value of HideReadMarks conditional field.
func (g *GlobalPrivacySettings) GetHideReadMarks() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(3)
}

// SetNewNoncontactPeersRequirePremium sets value of NewNoncontactPeersRequirePremium conditional field.
func (g *GlobalPrivacySettings) SetNewNoncontactPeersRequirePremium(value bool) {
	if value {
		g.Flags.Set(4)
		g.NewNoncontactPeersRequirePremium = true
	} else {
		g.Flags.Unset(4)
		g.NewNoncontactPeersRequirePremium = false
	}
}

// GetNewNoncontactPeersRequirePremium returns value of NewNoncontactPeersRequirePremium conditional field.
func (g *GlobalPrivacySettings) GetNewNoncontactPeersRequirePremium() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(4)
}

// SetDisplayGiftsButton sets value of DisplayGiftsButton conditional field.
func (g *GlobalPrivacySettings) SetDisplayGiftsButton(value bool) {
	if value {
		g.Flags.Set(7)
		g.DisplayGiftsButton = true
	} else {
		g.Flags.Unset(7)
		g.DisplayGiftsButton = false
	}
}

// GetDisplayGiftsButton returns value of DisplayGiftsButton conditional field.
func (g *GlobalPrivacySettings) GetDisplayGiftsButton() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(7)
}

// SetNoncontactPeersPaidStars sets value of NoncontactPeersPaidStars conditional field.
func (g *GlobalPrivacySettings) SetNoncontactPeersPaidStars(value int64) {
	g.Flags.Set(5)
	g.NoncontactPeersPaidStars = value
}

// GetNoncontactPeersPaidStars returns value of NoncontactPeersPaidStars conditional field and
// boolean which is true if field was set.
func (g *GlobalPrivacySettings) GetNoncontactPeersPaidStars() (value int64, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(5) {
		return value, false
	}
	return g.NoncontactPeersPaidStars, true
}

// SetDisallowedGifts sets value of DisallowedGifts conditional field.
func (g *GlobalPrivacySettings) SetDisallowedGifts(value DisallowedGiftsSettings) {
	g.Flags.Set(6)
	g.DisallowedGifts = value
}

// GetDisallowedGifts returns value of DisallowedGifts conditional field and
// boolean which is true if field was set.
func (g *GlobalPrivacySettings) GetDisallowedGifts() (value DisallowedGiftsSettings, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(6) {
		return value, false
	}
	return g.DisallowedGifts, true
}
