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

// AnswerCustomQueryRequest represents TL type `answerCustomQuery#b2e52d3f`.
type AnswerCustomQueryRequest struct {
	// Identifier of a custom query
	CustomQueryID int64
	// JSON-serialized answer to the query
	Data string
}

// AnswerCustomQueryRequestTypeID is TL type id of AnswerCustomQueryRequest.
const AnswerCustomQueryRequestTypeID = 0xb2e52d3f

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

func (a *AnswerCustomQueryRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.CustomQueryID == 0) {
		return false
	}
	if !(a.Data == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AnswerCustomQueryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "answerCustomQuery",
		ID:   AnswerCustomQueryRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CustomQueryID",
			SchemaName: "custom_query_id",
		},
		{
			Name:       "Data",
			SchemaName: "data",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AnswerCustomQueryRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode answerCustomQuery#b2e52d3f as nil")
	}
	b.PutID(AnswerCustomQueryRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AnswerCustomQueryRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode answerCustomQuery#b2e52d3f as nil")
	}
	b.PutLong(a.CustomQueryID)
	b.PutString(a.Data)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (a *AnswerCustomQueryRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode answerCustomQuery#b2e52d3f to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode answerCustomQuery#b2e52d3f: field custom_query_id: %w", err)
		}
		a.CustomQueryID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode answerCustomQuery#b2e52d3f: field data: %w", err)
		}
		a.Data = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AnswerCustomQueryRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode answerCustomQuery#b2e52d3f as nil")
	}
	b.ObjStart()
	b.PutID("answerCustomQuery")
	b.Comma()
	b.FieldStart("custom_query_id")
	b.PutLong(a.CustomQueryID)
	b.Comma()
	b.FieldStart("data")
	b.PutString(a.Data)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("answerCustomQuery"); err != nil {
				return fmt.Errorf("unable to decode answerCustomQuery#b2e52d3f: %w", err)
			}
		case "custom_query_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode answerCustomQuery#b2e52d3f: field custom_query_id: %w", err)
			}
			a.CustomQueryID = value
		case "data":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode answerCustomQuery#b2e52d3f: field data: %w", err)
			}
			a.Data = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCustomQueryID returns value of CustomQueryID field.
func (a *AnswerCustomQueryRequest) GetCustomQueryID() (value int64) {
	if a == nil {
		return
	}
	return a.CustomQueryID
}

// GetData returns value of Data field.
func (a *AnswerCustomQueryRequest) GetData() (value string) {
	if a == nil {
		return
	}
	return a.Data
}

// AnswerCustomQuery invokes method answerCustomQuery#b2e52d3f returning error if any.
func (c *Client) AnswerCustomQuery(ctx context.Context, request *AnswerCustomQueryRequest) error {
	var ok Ok

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