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

// StoriesStealthMode represents TL type `storiesStealthMode#712e27fd`.
// Information about the current stealth mode¹ session.
//
// Links:
//  1. https://core.telegram.org/api/stories#stealth-mode
//
// See https://core.telegram.org/constructor/storiesStealthMode for reference.
type StoriesStealthMode struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The date up to which stealth mode will be active.
	//
	// Use SetActiveUntilDate and GetActiveUntilDate helpers.
	ActiveUntilDate int
	// The date starting from which the user will be allowed to re-enable stealth mode again.
	//
	// Use SetCooldownUntilDate and GetCooldownUntilDate helpers.
	CooldownUntilDate int
}

// StoriesStealthModeTypeID is TL type id of StoriesStealthMode.
const StoriesStealthModeTypeID = 0x712e27fd

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

func (s *StoriesStealthMode) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.ActiveUntilDate == 0) {
		return false
	}
	if !(s.CooldownUntilDate == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesStealthMode from given interface.
func (s *StoriesStealthMode) FillFrom(from interface {
	GetActiveUntilDate() (value int, ok bool)
	GetCooldownUntilDate() (value int, ok bool)
}) {
	if val, ok := from.GetActiveUntilDate(); ok {
		s.ActiveUntilDate = val
	}

	if val, ok := from.GetCooldownUntilDate(); ok {
		s.CooldownUntilDate = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *StoriesStealthMode) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storiesStealthMode",
		ID:   StoriesStealthModeTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ActiveUntilDate",
			SchemaName: "active_until_date",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "CooldownUntilDate",
			SchemaName: "cooldown_until_date",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StoriesStealthMode) SetFlags() {
	if !(s.ActiveUntilDate == 0) {
		s.Flags.Set(0)
	}
	if !(s.CooldownUntilDate == 0) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *StoriesStealthMode) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storiesStealthMode#712e27fd as nil")
	}
	b.PutID(StoriesStealthModeTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoriesStealthMode) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storiesStealthMode#712e27fd as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode storiesStealthMode#712e27fd: field flags: %w", err)
	}
	if s.Flags.Has(0) {
		b.PutInt(s.ActiveUntilDate)
	}
	if s.Flags.Has(1) {
		b.PutInt(s.CooldownUntilDate)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StoriesStealthMode) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storiesStealthMode#712e27fd to nil")
	}
	if err := b.ConsumeID(StoriesStealthModeTypeID); err != nil {
		return fmt.Errorf("unable to decode storiesStealthMode#712e27fd: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StoriesStealthMode) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storiesStealthMode#712e27fd to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode storiesStealthMode#712e27fd: field flags: %w", err)
		}
	}
	if s.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode storiesStealthMode#712e27fd: field active_until_date: %w", err)
		}
		s.ActiveUntilDate = value
	}
	if s.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode storiesStealthMode#712e27fd: field cooldown_until_date: %w", err)
		}
		s.CooldownUntilDate = value
	}
	return nil
}

// SetActiveUntilDate sets value of ActiveUntilDate conditional field.
func (s *StoriesStealthMode) SetActiveUntilDate(value int) {
	s.Flags.Set(0)
	s.ActiveUntilDate = value
}

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

// SetCooldownUntilDate sets value of CooldownUntilDate conditional field.
func (s *StoriesStealthMode) SetCooldownUntilDate(value int) {
	s.Flags.Set(1)
	s.CooldownUntilDate = value
}

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