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

// SetAlarmRequest represents TL type `setAlarm#cbef7e15`.
type SetAlarmRequest struct {
	// Number of seconds before the function returns
	Seconds float64
}

// SetAlarmRequestTypeID is TL type id of SetAlarmRequest.
const SetAlarmRequestTypeID = 0xcbef7e15

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (s *SetAlarmRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAlarm#cbef7e15 as nil")
	}
	b.PutID(SetAlarmRequestTypeID)
	return s.EncodeBare(b)
}

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

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

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

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

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

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

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

// SetAlarm invokes method setAlarm#cbef7e15 returning error if any.
func (c *Client) SetAlarm(ctx context.Context, seconds float64) error {
	var ok Ok

	request := &SetAlarmRequest{
		Seconds: seconds,
	}
	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
