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

// Seconds represents TL type `seconds#3936e58e`.
type Seconds struct {
	// Number of seconds
	Seconds float64
}

// SecondsTypeID is TL type id of Seconds.
const SecondsTypeID = 0x3936e58e

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

func (s *Seconds) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Seconds == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *Seconds) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "seconds",
		ID:   SecondsTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Seconds",
			SchemaName: "seconds",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *Seconds) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode seconds#3936e58e as nil")
	}
	b.PutID(SecondsTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *Seconds) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode seconds#3936e58e as nil")
	}
	b.PutDouble(s.Seconds)
	return nil
}

// Decode implements bin.Decoder.
func (s *Seconds) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode seconds#3936e58e to nil")
	}
	if err := b.ConsumeID(SecondsTypeID); err != nil {
		return fmt.Errorf("unable to decode seconds#3936e58e: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *Seconds) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode seconds#3936e58e to nil")
	}
	{
		value, err := b.Double()
		if err != nil {
			return fmt.Errorf("unable to decode seconds#3936e58e: field seconds: %w", err)
		}
		s.Seconds = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *Seconds) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode seconds#3936e58e as nil")
	}
	b.ObjStart()
	b.PutID("seconds")
	b.Comma()
	b.FieldStart("seconds")
	b.PutDouble(s.Seconds)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *Seconds) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode seconds#3936e58e to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("seconds"); err != nil {
				return fmt.Errorf("unable to decode seconds#3936e58e: %w", err)
			}
		case "seconds":
			value, err := b.Double()
			if err != nil {
				return fmt.Errorf("unable to decode seconds#3936e58e: field seconds: %w", err)
			}
			s.Seconds = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSeconds returns value of Seconds field.
func (s *Seconds) GetSeconds() (value float64) {
	if s == nil {
		return
	}
	return s.Seconds
}
