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

// TestSquareIntRequest represents TL type `testSquareInt#fc6a6990`.
type TestSquareIntRequest struct {
	// Number to square
	X int32
}

// TestSquareIntRequestTypeID is TL type id of TestSquareIntRequest.
const TestSquareIntRequestTypeID = 0xfc6a6990

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

func (t *TestSquareIntRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.X == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TestSquareIntRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "testSquareInt",
		ID:   TestSquareIntRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "X",
			SchemaName: "x",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TestSquareIntRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testSquareInt#fc6a6990 as nil")
	}
	b.PutID(TestSquareIntRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TestSquareIntRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode testSquareInt#fc6a6990 as nil")
	}
	b.PutInt32(t.X)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (t *TestSquareIntRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode testSquareInt#fc6a6990 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode testSquareInt#fc6a6990: field x: %w", err)
		}
		t.X = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TestSquareIntRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode testSquareInt#fc6a6990 as nil")
	}
	b.ObjStart()
	b.PutID("testSquareInt")
	b.Comma()
	b.FieldStart("x")
	b.PutInt32(t.X)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("testSquareInt"); err != nil {
				return fmt.Errorf("unable to decode testSquareInt#fc6a6990: %w", err)
			}
		case "x":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode testSquareInt#fc6a6990: field x: %w", err)
			}
			t.X = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetX returns value of X field.
func (t *TestSquareIntRequest) GetX() (value int32) {
	if t == nil {
		return
	}
	return t.X
}

// TestSquareInt invokes method testSquareInt#fc6a6990 returning error if any.
func (c *Client) TestSquareInt(ctx context.Context, x int32) (*TestInt, error) {
	var result TestInt

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