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

// SetBusinessAccountUsernameRequest represents TL type `setBusinessAccountUsername#a564b661`.
type SetBusinessAccountUsernameRequest struct {
	// Unique identifier of business connection
	BusinessConnectionID string
	// The new value of the username
	Username string
}

// SetBusinessAccountUsernameRequestTypeID is TL type id of SetBusinessAccountUsernameRequest.
const SetBusinessAccountUsernameRequestTypeID = 0xa564b661

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

func (s *SetBusinessAccountUsernameRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.BusinessConnectionID == "") {
		return false
	}
	if !(s.Username == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetBusinessAccountUsernameRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setBusinessAccountUsername",
		ID:   SetBusinessAccountUsernameRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "Username",
			SchemaName: "username",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetBusinessAccountUsernameRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessAccountUsername#a564b661 as nil")
	}
	b.PutID(SetBusinessAccountUsernameRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetBusinessAccountUsernameRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessAccountUsername#a564b661 as nil")
	}
	b.PutString(s.BusinessConnectionID)
	b.PutString(s.Username)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetBusinessAccountUsernameRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setBusinessAccountUsername#a564b661 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setBusinessAccountUsername#a564b661: field business_connection_id: %w", err)
		}
		s.BusinessConnectionID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setBusinessAccountUsername#a564b661: field username: %w", err)
		}
		s.Username = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetBusinessAccountUsernameRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessAccountUsername#a564b661 as nil")
	}
	b.ObjStart()
	b.PutID("setBusinessAccountUsername")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(s.BusinessConnectionID)
	b.Comma()
	b.FieldStart("username")
	b.PutString(s.Username)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetBusinessAccountUsernameRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setBusinessAccountUsername#a564b661 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setBusinessAccountUsername"); err != nil {
				return fmt.Errorf("unable to decode setBusinessAccountUsername#a564b661: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setBusinessAccountUsername#a564b661: field business_connection_id: %w", err)
			}
			s.BusinessConnectionID = value
		case "username":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setBusinessAccountUsername#a564b661: field username: %w", err)
			}
			s.Username = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBusinessConnectionID returns value of BusinessConnectionID field.
func (s *SetBusinessAccountUsernameRequest) GetBusinessConnectionID() (value string) {
	if s == nil {
		return
	}
	return s.BusinessConnectionID
}

// GetUsername returns value of Username field.
func (s *SetBusinessAccountUsernameRequest) GetUsername() (value string) {
	if s == nil {
		return
	}
	return s.Username
}

// SetBusinessAccountUsername invokes method setBusinessAccountUsername#a564b661 returning error if any.
func (c *Client) SetBusinessAccountUsername(ctx context.Context, request *SetBusinessAccountUsernameRequest) error {
	var ok Ok

	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
