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

// AllowUnpaidMessagesFromUserRequest represents TL type `allowUnpaidMessagesFromUser#2fe57f57`.
type AllowUnpaidMessagesFromUserRequest struct {
	// Identifier of the user
	UserID int64
	// Pass true to refund the user previously paid messages
	RefundPayments bool
}

// AllowUnpaidMessagesFromUserRequestTypeID is TL type id of AllowUnpaidMessagesFromUserRequest.
const AllowUnpaidMessagesFromUserRequestTypeID = 0x2fe57f57

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

func (a *AllowUnpaidMessagesFromUserRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.UserID == 0) {
		return false
	}
	if !(a.RefundPayments == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AllowUnpaidMessagesFromUserRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "allowUnpaidMessagesFromUser",
		ID:   AllowUnpaidMessagesFromUserRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "RefundPayments",
			SchemaName: "refund_payments",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AllowUnpaidMessagesFromUserRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode allowUnpaidMessagesFromUser#2fe57f57 as nil")
	}
	b.PutID(AllowUnpaidMessagesFromUserRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AllowUnpaidMessagesFromUserRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode allowUnpaidMessagesFromUser#2fe57f57 as nil")
	}
	b.PutInt53(a.UserID)
	b.PutBool(a.RefundPayments)
	return nil
}

// Decode implements bin.Decoder.
func (a *AllowUnpaidMessagesFromUserRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode allowUnpaidMessagesFromUser#2fe57f57 to nil")
	}
	if err := b.ConsumeID(AllowUnpaidMessagesFromUserRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode allowUnpaidMessagesFromUser#2fe57f57: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AllowUnpaidMessagesFromUserRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode allowUnpaidMessagesFromUser#2fe57f57 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode allowUnpaidMessagesFromUser#2fe57f57: field user_id: %w", err)
		}
		a.UserID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode allowUnpaidMessagesFromUser#2fe57f57: field refund_payments: %w", err)
		}
		a.RefundPayments = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AllowUnpaidMessagesFromUserRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode allowUnpaidMessagesFromUser#2fe57f57 as nil")
	}
	b.ObjStart()
	b.PutID("allowUnpaidMessagesFromUser")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(a.UserID)
	b.Comma()
	b.FieldStart("refund_payments")
	b.PutBool(a.RefundPayments)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AllowUnpaidMessagesFromUserRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode allowUnpaidMessagesFromUser#2fe57f57 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("allowUnpaidMessagesFromUser"); err != nil {
				return fmt.Errorf("unable to decode allowUnpaidMessagesFromUser#2fe57f57: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode allowUnpaidMessagesFromUser#2fe57f57: field user_id: %w", err)
			}
			a.UserID = value
		case "refund_payments":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode allowUnpaidMessagesFromUser#2fe57f57: field refund_payments: %w", err)
			}
			a.RefundPayments = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetRefundPayments returns value of RefundPayments field.
func (a *AllowUnpaidMessagesFromUserRequest) GetRefundPayments() (value bool) {
	if a == nil {
		return
	}
	return a.RefundPayments
}

// AllowUnpaidMessagesFromUser invokes method allowUnpaidMessagesFromUser#2fe57f57 returning error if any.
func (c *Client) AllowUnpaidMessagesFromUser(ctx context.Context, request *AllowUnpaidMessagesFromUserRequest) error {
	var ok Ok

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