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

// AllowBotToSendMessagesRequest represents TL type `allowBotToSendMessages#69e9c58e`.
type AllowBotToSendMessagesRequest struct {
	// Identifier of the target bot
	BotUserID int64
}

// AllowBotToSendMessagesRequestTypeID is TL type id of AllowBotToSendMessagesRequest.
const AllowBotToSendMessagesRequestTypeID = 0x69e9c58e

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

func (a *AllowBotToSendMessagesRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.BotUserID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AllowBotToSendMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "allowBotToSendMessages",
		ID:   AllowBotToSendMessagesRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AllowBotToSendMessagesRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode allowBotToSendMessages#69e9c58e as nil")
	}
	b.PutID(AllowBotToSendMessagesRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AllowBotToSendMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode allowBotToSendMessages#69e9c58e as nil")
	}
	b.PutInt53(a.BotUserID)
	return nil
}

// Decode implements bin.Decoder.
func (a *AllowBotToSendMessagesRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode allowBotToSendMessages#69e9c58e to nil")
	}
	if err := b.ConsumeID(AllowBotToSendMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode allowBotToSendMessages#69e9c58e: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AllowBotToSendMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode allowBotToSendMessages#69e9c58e to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode allowBotToSendMessages#69e9c58e: field bot_user_id: %w", err)
		}
		a.BotUserID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AllowBotToSendMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode allowBotToSendMessages#69e9c58e as nil")
	}
	b.ObjStart()
	b.PutID("allowBotToSendMessages")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(a.BotUserID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AllowBotToSendMessagesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode allowBotToSendMessages#69e9c58e to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("allowBotToSendMessages"); err != nil {
				return fmt.Errorf("unable to decode allowBotToSendMessages#69e9c58e: %w", err)
			}
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode allowBotToSendMessages#69e9c58e: field bot_user_id: %w", err)
			}
			a.BotUserID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBotUserID returns value of BotUserID field.
func (a *AllowBotToSendMessagesRequest) GetBotUserID() (value int64) {
	if a == nil {
		return
	}
	return a.BotUserID
}

// AllowBotToSendMessages invokes method allowBotToSendMessages#69e9c58e returning error if any.
func (c *Client) AllowBotToSendMessages(ctx context.Context, botuserid int64) error {
	var ok Ok

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