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

// CloseSecretChatRequest represents TL type `closeSecretChat#e3ed044b`.
type CloseSecretChatRequest struct {
	// Secret chat identifier
	SecretChatID int32
}

// CloseSecretChatRequestTypeID is TL type id of CloseSecretChatRequest.
const CloseSecretChatRequestTypeID = 0xe3ed044b

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CloseSecretChatRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "closeSecretChat",
		ID:   CloseSecretChatRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SecretChatID",
			SchemaName: "secret_chat_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CloseSecretChatRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode closeSecretChat#e3ed044b as nil")
	}
	b.PutID(CloseSecretChatRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CloseSecretChatRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode closeSecretChat#e3ed044b as nil")
	}
	b.PutInt32(c.SecretChatID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CloseSecretChatRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode closeSecretChat#e3ed044b to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode closeSecretChat#e3ed044b: field secret_chat_id: %w", err)
		}
		c.SecretChatID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CloseSecretChatRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode closeSecretChat#e3ed044b as nil")
	}
	b.ObjStart()
	b.PutID("closeSecretChat")
	b.Comma()
	b.FieldStart("secret_chat_id")
	b.PutInt32(c.SecretChatID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetSecretChatID returns value of SecretChatID field.
func (c *CloseSecretChatRequest) GetSecretChatID() (value int32) {
	if c == nil {
		return
	}
	return c.SecretChatID
}

// CloseSecretChat invokes method closeSecretChat#e3ed044b returning error if any.
func (c *Client) CloseSecretChat(ctx context.Context, secretchatid int32) error {
	var ok Ok

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