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

// InlineQueryResults represents TL type `inlineQueryResults#ffd17579`.
type InlineQueryResults struct {
	// Unique identifier of the inline query
	InlineQueryID int64
	// Button to be shown above inline query results; may be null
	Button InlineQueryResultsButton
	// Results of the query
	Results []InlineQueryResultClass
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// InlineQueryResultsTypeID is TL type id of InlineQueryResults.
const InlineQueryResultsTypeID = 0xffd17579

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

func (i *InlineQueryResults) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.InlineQueryID == 0) {
		return false
	}
	if !(i.Button.Zero()) {
		return false
	}
	if !(i.Results == nil) {
		return false
	}
	if !(i.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InlineQueryResults) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inlineQueryResults",
		ID:   InlineQueryResultsTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "InlineQueryID",
			SchemaName: "inline_query_id",
		},
		{
			Name:       "Button",
			SchemaName: "button",
		},
		{
			Name:       "Results",
			SchemaName: "results",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InlineQueryResults) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inlineQueryResults#ffd17579 as nil")
	}
	b.PutID(InlineQueryResultsTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InlineQueryResults) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inlineQueryResults#ffd17579 as nil")
	}
	b.PutLong(i.InlineQueryID)
	if err := i.Button.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inlineQueryResults#ffd17579: field button: %w", err)
	}
	b.PutInt(len(i.Results))
	for idx, v := range i.Results {
		if v == nil {
			return fmt.Errorf("unable to encode inlineQueryResults#ffd17579: field results element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare inlineQueryResults#ffd17579: field results element with index %d: %w", idx, err)
		}
	}
	b.PutString(i.NextOffset)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (i *InlineQueryResults) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inlineQueryResults#ffd17579 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field inline_query_id: %w", err)
		}
		i.InlineQueryID = value
	}
	{
		if err := i.Button.Decode(b); err != nil {
			return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field button: %w", err)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field results: %w", err)
		}

		if headerLen > 0 {
			i.Results = make([]InlineQueryResultClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInlineQueryResult(b)
			if err != nil {
				return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field results: %w", err)
			}
			i.Results = append(i.Results, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field next_offset: %w", err)
		}
		i.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InlineQueryResults) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inlineQueryResults#ffd17579 as nil")
	}
	b.ObjStart()
	b.PutID("inlineQueryResults")
	b.Comma()
	b.FieldStart("inline_query_id")
	b.PutLong(i.InlineQueryID)
	b.Comma()
	b.FieldStart("button")
	if err := i.Button.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inlineQueryResults#ffd17579: field button: %w", err)
	}
	b.Comma()
	b.FieldStart("results")
	b.ArrStart()
	for idx, v := range i.Results {
		if v == nil {
			return fmt.Errorf("unable to encode inlineQueryResults#ffd17579: field results element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode inlineQueryResults#ffd17579: field results element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(i.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InlineQueryResults) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inlineQueryResults#ffd17579 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inlineQueryResults"); err != nil {
				return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: %w", err)
			}
		case "inline_query_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field inline_query_id: %w", err)
			}
			i.InlineQueryID = value
		case "button":
			if err := i.Button.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field button: %w", err)
			}
		case "results":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONInlineQueryResult(b)
				if err != nil {
					return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field results: %w", err)
				}
				i.Results = append(i.Results, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field results: %w", err)
			}
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode inlineQueryResults#ffd17579: field next_offset: %w", err)
			}
			i.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetInlineQueryID returns value of InlineQueryID field.
func (i *InlineQueryResults) GetInlineQueryID() (value int64) {
	if i == nil {
		return
	}
	return i.InlineQueryID
}

// GetButton returns value of Button field.
func (i *InlineQueryResults) GetButton() (value InlineQueryResultsButton) {
	if i == nil {
		return
	}
	return i.Button
}

// GetResults returns value of Results field.
func (i *InlineQueryResults) GetResults() (value []InlineQueryResultClass) {
	if i == nil {
		return
	}
	return i.Results
}

// GetNextOffset returns value of NextOffset field.
func (i *InlineQueryResults) GetNextOffset() (value string) {
	if i == nil {
		return
	}
	return i.NextOffset
}
