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

// SetChatMessageAutoDeleteTimeRequest represents TL type `setChatMessageAutoDeleteTime#a641b4ff`.
type SetChatMessageAutoDeleteTimeRequest struct {
	// Chat identifier
	ChatID int64
	// New time value, in seconds; unless the chat is secret, it must be from 0 up to 365 *
	// 86400 and be divisible by 86400. If 0, then messages aren't deleted automatically
	MessageAutoDeleteTime int32
}

// SetChatMessageAutoDeleteTimeRequestTypeID is TL type id of SetChatMessageAutoDeleteTimeRequest.
const SetChatMessageAutoDeleteTimeRequestTypeID = 0xa641b4ff

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetChatMessageAutoDeleteTimeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setChatMessageAutoDeleteTime",
		ID:   SetChatMessageAutoDeleteTimeRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageAutoDeleteTime",
			SchemaName: "message_auto_delete_time",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetChatMessageAutoDeleteTimeRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatMessageAutoDeleteTime#a641b4ff as nil")
	}
	b.PutID(SetChatMessageAutoDeleteTimeRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetChatMessageAutoDeleteTimeRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatMessageAutoDeleteTime#a641b4ff as nil")
	}
	b.PutInt53(s.ChatID)
	b.PutInt32(s.MessageAutoDeleteTime)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetChatMessageAutoDeleteTimeRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatMessageAutoDeleteTime#a641b4ff to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setChatMessageAutoDeleteTime#a641b4ff: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode setChatMessageAutoDeleteTime#a641b4ff: field message_auto_delete_time: %w", err)
		}
		s.MessageAutoDeleteTime = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetChatMessageAutoDeleteTimeRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatMessageAutoDeleteTime#a641b4ff as nil")
	}
	b.ObjStart()
	b.PutID("setChatMessageAutoDeleteTime")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("message_auto_delete_time")
	b.PutInt32(s.MessageAutoDeleteTime)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setChatMessageAutoDeleteTime"); err != nil {
				return fmt.Errorf("unable to decode setChatMessageAutoDeleteTime#a641b4ff: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setChatMessageAutoDeleteTime#a641b4ff: field chat_id: %w", err)
			}
			s.ChatID = value
		case "message_auto_delete_time":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode setChatMessageAutoDeleteTime#a641b4ff: field message_auto_delete_time: %w", err)
			}
			s.MessageAutoDeleteTime = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (s *SetChatMessageAutoDeleteTimeRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

// GetMessageAutoDeleteTime returns value of MessageAutoDeleteTime field.
func (s *SetChatMessageAutoDeleteTimeRequest) GetMessageAutoDeleteTime() (value int32) {
	if s == nil {
		return
	}
	return s.MessageAutoDeleteTime
}

// SetChatMessageAutoDeleteTime invokes method setChatMessageAutoDeleteTime#a641b4ff returning error if any.
func (c *Client) SetChatMessageAutoDeleteTime(ctx context.Context, request *SetChatMessageAutoDeleteTimeRequest) error {
	var ok Ok

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