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

// SavePreparedInlineMessageRequest represents TL type `savePreparedInlineMessage#c71468d9`.
type SavePreparedInlineMessageRequest struct {
	// Identifier of the user
	UserID int64
	// The description of the message
	Result InputInlineQueryResultClass
	// Types of the chats to which the message can be sent
	ChatTypes TargetChatTypes
}

// SavePreparedInlineMessageRequestTypeID is TL type id of SavePreparedInlineMessageRequest.
const SavePreparedInlineMessageRequestTypeID = 0xc71468d9

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

func (s *SavePreparedInlineMessageRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.UserID == 0) {
		return false
	}
	if !(s.Result == nil) {
		return false
	}
	if !(s.ChatTypes.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SavePreparedInlineMessageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "savePreparedInlineMessage",
		ID:   SavePreparedInlineMessageRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "Result",
			SchemaName: "result",
		},
		{
			Name:       "ChatTypes",
			SchemaName: "chat_types",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SavePreparedInlineMessageRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode savePreparedInlineMessage#c71468d9 as nil")
	}
	b.PutID(SavePreparedInlineMessageRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SavePreparedInlineMessageRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode savePreparedInlineMessage#c71468d9 as nil")
	}
	b.PutInt53(s.UserID)
	if s.Result == nil {
		return fmt.Errorf("unable to encode savePreparedInlineMessage#c71468d9: field result is nil")
	}
	if err := s.Result.Encode(b); err != nil {
		return fmt.Errorf("unable to encode savePreparedInlineMessage#c71468d9: field result: %w", err)
	}
	if err := s.ChatTypes.Encode(b); err != nil {
		return fmt.Errorf("unable to encode savePreparedInlineMessage#c71468d9: field chat_types: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SavePreparedInlineMessageRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode savePreparedInlineMessage#c71468d9 to nil")
	}
	if err := b.ConsumeID(SavePreparedInlineMessageRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode savePreparedInlineMessage#c71468d9: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SavePreparedInlineMessageRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode savePreparedInlineMessage#c71468d9 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode savePreparedInlineMessage#c71468d9: field user_id: %w", err)
		}
		s.UserID = value
	}
	{
		value, err := DecodeInputInlineQueryResult(b)
		if err != nil {
			return fmt.Errorf("unable to decode savePreparedInlineMessage#c71468d9: field result: %w", err)
		}
		s.Result = value
	}
	{
		if err := s.ChatTypes.Decode(b); err != nil {
			return fmt.Errorf("unable to decode savePreparedInlineMessage#c71468d9: field chat_types: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SavePreparedInlineMessageRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode savePreparedInlineMessage#c71468d9 as nil")
	}
	b.ObjStart()
	b.PutID("savePreparedInlineMessage")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(s.UserID)
	b.Comma()
	b.FieldStart("result")
	if s.Result == nil {
		return fmt.Errorf("unable to encode savePreparedInlineMessage#c71468d9: field result is nil")
	}
	if err := s.Result.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode savePreparedInlineMessage#c71468d9: field result: %w", err)
	}
	b.Comma()
	b.FieldStart("chat_types")
	if err := s.ChatTypes.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode savePreparedInlineMessage#c71468d9: field chat_types: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SavePreparedInlineMessageRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode savePreparedInlineMessage#c71468d9 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("savePreparedInlineMessage"); err != nil {
				return fmt.Errorf("unable to decode savePreparedInlineMessage#c71468d9: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode savePreparedInlineMessage#c71468d9: field user_id: %w", err)
			}
			s.UserID = value
		case "result":
			value, err := DecodeTDLibJSONInputInlineQueryResult(b)
			if err != nil {
				return fmt.Errorf("unable to decode savePreparedInlineMessage#c71468d9: field result: %w", err)
			}
			s.Result = value
		case "chat_types":
			if err := s.ChatTypes.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode savePreparedInlineMessage#c71468d9: field chat_types: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (s *SavePreparedInlineMessageRequest) GetUserID() (value int64) {
	if s == nil {
		return
	}
	return s.UserID
}

// GetResult returns value of Result field.
func (s *SavePreparedInlineMessageRequest) GetResult() (value InputInlineQueryResultClass) {
	if s == nil {
		return
	}
	return s.Result
}

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

// SavePreparedInlineMessage invokes method savePreparedInlineMessage#c71468d9 returning error if any.
func (c *Client) SavePreparedInlineMessage(ctx context.Context, request *SavePreparedInlineMessageRequest) (*PreparedInlineMessageID, error) {
	var result PreparedInlineMessageID

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