// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// BusinessAwayMessageSettings represents TL type `businessAwayMessageSettings#150ba2e9`.
type BusinessAwayMessageSettings struct {
	// Unique quick reply shortcut identifier for the away messages
	ShortcutID int32
	// Chosen recipients of the away messages
	Recipients BusinessRecipients
	// Settings used to check whether the current user is away
	Schedule BusinessAwayMessageScheduleClass
	// True, if the messages must not be sent if the account was online in the last 10
	// minutes
	OfflineOnly bool
}

// BusinessAwayMessageSettingsTypeID is TL type id of BusinessAwayMessageSettings.
const BusinessAwayMessageSettingsTypeID = 0x150ba2e9

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (b *BusinessAwayMessageSettings) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessAwayMessageSettings#150ba2e9 as nil")
	}
	buf.PutID(BusinessAwayMessageSettingsTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessAwayMessageSettings) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessAwayMessageSettings#150ba2e9 as nil")
	}
	buf.PutInt32(b.ShortcutID)
	if err := b.Recipients.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessAwayMessageSettings#150ba2e9: field recipients: %w", err)
	}
	if b.Schedule == nil {
		return fmt.Errorf("unable to encode businessAwayMessageSettings#150ba2e9: field schedule is nil")
	}
	if err := b.Schedule.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessAwayMessageSettings#150ba2e9: field schedule: %w", err)
	}
	buf.PutBool(b.OfflineOnly)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (b *BusinessAwayMessageSettings) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessAwayMessageSettings#150ba2e9 to nil")
	}
	{
		value, err := buf.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: field shortcut_id: %w", err)
		}
		b.ShortcutID = value
	}
	{
		if err := b.Recipients.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: field recipients: %w", err)
		}
	}
	{
		value, err := DecodeBusinessAwayMessageSchedule(buf)
		if err != nil {
			return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: field schedule: %w", err)
		}
		b.Schedule = value
	}
	{
		value, err := buf.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: field offline_only: %w", err)
		}
		b.OfflineOnly = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BusinessAwayMessageSettings) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode businessAwayMessageSettings#150ba2e9 as nil")
	}
	buf.ObjStart()
	buf.PutID("businessAwayMessageSettings")
	buf.Comma()
	buf.FieldStart("shortcut_id")
	buf.PutInt32(b.ShortcutID)
	buf.Comma()
	buf.FieldStart("recipients")
	if err := b.Recipients.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode businessAwayMessageSettings#150ba2e9: field recipients: %w", err)
	}
	buf.Comma()
	buf.FieldStart("schedule")
	if b.Schedule == nil {
		return fmt.Errorf("unable to encode businessAwayMessageSettings#150ba2e9: field schedule is nil")
	}
	if err := b.Schedule.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode businessAwayMessageSettings#150ba2e9: field schedule: %w", err)
	}
	buf.Comma()
	buf.FieldStart("offline_only")
	buf.PutBool(b.OfflineOnly)
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BusinessAwayMessageSettings) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode businessAwayMessageSettings#150ba2e9 to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("businessAwayMessageSettings"); err != nil {
				return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: %w", err)
			}
		case "shortcut_id":
			value, err := buf.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: field shortcut_id: %w", err)
			}
			b.ShortcutID = value
		case "recipients":
			if err := b.Recipients.DecodeTDLibJSON(buf); err != nil {
				return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: field recipients: %w", err)
			}
		case "schedule":
			value, err := DecodeTDLibJSONBusinessAwayMessageSchedule(buf)
			if err != nil {
				return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: field schedule: %w", err)
			}
			b.Schedule = value
		case "offline_only":
			value, err := buf.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode businessAwayMessageSettings#150ba2e9: field offline_only: %w", err)
			}
			b.OfflineOnly = value
		default:
			return buf.Skip()
		}
		return nil
	})
}

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

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

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

// GetOfflineOnly returns value of OfflineOnly field.
func (b *BusinessAwayMessageSettings) GetOfflineOnly() (value bool) {
	if b == nil {
		return
	}
	return b.OfflineOnly
}
