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

// GetGroupsInCommonRequest represents TL type `getGroupsInCommon#16bdd36a`.
type GetGroupsInCommonRequest struct {
	// User identifier
	UserID int64
	// Chat identifier starting from which to return chats; use 0 for the first request
	OffsetChatID int64
	// The maximum number of chats to be returned; up to 100
	Limit int32
}

// GetGroupsInCommonRequestTypeID is TL type id of GetGroupsInCommonRequest.
const GetGroupsInCommonRequestTypeID = 0x16bdd36a

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

func (g *GetGroupsInCommonRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.UserID == 0) {
		return false
	}
	if !(g.OffsetChatID == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetGroupsInCommonRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getGroupsInCommon",
		ID:   GetGroupsInCommonRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "OffsetChatID",
			SchemaName: "offset_chat_id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetGroupsInCommonRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getGroupsInCommon#16bdd36a as nil")
	}
	b.PutID(GetGroupsInCommonRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetGroupsInCommonRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getGroupsInCommon#16bdd36a as nil")
	}
	b.PutInt53(g.UserID)
	b.PutInt53(g.OffsetChatID)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetGroupsInCommonRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getGroupsInCommon#16bdd36a to nil")
	}
	if err := b.ConsumeID(GetGroupsInCommonRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getGroupsInCommon#16bdd36a: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetGroupsInCommonRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getGroupsInCommon#16bdd36a to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getGroupsInCommon#16bdd36a: field user_id: %w", err)
		}
		g.UserID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getGroupsInCommon#16bdd36a: field offset_chat_id: %w", err)
		}
		g.OffsetChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getGroupsInCommon#16bdd36a: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetGroupsInCommonRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getGroupsInCommon#16bdd36a as nil")
	}
	b.ObjStart()
	b.PutID("getGroupsInCommon")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(g.UserID)
	b.Comma()
	b.FieldStart("offset_chat_id")
	b.PutInt53(g.OffsetChatID)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetGroupsInCommonRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getGroupsInCommon#16bdd36a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getGroupsInCommon"); err != nil {
				return fmt.Errorf("unable to decode getGroupsInCommon#16bdd36a: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getGroupsInCommon#16bdd36a: field user_id: %w", err)
			}
			g.UserID = value
		case "offset_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getGroupsInCommon#16bdd36a: field offset_chat_id: %w", err)
			}
			g.OffsetChatID = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getGroupsInCommon#16bdd36a: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (g *GetGroupsInCommonRequest) GetUserID() (value int64) {
	if g == nil {
		return
	}
	return g.UserID
}

// GetOffsetChatID returns value of OffsetChatID field.
func (g *GetGroupsInCommonRequest) GetOffsetChatID() (value int64) {
	if g == nil {
		return
	}
	return g.OffsetChatID
}

// GetLimit returns value of Limit field.
func (g *GetGroupsInCommonRequest) GetLimit() (value int32) {
	if g == nil {
		return
	}
	return g.Limit
}

// GetGroupsInCommon invokes method getGroupsInCommon#16bdd36a returning error if any.
func (c *Client) GetGroupsInCommon(ctx context.Context, request *GetGroupsInCommonRequest) (*Chats, error) {
	var result Chats

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