// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// InvokeWithMessagesRangeRequest represents TL type `invokeWithMessagesRange#365275f2`.
// Invoke with the given message range
//
// See https://core.telegram.org/constructor/invokeWithMessagesRange for reference.
type InvokeWithMessagesRangeRequest struct {
	// Message range
	Range MessageRange
	// Query
	Query bin.Object
}

// InvokeWithMessagesRangeRequestTypeID is TL type id of InvokeWithMessagesRangeRequest.
const InvokeWithMessagesRangeRequestTypeID = 0x365275f2

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

func (i *InvokeWithMessagesRangeRequest) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Range.Zero()) {
		return false
	}
	if !(i.Query == nil) {
		return false
	}

	return true
}

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

// FillFrom fills InvokeWithMessagesRangeRequest from given interface.
func (i *InvokeWithMessagesRangeRequest) FillFrom(from interface {
	GetRange() (value MessageRange)
	GetQuery() (value bin.Object)
}) {
	i.Range = from.GetRange()
	i.Query = from.GetQuery()
}

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

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

// TypeInfo returns info about TL type.
func (i *InvokeWithMessagesRangeRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "invokeWithMessagesRange",
		ID:   InvokeWithMessagesRangeRequestTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Range",
			SchemaName: "range",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InvokeWithMessagesRangeRequest) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode invokeWithMessagesRange#365275f2 as nil")
	}
	b.PutID(InvokeWithMessagesRangeRequestTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InvokeWithMessagesRangeRequest) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode invokeWithMessagesRange#365275f2 as nil")
	}
	if err := i.Range.Encode(b); err != nil {
		return fmt.Errorf("unable to encode invokeWithMessagesRange#365275f2: field range: %w", err)
	}
	if err := i.Query.Encode(b); err != nil {
		return fmt.Errorf("unable to encode invokeWithMessagesRange#365275f2: field query: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *InvokeWithMessagesRangeRequest) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode invokeWithMessagesRange#365275f2 to nil")
	}
	if err := b.ConsumeID(InvokeWithMessagesRangeRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode invokeWithMessagesRange#365275f2: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InvokeWithMessagesRangeRequest) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode invokeWithMessagesRange#365275f2 to nil")
	}
	{
		if err := i.Range.Decode(b); err != nil {
			return fmt.Errorf("unable to decode invokeWithMessagesRange#365275f2: field range: %w", err)
		}
	}
	{
		if err := i.Query.Decode(b); err != nil {
			return fmt.Errorf("unable to decode invokeWithMessagesRange#365275f2: field query: %w", err)
		}
	}
	return nil
}

// GetRange returns value of Range field.
func (i *InvokeWithMessagesRangeRequest) GetRange() (value MessageRange) {
	if i == nil {
		return
	}
	return i.Range
}

// GetQuery returns value of Query field.
func (i *InvokeWithMessagesRangeRequest) GetQuery() (value bin.Object) {
	if i == nil {
		return
	}
	return i.Query
}
