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

// PreparedInlineMessage represents TL type `preparedInlineMessage#942e7cc2`.
type PreparedInlineMessage struct {
	// Unique identifier of the inline query to pass to sendInlineQueryResultMessage
	InlineQueryID int64
	// Resulted inline message of the query
	Result InlineQueryResultClass
	// Types of the chats to which the message can be sent
	ChatTypes TargetChatTypes
}

// PreparedInlineMessageTypeID is TL type id of PreparedInlineMessage.
const PreparedInlineMessageTypeID = 0x942e7cc2

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

func (p *PreparedInlineMessage) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.InlineQueryID == 0) {
		return false
	}
	if !(p.Result == nil) {
		return false
	}
	if !(p.ChatTypes.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PreparedInlineMessage) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "preparedInlineMessage",
		ID:   PreparedInlineMessageTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "InlineQueryID",
			SchemaName: "inline_query_id",
		},
		{
			Name:       "Result",
			SchemaName: "result",
		},
		{
			Name:       "ChatTypes",
			SchemaName: "chat_types",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PreparedInlineMessage) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode preparedInlineMessage#942e7cc2 as nil")
	}
	b.PutID(PreparedInlineMessageTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PreparedInlineMessage) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode preparedInlineMessage#942e7cc2 as nil")
	}
	b.PutLong(p.InlineQueryID)
	if p.Result == nil {
		return fmt.Errorf("unable to encode preparedInlineMessage#942e7cc2: field result is nil")
	}
	if err := p.Result.Encode(b); err != nil {
		return fmt.Errorf("unable to encode preparedInlineMessage#942e7cc2: field result: %w", err)
	}
	if err := p.ChatTypes.Encode(b); err != nil {
		return fmt.Errorf("unable to encode preparedInlineMessage#942e7cc2: field chat_types: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PreparedInlineMessage) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode preparedInlineMessage#942e7cc2 to nil")
	}
	if err := b.ConsumeID(PreparedInlineMessageTypeID); err != nil {
		return fmt.Errorf("unable to decode preparedInlineMessage#942e7cc2: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PreparedInlineMessage) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode preparedInlineMessage#942e7cc2 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode preparedInlineMessage#942e7cc2: field inline_query_id: %w", err)
		}
		p.InlineQueryID = value
	}
	{
		value, err := DecodeInlineQueryResult(b)
		if err != nil {
			return fmt.Errorf("unable to decode preparedInlineMessage#942e7cc2: field result: %w", err)
		}
		p.Result = value
	}
	{
		if err := p.ChatTypes.Decode(b); err != nil {
			return fmt.Errorf("unable to decode preparedInlineMessage#942e7cc2: field chat_types: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PreparedInlineMessage) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode preparedInlineMessage#942e7cc2 as nil")
	}
	b.ObjStart()
	b.PutID("preparedInlineMessage")
	b.Comma()
	b.FieldStart("inline_query_id")
	b.PutLong(p.InlineQueryID)
	b.Comma()
	b.FieldStart("result")
	if p.Result == nil {
		return fmt.Errorf("unable to encode preparedInlineMessage#942e7cc2: field result is nil")
	}
	if err := p.Result.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode preparedInlineMessage#942e7cc2: field result: %w", err)
	}
	b.Comma()
	b.FieldStart("chat_types")
	if err := p.ChatTypes.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode preparedInlineMessage#942e7cc2: field chat_types: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PreparedInlineMessage) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode preparedInlineMessage#942e7cc2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("preparedInlineMessage"); err != nil {
				return fmt.Errorf("unable to decode preparedInlineMessage#942e7cc2: %w", err)
			}
		case "inline_query_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode preparedInlineMessage#942e7cc2: field inline_query_id: %w", err)
			}
			p.InlineQueryID = value
		case "result":
			value, err := DecodeTDLibJSONInlineQueryResult(b)
			if err != nil {
				return fmt.Errorf("unable to decode preparedInlineMessage#942e7cc2: field result: %w", err)
			}
			p.Result = value
		case "chat_types":
			if err := p.ChatTypes.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode preparedInlineMessage#942e7cc2: field chat_types: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetResult returns value of Result field.
func (p *PreparedInlineMessage) GetResult() (value InlineQueryResultClass) {
	if p == nil {
		return
	}
	return p.Result
}

// GetChatTypes returns value of ChatTypes field.
func (p *PreparedInlineMessage) GetChatTypes() (value TargetChatTypes) {
	if p == nil {
		return
	}
	return p.ChatTypes
}
