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

// SetAccountTTLRequest represents TL type `setAccountTtl#29ce58e8`.
type SetAccountTTLRequest struct {
	// New account TTL
	TTL AccountTTL
}

// SetAccountTTLRequestTypeID is TL type id of SetAccountTTLRequest.
const SetAccountTTLRequestTypeID = 0x29ce58e8

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (s *SetAccountTTLRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAccountTtl#29ce58e8 as nil")
	}
	b.PutID(SetAccountTTLRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetAccountTTLRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setAccountTtl#29ce58e8 as nil")
	}
	if err := s.TTL.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setAccountTtl#29ce58e8: field ttl: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetAccountTTLRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setAccountTtl#29ce58e8 to nil")
	}
	{
		if err := s.TTL.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setAccountTtl#29ce58e8: field ttl: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetAccountTTLRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setAccountTtl#29ce58e8 as nil")
	}
	b.ObjStart()
	b.PutID("setAccountTtl")
	b.Comma()
	b.FieldStart("ttl")
	if err := s.TTL.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setAccountTtl#29ce58e8: field ttl: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setAccountTtl"); err != nil {
				return fmt.Errorf("unable to decode setAccountTtl#29ce58e8: %w", err)
			}
		case "ttl":
			if err := s.TTL.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setAccountTtl#29ce58e8: field ttl: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTTL returns value of TTL field.
func (s *SetAccountTTLRequest) GetTTL() (value AccountTTL) {
	if s == nil {
		return
	}
	return s.TTL
}

// SetAccountTTL invokes method setAccountTtl#29ce58e8 returning error if any.
func (c *Client) SetAccountTTL(ctx context.Context, ttl AccountTTL) error {
	var ok Ok

	request := &SetAccountTTLRequest{
		TTL: ttl,
	}
	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
