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

// PingProxyRequest represents TL type `pingProxy#c59b40b1`.
type PingProxyRequest struct {
	// Proxy identifier. Use 0 to ping a Telegram server without a proxy
	ProxyID int32
}

// PingProxyRequestTypeID is TL type id of PingProxyRequest.
const PingProxyRequestTypeID = 0xc59b40b1

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

func (p *PingProxyRequest) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.ProxyID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PingProxyRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "pingProxy",
		ID:   PingProxyRequestTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ProxyID",
			SchemaName: "proxy_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PingProxyRequest) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pingProxy#c59b40b1 as nil")
	}
	b.PutID(PingProxyRequestTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PingProxyRequest) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode pingProxy#c59b40b1 as nil")
	}
	b.PutInt32(p.ProxyID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *PingProxyRequest) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode pingProxy#c59b40b1 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode pingProxy#c59b40b1: field proxy_id: %w", err)
		}
		p.ProxyID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PingProxyRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode pingProxy#c59b40b1 as nil")
	}
	b.ObjStart()
	b.PutID("pingProxy")
	b.Comma()
	b.FieldStart("proxy_id")
	b.PutInt32(p.ProxyID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetProxyID returns value of ProxyID field.
func (p *PingProxyRequest) GetProxyID() (value int32) {
	if p == nil {
		return
	}
	return p.ProxyID
}

// PingProxy invokes method pingProxy#c59b40b1 returning error if any.
func (c *Client) PingProxy(ctx context.Context, proxyid int32) (*Seconds, error) {
	var result Seconds

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