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

// GetChatStoryInteractionsRequest represents TL type `getChatStoryInteractions#1521ee74`.
type GetChatStoryInteractionsRequest struct {
	// The identifier of the poster of the story
	StoryPosterChatID int64
	// Story identifier
	StoryID int32
	// Pass the default heart reaction or a suggested reaction type to receive only
	// interactions with the specified reaction type; pass null to receive all interactions;
	// reactionTypePaid isn't supported
	ReactionType ReactionTypeClass
	// Pass true to get forwards and reposts first, then reactions, then other views; pass
	// false to get interactions sorted just by interaction date
	PreferForwards bool
	// Offset of the first entry to return as received from the previous request; use empty
	// string to get the first chunk of results
	Offset string
	// The maximum number of story interactions to return
	Limit int32
}

// GetChatStoryInteractionsRequestTypeID is TL type id of GetChatStoryInteractionsRequest.
const GetChatStoryInteractionsRequestTypeID = 0x1521ee74

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

func (g *GetChatStoryInteractionsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.StoryPosterChatID == 0) {
		return false
	}
	if !(g.StoryID == 0) {
		return false
	}
	if !(g.ReactionType == nil) {
		return false
	}
	if !(g.PreferForwards == false) {
		return false
	}
	if !(g.Offset == "") {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatStoryInteractionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChatStoryInteractions",
		ID:   GetChatStoryInteractionsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryPosterChatID",
			SchemaName: "story_poster_chat_id",
		},
		{
			Name:       "StoryID",
			SchemaName: "story_id",
		},
		{
			Name:       "ReactionType",
			SchemaName: "reaction_type",
		},
		{
			Name:       "PreferForwards",
			SchemaName: "prefer_forwards",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetChatStoryInteractionsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatStoryInteractions#1521ee74 as nil")
	}
	b.PutID(GetChatStoryInteractionsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetChatStoryInteractionsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatStoryInteractions#1521ee74 as nil")
	}
	b.PutInt53(g.StoryPosterChatID)
	b.PutInt32(g.StoryID)
	if g.ReactionType == nil {
		return fmt.Errorf("unable to encode getChatStoryInteractions#1521ee74: field reaction_type is nil")
	}
	if err := g.ReactionType.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getChatStoryInteractions#1521ee74: field reaction_type: %w", err)
	}
	b.PutBool(g.PreferForwards)
	b.PutString(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetChatStoryInteractionsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatStoryInteractions#1521ee74 to nil")
	}
	if err := b.ConsumeID(GetChatStoryInteractionsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetChatStoryInteractionsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatStoryInteractions#1521ee74 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field story_poster_chat_id: %w", err)
		}
		g.StoryPosterChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field story_id: %w", err)
		}
		g.StoryID = value
	}
	{
		value, err := DecodeReactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field reaction_type: %w", err)
		}
		g.ReactionType = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field prefer_forwards: %w", err)
		}
		g.PreferForwards = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetChatStoryInteractionsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatStoryInteractions#1521ee74 as nil")
	}
	b.ObjStart()
	b.PutID("getChatStoryInteractions")
	b.Comma()
	b.FieldStart("story_poster_chat_id")
	b.PutInt53(g.StoryPosterChatID)
	b.Comma()
	b.FieldStart("story_id")
	b.PutInt32(g.StoryID)
	b.Comma()
	b.FieldStart("reaction_type")
	if g.ReactionType == nil {
		return fmt.Errorf("unable to encode getChatStoryInteractions#1521ee74: field reaction_type is nil")
	}
	if err := g.ReactionType.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getChatStoryInteractions#1521ee74: field reaction_type: %w", err)
	}
	b.Comma()
	b.FieldStart("prefer_forwards")
	b.PutBool(g.PreferForwards)
	b.Comma()
	b.FieldStart("offset")
	b.PutString(g.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetChatStoryInteractionsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatStoryInteractions#1521ee74 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getChatStoryInteractions"); err != nil {
				return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: %w", err)
			}
		case "story_poster_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field story_poster_chat_id: %w", err)
			}
			g.StoryPosterChatID = value
		case "story_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field story_id: %w", err)
			}
			g.StoryID = value
		case "reaction_type":
			value, err := DecodeTDLibJSONReactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field reaction_type: %w", err)
			}
			g.ReactionType = value
		case "prefer_forwards":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field prefer_forwards: %w", err)
			}
			g.PreferForwards = value
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getChatStoryInteractions#1521ee74: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStoryPosterChatID returns value of StoryPosterChatID field.
func (g *GetChatStoryInteractionsRequest) GetStoryPosterChatID() (value int64) {
	if g == nil {
		return
	}
	return g.StoryPosterChatID
}

// GetStoryID returns value of StoryID field.
func (g *GetChatStoryInteractionsRequest) GetStoryID() (value int32) {
	if g == nil {
		return
	}
	return g.StoryID
}

// GetReactionType returns value of ReactionType field.
func (g *GetChatStoryInteractionsRequest) GetReactionType() (value ReactionTypeClass) {
	if g == nil {
		return
	}
	return g.ReactionType
}

// GetPreferForwards returns value of PreferForwards field.
func (g *GetChatStoryInteractionsRequest) GetPreferForwards() (value bool) {
	if g == nil {
		return
	}
	return g.PreferForwards
}

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

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

// GetChatStoryInteractions invokes method getChatStoryInteractions#1521ee74 returning error if any.
func (c *Client) GetChatStoryInteractions(ctx context.Context, request *GetChatStoryInteractionsRequest) (*StoryInteractions, error) {
	var result StoryInteractions

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