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

// BusinessAwayMessage represents TL type `businessAwayMessage#ef156a5c`.
// Describes a Telegram Business away message¹, automatically sent to users writing to
// us when we're offline, during closing hours, while we're on vacation, or in some other
// custom time period when we cannot immediately answer to the user.
//
// Links:
//  1. https://core.telegram.org/api/business#away-messages
//
// See https://core.telegram.org/constructor/businessAwayMessage for reference.
type BusinessAwayMessage struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, the messages will not be sent if the account was online in the last 10 minutes.
	OfflineOnly bool
	// ID of a quick reply shorcut, containing the away messages to send, see here » for
	// more info¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/business#quick-reply-shortcuts
	ShortcutID int
	// Specifies when should the away messages be sent.
	Schedule BusinessAwayMessageScheduleClass
	// Allowed recipients for the away messages.
	Recipients BusinessRecipients
}

// BusinessAwayMessageTypeID is TL type id of BusinessAwayMessage.
const BusinessAwayMessageTypeID = 0xef156a5c

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

func (b *BusinessAwayMessage) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.OfflineOnly == false) {
		return false
	}
	if !(b.ShortcutID == 0) {
		return false
	}
	if !(b.Schedule == nil) {
		return false
	}
	if !(b.Recipients.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills BusinessAwayMessage from given interface.
func (b *BusinessAwayMessage) FillFrom(from interface {
	GetOfflineOnly() (value bool)
	GetShortcutID() (value int)
	GetSchedule() (value BusinessAwayMessageScheduleClass)
	GetRecipients() (value BusinessRecipients)
}) {
	b.OfflineOnly = from.GetOfflineOnly()
	b.ShortcutID = from.GetShortcutID()
	b.Schedule = from.GetSchedule()
	b.Recipients = from.GetRecipients()
}

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

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

// TypeInfo returns info about TL type.
func (b *BusinessAwayMessage) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessAwayMessage",
		ID:   BusinessAwayMessageTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OfflineOnly",
			SchemaName: "offline_only",
			Null:       !b.Flags.Has(0),
		},
		{
			Name:       "ShortcutID",
			SchemaName: "shortcut_id",
		},
		{
			Name:       "Schedule",
			SchemaName: "schedule",
		},
		{
			Name:       "Recipients",
			SchemaName: "recipients",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *BusinessAwayMessage) SetFlags() {
	if !(b.OfflineOnly == false) {
		b.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (b *BusinessAwayMessage) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessAwayMessage#ef156a5c as nil")
	}
	buf.PutID(BusinessAwayMessageTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessAwayMessage) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessAwayMessage#ef156a5c as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessAwayMessage#ef156a5c: field flags: %w", err)
	}
	buf.PutInt(b.ShortcutID)
	if b.Schedule == nil {
		return fmt.Errorf("unable to encode businessAwayMessage#ef156a5c: field schedule is nil")
	}
	if err := b.Schedule.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessAwayMessage#ef156a5c: field schedule: %w", err)
	}
	if err := b.Recipients.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessAwayMessage#ef156a5c: field recipients: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BusinessAwayMessage) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessAwayMessage#ef156a5c to nil")
	}
	if err := buf.ConsumeID(BusinessAwayMessageTypeID); err != nil {
		return fmt.Errorf("unable to decode businessAwayMessage#ef156a5c: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BusinessAwayMessage) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessAwayMessage#ef156a5c to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessAwayMessage#ef156a5c: field flags: %w", err)
		}
	}
	b.OfflineOnly = b.Flags.Has(0)
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode businessAwayMessage#ef156a5c: field shortcut_id: %w", err)
		}
		b.ShortcutID = value
	}
	{
		value, err := DecodeBusinessAwayMessageSchedule(buf)
		if err != nil {
			return fmt.Errorf("unable to decode businessAwayMessage#ef156a5c: field schedule: %w", err)
		}
		b.Schedule = value
	}
	{
		if err := b.Recipients.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessAwayMessage#ef156a5c: field recipients: %w", err)
		}
	}
	return nil
}

// SetOfflineOnly sets value of OfflineOnly conditional field.
func (b *BusinessAwayMessage) SetOfflineOnly(value bool) {
	if value {
		b.Flags.Set(0)
		b.OfflineOnly = true
	} else {
		b.Flags.Unset(0)
		b.OfflineOnly = false
	}
}

// GetOfflineOnly returns value of OfflineOnly conditional field.
func (b *BusinessAwayMessage) GetOfflineOnly() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(0)
}

// GetShortcutID returns value of ShortcutID field.
func (b *BusinessAwayMessage) GetShortcutID() (value int) {
	if b == nil {
		return
	}
	return b.ShortcutID
}

// GetSchedule returns value of Schedule field.
func (b *BusinessAwayMessage) GetSchedule() (value BusinessAwayMessageScheduleClass) {
	if b == nil {
		return
	}
	return b.Schedule
}

// GetRecipients returns value of Recipients field.
func (b *BusinessAwayMessage) GetRecipients() (value BusinessRecipients) {
	if b == nil {
		return
	}
	return b.Recipients
}
