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

// CallID represents TL type `callId#3eac609`.
type CallID struct {
	// Call identifier
	ID int32
}

// CallIDTypeID is TL type id of CallID.
const CallIDTypeID = 0x3eac609

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

func (c *CallID) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CallID) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "callId",
		ID:   CallIDTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CallID) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode callId#3eac609 as nil")
	}
	b.PutID(CallIDTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CallID) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode callId#3eac609 as nil")
	}
	b.PutInt32(c.ID)
	return nil
}

// Decode implements bin.Decoder.
func (c *CallID) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode callId#3eac609 to nil")
	}
	if err := b.ConsumeID(CallIDTypeID); err != nil {
		return fmt.Errorf("unable to decode callId#3eac609: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CallID) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode callId#3eac609 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode callId#3eac609: field id: %w", err)
		}
		c.ID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CallID) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode callId#3eac609 as nil")
	}
	b.ObjStart()
	b.PutID("callId")
	b.Comma()
	b.FieldStart("id")
	b.PutInt32(c.ID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CallID) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode callId#3eac609 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("callId"); err != nil {
				return fmt.Errorf("unable to decode callId#3eac609: %w", err)
			}
		case "id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode callId#3eac609: field id: %w", err)
			}
			c.ID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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