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

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

// AccountUpdateBusinessGreetingMessageRequestTypeID is TL type id of AccountUpdateBusinessGreetingMessageRequest.
const AccountUpdateBusinessGreetingMessageRequestTypeID = 0x66cdafc4

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

func (u *AccountUpdateBusinessGreetingMessageRequest) 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 *AccountUpdateBusinessGreetingMessageRequest) String() string {
	if u == nil {
		return "AccountUpdateBusinessGreetingMessageRequest(nil)"
	}
	type Alias AccountUpdateBusinessGreetingMessageRequest
	return fmt.Sprintf("AccountUpdateBusinessGreetingMessageRequest%+v", Alias(*u))
}

// FillFrom fills AccountUpdateBusinessGreetingMessageRequest from given interface.
func (u *AccountUpdateBusinessGreetingMessageRequest) FillFrom(from interface {
	GetMessage() (value InputBusinessGreetingMessage, 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 (*AccountUpdateBusinessGreetingMessageRequest) TypeID() uint32 {
	return AccountUpdateBusinessGreetingMessageRequestTypeID
}

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

// TypeInfo returns info about TL type.
func (u *AccountUpdateBusinessGreetingMessageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.updateBusinessGreetingMessage",
		ID:   AccountUpdateBusinessGreetingMessageRequestTypeID,
	}
	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 *AccountUpdateBusinessGreetingMessageRequest) SetFlags() {
	if !(u.Message.Zero()) {
		u.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (u *AccountUpdateBusinessGreetingMessageRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateBusinessGreetingMessage#66cdafc4 as nil")
	}
	b.PutID(AccountUpdateBusinessGreetingMessageRequestTypeID)
	return u.EncodeBare(b)
}

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

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

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

// SetMessage sets value of Message conditional field.
func (u *AccountUpdateBusinessGreetingMessageRequest) SetMessage(value InputBusinessGreetingMessage) {
	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 *AccountUpdateBusinessGreetingMessageRequest) GetMessage() (value InputBusinessGreetingMessage, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(0) {
		return value, false
	}
	return u.Message, true
}

// AccountUpdateBusinessGreetingMessage invokes method account.updateBusinessGreetingMessage#66cdafc4 returning error if any.
// Set a list of Telegram Business greeting messages¹.
//
// Links:
//  1. https://core.telegram.org/api/business#greeting-messages
//
// See https://core.telegram.org/method/account.updateBusinessGreetingMessage for reference.
func (c *Client) AccountUpdateBusinessGreetingMessage(ctx context.Context, request *AccountUpdateBusinessGreetingMessageRequest) (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
}
