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

// CanSendMessageToUserRequest represents TL type `canSendMessageToUser#5b2a2836`.
type CanSendMessageToUserRequest struct {
	// Identifier of the other user
	UserID int64
	// Pass true to get only locally available information without sending network requests
	OnlyLocal bool
}

// CanSendMessageToUserRequestTypeID is TL type id of CanSendMessageToUserRequest.
const CanSendMessageToUserRequestTypeID = 0x5b2a2836

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

func (c *CanSendMessageToUserRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.UserID == 0) {
		return false
	}
	if !(c.OnlyLocal == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CanSendMessageToUserRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "canSendMessageToUser",
		ID:   CanSendMessageToUserRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "OnlyLocal",
			SchemaName: "only_local",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CanSendMessageToUserRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode canSendMessageToUser#5b2a2836 as nil")
	}
	b.PutID(CanSendMessageToUserRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CanSendMessageToUserRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode canSendMessageToUser#5b2a2836 as nil")
	}
	b.PutInt53(c.UserID)
	b.PutBool(c.OnlyLocal)
	return nil
}

// Decode implements bin.Decoder.
func (c *CanSendMessageToUserRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode canSendMessageToUser#5b2a2836 to nil")
	}
	if err := b.ConsumeID(CanSendMessageToUserRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode canSendMessageToUser#5b2a2836: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CanSendMessageToUserRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode canSendMessageToUser#5b2a2836 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode canSendMessageToUser#5b2a2836: field user_id: %w", err)
		}
		c.UserID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode canSendMessageToUser#5b2a2836: field only_local: %w", err)
		}
		c.OnlyLocal = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CanSendMessageToUserRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode canSendMessageToUser#5b2a2836 as nil")
	}
	b.ObjStart()
	b.PutID("canSendMessageToUser")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(c.UserID)
	b.Comma()
	b.FieldStart("only_local")
	b.PutBool(c.OnlyLocal)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CanSendMessageToUserRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode canSendMessageToUser#5b2a2836 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("canSendMessageToUser"); err != nil {
				return fmt.Errorf("unable to decode canSendMessageToUser#5b2a2836: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode canSendMessageToUser#5b2a2836: field user_id: %w", err)
			}
			c.UserID = value
		case "only_local":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode canSendMessageToUser#5b2a2836: field only_local: %w", err)
			}
			c.OnlyLocal = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (c *CanSendMessageToUserRequest) GetUserID() (value int64) {
	if c == nil {
		return
	}
	return c.UserID
}

// GetOnlyLocal returns value of OnlyLocal field.
func (c *CanSendMessageToUserRequest) GetOnlyLocal() (value bool) {
	if c == nil {
		return
	}
	return c.OnlyLocal
}

// CanSendMessageToUser invokes method canSendMessageToUser#5b2a2836 returning error if any.
func (c *Client) CanSendMessageToUser(ctx context.Context, request *CanSendMessageToUserRequest) (CanSendMessageToUserResultClass, error) {
	var result CanSendMessageToUserResultBox

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