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

// GetChatFolderNewChatsRequest represents TL type `getChatFolderNewChats#7e8d2ccc`.
type GetChatFolderNewChatsRequest struct {
	// Chat folder identifier
	ChatFolderID int32
}

// GetChatFolderNewChatsRequestTypeID is TL type id of GetChatFolderNewChatsRequest.
const GetChatFolderNewChatsRequestTypeID = 0x7e8d2ccc

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

func (g *GetChatFolderNewChatsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatFolderID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatFolderNewChatsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChatFolderNewChats",
		ID:   GetChatFolderNewChatsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatFolderID",
			SchemaName: "chat_folder_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetChatFolderNewChatsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderNewChats#7e8d2ccc as nil")
	}
	b.PutID(GetChatFolderNewChatsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetChatFolderNewChatsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderNewChats#7e8d2ccc as nil")
	}
	b.PutInt32(g.ChatFolderID)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetChatFolderNewChatsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatFolderNewChats#7e8d2ccc to nil")
	}
	if err := b.ConsumeID(GetChatFolderNewChatsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getChatFolderNewChats#7e8d2ccc: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetChatFolderNewChatsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatFolderNewChats#7e8d2ccc to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getChatFolderNewChats#7e8d2ccc: field chat_folder_id: %w", err)
		}
		g.ChatFolderID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetChatFolderNewChatsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderNewChats#7e8d2ccc as nil")
	}
	b.ObjStart()
	b.PutID("getChatFolderNewChats")
	b.Comma()
	b.FieldStart("chat_folder_id")
	b.PutInt32(g.ChatFolderID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetChatFolderNewChatsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatFolderNewChats#7e8d2ccc to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getChatFolderNewChats"); err != nil {
				return fmt.Errorf("unable to decode getChatFolderNewChats#7e8d2ccc: %w", err)
			}
		case "chat_folder_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getChatFolderNewChats#7e8d2ccc: field chat_folder_id: %w", err)
			}
			g.ChatFolderID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatFolderID returns value of ChatFolderID field.
func (g *GetChatFolderNewChatsRequest) GetChatFolderID() (value int32) {
	if g == nil {
		return
	}
	return g.ChatFolderID
}

// GetChatFolderNewChats invokes method getChatFolderNewChats#7e8d2ccc returning error if any.
func (c *Client) GetChatFolderNewChats(ctx context.Context, chatfolderid int32) (*Chats, error) {
	var result Chats

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