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

// CallServer represents TL type `callServer#6f37df97`.
type CallServer struct {
	// Server identifier
	ID int64
	// Server IPv4 address
	IPAddress string
	// Server IPv6 address
	Ipv6Address string
	// Server port number
	Port int32
	// Server type
	Type CallServerTypeClass
}

// CallServerTypeID is TL type id of CallServer.
const CallServerTypeID = 0x6f37df97

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

func (c *CallServer) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ID == 0) {
		return false
	}
	if !(c.IPAddress == "") {
		return false
	}
	if !(c.Ipv6Address == "") {
		return false
	}
	if !(c.Port == 0) {
		return false
	}
	if !(c.Type == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CallServer) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "callServer",
		ID:   CallServerTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "IPAddress",
			SchemaName: "ip_address",
		},
		{
			Name:       "Ipv6Address",
			SchemaName: "ipv6_address",
		},
		{
			Name:       "Port",
			SchemaName: "port",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CallServer) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode callServer#6f37df97 as nil")
	}
	b.PutID(CallServerTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CallServer) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode callServer#6f37df97 as nil")
	}
	b.PutLong(c.ID)
	b.PutString(c.IPAddress)
	b.PutString(c.Ipv6Address)
	b.PutInt32(c.Port)
	if c.Type == nil {
		return fmt.Errorf("unable to encode callServer#6f37df97: field type is nil")
	}
	if err := c.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode callServer#6f37df97: field type: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *CallServer) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode callServer#6f37df97 to nil")
	}
	if err := b.ConsumeID(CallServerTypeID); err != nil {
		return fmt.Errorf("unable to decode callServer#6f37df97: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CallServer) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode callServer#6f37df97 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode callServer#6f37df97: field id: %w", err)
		}
		c.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode callServer#6f37df97: field ip_address: %w", err)
		}
		c.IPAddress = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode callServer#6f37df97: field ipv6_address: %w", err)
		}
		c.Ipv6Address = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode callServer#6f37df97: field port: %w", err)
		}
		c.Port = value
	}
	{
		value, err := DecodeCallServerType(b)
		if err != nil {
			return fmt.Errorf("unable to decode callServer#6f37df97: field type: %w", err)
		}
		c.Type = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CallServer) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode callServer#6f37df97 as nil")
	}
	b.ObjStart()
	b.PutID("callServer")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(c.ID)
	b.Comma()
	b.FieldStart("ip_address")
	b.PutString(c.IPAddress)
	b.Comma()
	b.FieldStart("ipv6_address")
	b.PutString(c.Ipv6Address)
	b.Comma()
	b.FieldStart("port")
	b.PutInt32(c.Port)
	b.Comma()
	b.FieldStart("type")
	if c.Type == nil {
		return fmt.Errorf("unable to encode callServer#6f37df97: field type is nil")
	}
	if err := c.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode callServer#6f37df97: field type: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CallServer) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode callServer#6f37df97 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("callServer"); err != nil {
				return fmt.Errorf("unable to decode callServer#6f37df97: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode callServer#6f37df97: field id: %w", err)
			}
			c.ID = value
		case "ip_address":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode callServer#6f37df97: field ip_address: %w", err)
			}
			c.IPAddress = value
		case "ipv6_address":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode callServer#6f37df97: field ipv6_address: %w", err)
			}
			c.Ipv6Address = value
		case "port":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode callServer#6f37df97: field port: %w", err)
			}
			c.Port = value
		case "type":
			value, err := DecodeTDLibJSONCallServerType(b)
			if err != nil {
				return fmt.Errorf("unable to decode callServer#6f37df97: field type: %w", err)
			}
			c.Type = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (c *CallServer) GetID() (value int64) {
	if c == nil {
		return
	}
	return c.ID
}

// GetIPAddress returns value of IPAddress field.
func (c *CallServer) GetIPAddress() (value string) {
	if c == nil {
		return
	}
	return c.IPAddress
}

// GetIpv6Address returns value of Ipv6Address field.
func (c *CallServer) GetIpv6Address() (value string) {
	if c == nil {
		return
	}
	return c.Ipv6Address
}

// GetPort returns value of Port field.
func (c *CallServer) GetPort() (value int32) {
	if c == nil {
		return
	}
	return c.Port
}

// GetType returns value of Type field.
func (c *CallServer) GetType() (value CallServerTypeClass) {
	if c == nil {
		return
	}
	return c.Type
}
