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

// GetStoryInteractionsRequest represents TL type `getStoryInteractions#1cd1400d`.
type GetStoryInteractionsRequest struct {
	// Story identifier
	StoryID int32
	// Query to search for in names, usernames and titles; may be empty to get all relevant
	// interactions
	Query string
	// Pass true to get only interactions by contacts; pass false to get all relevant
	// interactions
	OnlyContacts bool
	// 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
	// Pass true to get interactions with reaction first; pass false to get interactions
	// sorted just by interaction date. Ignored if prefer_forwards == true
	PreferWithReaction 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
}

// GetStoryInteractionsRequestTypeID is TL type id of GetStoryInteractionsRequest.
const GetStoryInteractionsRequestTypeID = 0x1cd1400d

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

func (g *GetStoryInteractionsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.StoryID == 0) {
		return false
	}
	if !(g.Query == "") {
		return false
	}
	if !(g.OnlyContacts == false) {
		return false
	}
	if !(g.PreferForwards == false) {
		return false
	}
	if !(g.PreferWithReaction == false) {
		return false
	}
	if !(g.Offset == "") {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetStoryInteractionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getStoryInteractions",
		ID:   GetStoryInteractionsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryID",
			SchemaName: "story_id",
		},
		{
			Name:       "Query",
			SchemaName: "query",
		},
		{
			Name:       "OnlyContacts",
			SchemaName: "only_contacts",
		},
		{
			Name:       "PreferForwards",
			SchemaName: "prefer_forwards",
		},
		{
			Name:       "PreferWithReaction",
			SchemaName: "prefer_with_reaction",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetStoryInteractionsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStoryInteractions#1cd1400d as nil")
	}
	b.PutID(GetStoryInteractionsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetStoryInteractionsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStoryInteractions#1cd1400d as nil")
	}
	b.PutInt32(g.StoryID)
	b.PutString(g.Query)
	b.PutBool(g.OnlyContacts)
	b.PutBool(g.PreferForwards)
	b.PutBool(g.PreferWithReaction)
	b.PutString(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetStoryInteractionsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStoryInteractions#1cd1400d to nil")
	}
	if err := b.ConsumeID(GetStoryInteractionsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetStoryInteractionsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStoryInteractions#1cd1400d to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field story_id: %w", err)
		}
		g.StoryID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field query: %w", err)
		}
		g.Query = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field only_contacts: %w", err)
		}
		g.OnlyContacts = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field prefer_forwards: %w", err)
		}
		g.PreferForwards = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field prefer_with_reaction: %w", err)
		}
		g.PreferWithReaction = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetStoryInteractionsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getStoryInteractions#1cd1400d as nil")
	}
	b.ObjStart()
	b.PutID("getStoryInteractions")
	b.Comma()
	b.FieldStart("story_id")
	b.PutInt32(g.StoryID)
	b.Comma()
	b.FieldStart("query")
	b.PutString(g.Query)
	b.Comma()
	b.FieldStart("only_contacts")
	b.PutBool(g.OnlyContacts)
	b.Comma()
	b.FieldStart("prefer_forwards")
	b.PutBool(g.PreferForwards)
	b.Comma()
	b.FieldStart("prefer_with_reaction")
	b.PutBool(g.PreferWithReaction)
	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 *GetStoryInteractionsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getStoryInteractions#1cd1400d to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getStoryInteractions"); err != nil {
				return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: %w", err)
			}
		case "story_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field story_id: %w", err)
			}
			g.StoryID = value
		case "query":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field query: %w", err)
			}
			g.Query = value
		case "only_contacts":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field only_contacts: %w", err)
			}
			g.OnlyContacts = value
		case "prefer_forwards":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field prefer_forwards: %w", err)
			}
			g.PreferForwards = value
		case "prefer_with_reaction":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field prefer_with_reaction: %w", err)
			}
			g.PreferWithReaction = value
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryInteractions#1cd1400d: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetQuery returns value of Query field.
func (g *GetStoryInteractionsRequest) GetQuery() (value string) {
	if g == nil {
		return
	}
	return g.Query
}

// GetOnlyContacts returns value of OnlyContacts field.
func (g *GetStoryInteractionsRequest) GetOnlyContacts() (value bool) {
	if g == nil {
		return
	}
	return g.OnlyContacts
}

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

// GetPreferWithReaction returns value of PreferWithReaction field.
func (g *GetStoryInteractionsRequest) GetPreferWithReaction() (value bool) {
	if g == nil {
		return
	}
	return g.PreferWithReaction
}

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

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

// GetStoryInteractions invokes method getStoryInteractions#1cd1400d returning error if any.
func (c *Client) GetStoryInteractions(ctx context.Context, request *GetStoryInteractionsRequest) (*StoryInteractions, error) {
	var result StoryInteractions

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