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

// CloseChatRequest represents TL type `closeChat#25e86e9`.
type CloseChatRequest struct {
	// Chat identifier
	ChatID int64
}

// CloseChatRequestTypeID is TL type id of CloseChatRequest.
const CloseChatRequestTypeID = 0x25e86e9

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CloseChatRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "closeChat",
		ID:   CloseChatRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CloseChatRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode closeChat#25e86e9 as nil")
	}
	b.PutID(CloseChatRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CloseChatRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode closeChat#25e86e9 as nil")
	}
	b.PutInt53(c.ChatID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CloseChatRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode closeChat#25e86e9 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode closeChat#25e86e9: field chat_id: %w", err)
		}
		c.ChatID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CloseChatRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode closeChat#25e86e9 as nil")
	}
	b.ObjStart()
	b.PutID("closeChat")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(c.ChatID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// CloseChat invokes method closeChat#25e86e9 returning error if any.
func (c *Client) CloseChat(ctx context.Context, chatid int64) error {
	var ok Ok

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