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

// AccountUpdateBusinessAwayMessageRequest represents TL type `account.updateBusinessAwayMessage#a26a7fa5`.
// Set a list of Telegram Business away messages¹.
//
// Links:
//  1. https://core.telegram.org/api/business#away-messages
//
// See https://core.telegram.org/method/account.updateBusinessAwayMessage for reference.
type AccountUpdateBusinessAwayMessageRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Away message configuration and contents.
	//
	// Use SetMessage and GetMessage helpers.
	Message InputBusinessAwayMessage
}

// AccountUpdateBusinessAwayMessageRequestTypeID is TL type id of AccountUpdateBusinessAwayMessageRequest.
const AccountUpdateBusinessAwayMessageRequestTypeID = 0xa26a7fa5

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

func (u *AccountUpdateBusinessAwayMessageRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.Message.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills AccountUpdateBusinessAwayMessageRequest from given interface.
func (u *AccountUpdateBusinessAwayMessageRequest) FillFrom(from interface {
	GetMessage() (value InputBusinessAwayMessage, ok bool)
}) {
	if val, ok := from.GetMessage(); ok {
		u.Message = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*AccountUpdateBusinessAwayMessageRequest) TypeName() string {
	return "account.updateBusinessAwayMessage"
}

// TypeInfo returns info about TL type.
func (u *AccountUpdateBusinessAwayMessageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.updateBusinessAwayMessage",
		ID:   AccountUpdateBusinessAwayMessageRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Message",
			SchemaName: "message",
			Null:       !u.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *AccountUpdateBusinessAwayMessageRequest) SetFlags() {
	if !(u.Message.Zero()) {
		u.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (u *AccountUpdateBusinessAwayMessageRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateBusinessAwayMessage#a26a7fa5 as nil")
	}
	b.PutID(AccountUpdateBusinessAwayMessageRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *AccountUpdateBusinessAwayMessageRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateBusinessAwayMessage#a26a7fa5 as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.updateBusinessAwayMessage#a26a7fa5: field flags: %w", err)
	}
	if u.Flags.Has(0) {
		if err := u.Message.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.updateBusinessAwayMessage#a26a7fa5: field message: %w", err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *AccountUpdateBusinessAwayMessageRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode account.updateBusinessAwayMessage#a26a7fa5 to nil")
	}
	if err := b.ConsumeID(AccountUpdateBusinessAwayMessageRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.updateBusinessAwayMessage#a26a7fa5: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *AccountUpdateBusinessAwayMessageRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode account.updateBusinessAwayMessage#a26a7fa5 to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.updateBusinessAwayMessage#a26a7fa5: field flags: %w", err)
		}
	}
	if u.Flags.Has(0) {
		if err := u.Message.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.updateBusinessAwayMessage#a26a7fa5: field message: %w", err)
		}
	}
	return nil
}

// SetMessage sets value of Message conditional field.
func (u *AccountUpdateBusinessAwayMessageRequest) SetMessage(value InputBusinessAwayMessage) {
	u.Flags.Set(0)
	u.Message = value
}

// GetMessage returns value of Message conditional field and
// boolean which is true if field was set.
func (u *AccountUpdateBusinessAwayMessageRequest) GetMessage() (value InputBusinessAwayMessage, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(0) {
		return value, false
	}
	return u.Message, true
}

// AccountUpdateBusinessAwayMessage invokes method account.updateBusinessAwayMessage#a26a7fa5 returning error if any.
// Set a list of Telegram Business away messages¹.
//
// Links:
//  1. https://core.telegram.org/api/business#away-messages
//
// See https://core.telegram.org/method/account.updateBusinessAwayMessage for reference.
func (c *Client) AccountUpdateBusinessAwayMessage(ctx context.Context, request *AccountUpdateBusinessAwayMessageRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
