// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// PhoneGetGroupCallChainBlocksRequest represents TL type `phone.getGroupCallChainBlocks#ee9f88a6`.
// Fetch the blocks of a conference blockchain »¹.
//
// Links:
//  1. https://core.telegram.org/api/end-to-end/group-calls
//
// See https://core.telegram.org/method/phone.getGroupCallChainBlocks for reference.
type PhoneGetGroupCallChainBlocksRequest struct {
	// The conference.
	Call InputGroupCallClass
	// Subchain ID.
	SubChainID int
	// Offset for pagination.
	Offset int
	// Maximum number of blocks to return in this call, see pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Limit int
}

// PhoneGetGroupCallChainBlocksRequestTypeID is TL type id of PhoneGetGroupCallChainBlocksRequest.
const PhoneGetGroupCallChainBlocksRequestTypeID = 0xee9f88a6

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

func (g *PhoneGetGroupCallChainBlocksRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Call == nil) {
		return false
	}
	if !(g.SubChainID == 0) {
		return false
	}
	if !(g.Offset == 0) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

// FillFrom fills PhoneGetGroupCallChainBlocksRequest from given interface.
func (g *PhoneGetGroupCallChainBlocksRequest) FillFrom(from interface {
	GetCall() (value InputGroupCallClass)
	GetSubChainID() (value int)
	GetOffset() (value int)
	GetLimit() (value int)
}) {
	g.Call = from.GetCall()
	g.SubChainID = from.GetSubChainID()
	g.Offset = from.GetOffset()
	g.Limit = from.GetLimit()
}

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

// TypeName returns name of type in TL schema.
func (*PhoneGetGroupCallChainBlocksRequest) TypeName() string {
	return "phone.getGroupCallChainBlocks"
}

// TypeInfo returns info about TL type.
func (g *PhoneGetGroupCallChainBlocksRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phone.getGroupCallChainBlocks",
		ID:   PhoneGetGroupCallChainBlocksRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Call",
			SchemaName: "call",
		},
		{
			Name:       "SubChainID",
			SchemaName: "sub_chain_id",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *PhoneGetGroupCallChainBlocksRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode phone.getGroupCallChainBlocks#ee9f88a6 as nil")
	}
	b.PutID(PhoneGetGroupCallChainBlocksRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *PhoneGetGroupCallChainBlocksRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode phone.getGroupCallChainBlocks#ee9f88a6 as nil")
	}
	if g.Call == nil {
		return fmt.Errorf("unable to encode phone.getGroupCallChainBlocks#ee9f88a6: field call is nil")
	}
	if err := g.Call.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phone.getGroupCallChainBlocks#ee9f88a6: field call: %w", err)
	}
	b.PutInt(g.SubChainID)
	b.PutInt(g.Offset)
	b.PutInt(g.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *PhoneGetGroupCallChainBlocksRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode phone.getGroupCallChainBlocks#ee9f88a6 to nil")
	}
	{
		value, err := DecodeInputGroupCall(b)
		if err != nil {
			return fmt.Errorf("unable to decode phone.getGroupCallChainBlocks#ee9f88a6: field call: %w", err)
		}
		g.Call = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode phone.getGroupCallChainBlocks#ee9f88a6: field sub_chain_id: %w", err)
		}
		g.SubChainID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode phone.getGroupCallChainBlocks#ee9f88a6: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode phone.getGroupCallChainBlocks#ee9f88a6: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// GetCall returns value of Call field.
func (g *PhoneGetGroupCallChainBlocksRequest) GetCall() (value InputGroupCallClass) {
	if g == nil {
		return
	}
	return g.Call
}

// GetSubChainID returns value of SubChainID field.
func (g *PhoneGetGroupCallChainBlocksRequest) GetSubChainID() (value int) {
	if g == nil {
		return
	}
	return g.SubChainID
}

// GetOffset returns value of Offset field.
func (g *PhoneGetGroupCallChainBlocksRequest) GetOffset() (value int) {
	if g == nil {
		return
	}
	return g.Offset
}

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

// PhoneGetGroupCallChainBlocks invokes method phone.getGroupCallChainBlocks#ee9f88a6 returning error if any.
// Fetch the blocks of a conference blockchain »¹.
//
// Links:
//  1. https://core.telegram.org/api/end-to-end/group-calls
//
// Possible errors:
//
//	400 GROUPCALL_INVALID: The specified group call is invalid.
//
// See https://core.telegram.org/method/phone.getGroupCallChainBlocks for reference.
func (c *Client) PhoneGetGroupCallChainBlocks(ctx context.Context, request *PhoneGetGroupCallChainBlocksRequest) (UpdatesClass, error) {
	var result UpdatesBox

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