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

// GetChatMemberRequest represents TL type `getChatMember#d0c15272`.
type GetChatMemberRequest struct {
	// Chat identifier
	ChatID int64
	// Member identifier
	MemberID MessageSenderClass
}

// GetChatMemberRequestTypeID is TL type id of GetChatMemberRequest.
const GetChatMemberRequestTypeID = 0xd0c15272

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

func (g *GetChatMemberRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatID == 0) {
		return false
	}
	if !(g.MemberID == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatMemberRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChatMember",
		ID:   GetChatMemberRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MemberID",
			SchemaName: "member_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetChatMemberRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatMember#d0c15272 as nil")
	}
	b.PutID(GetChatMemberRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetChatMemberRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatMember#d0c15272 as nil")
	}
	b.PutInt53(g.ChatID)
	if g.MemberID == nil {
		return fmt.Errorf("unable to encode getChatMember#d0c15272: field member_id is nil")
	}
	if err := g.MemberID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getChatMember#d0c15272: field member_id: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetChatMemberRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatMember#d0c15272 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getChatMember#d0c15272: field chat_id: %w", err)
		}
		g.ChatID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode getChatMember#d0c15272: field member_id: %w", err)
		}
		g.MemberID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetChatMemberRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatMember#d0c15272 as nil")
	}
	b.ObjStart()
	b.PutID("getChatMember")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(g.ChatID)
	b.Comma()
	b.FieldStart("member_id")
	if g.MemberID == nil {
		return fmt.Errorf("unable to encode getChatMember#d0c15272: field member_id is nil")
	}
	if err := g.MemberID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getChatMember#d0c15272: field member_id: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// GetMemberID returns value of MemberID field.
func (g *GetChatMemberRequest) GetMemberID() (value MessageSenderClass) {
	if g == nil {
		return
	}
	return g.MemberID
}

// GetChatMember invokes method getChatMember#d0c15272 returning error if any.
func (c *Client) GetChatMember(ctx context.Context, request *GetChatMemberRequest) (*ChatMember, error) {
	var result ChatMember

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