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

// GetStoryPublicForwardsRequest represents TL type `getStoryPublicForwards#6be82f32`.
type GetStoryPublicForwardsRequest struct {
	// The identifier of the poster of the story
	StoryPosterChatID int64
	// The identifier of the story
	StoryID int32
	// 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 messages and stories to be returned; must be positive and can't
	// be greater than 100. For optimal performance, the number of returned objects is chosen
	// by TDLib and can be smaller than the specified limit
	Limit int32
}

// GetStoryPublicForwardsRequestTypeID is TL type id of GetStoryPublicForwardsRequest.
const GetStoryPublicForwardsRequestTypeID = 0x6be82f32

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetStoryPublicForwardsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getStoryPublicForwards",
		ID:   GetStoryPublicForwardsRequestTypeID,
	}
	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:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetStoryPublicForwardsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStoryPublicForwards#6be82f32 as nil")
	}
	b.PutID(GetStoryPublicForwardsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetStoryPublicForwardsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStoryPublicForwards#6be82f32 as nil")
	}
	b.PutInt53(g.StoryPosterChatID)
	b.PutInt32(g.StoryID)
	b.PutString(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetStoryPublicForwardsRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStoryPublicForwards#6be82f32 to nil")
	}
	if err := b.ConsumeID(GetStoryPublicForwardsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetStoryPublicForwardsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStoryPublicForwards#6be82f32 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: field story_poster_chat_id: %w", err)
		}
		g.StoryPosterChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: field story_id: %w", err)
		}
		g.StoryID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetStoryPublicForwardsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getStoryPublicForwards#6be82f32 as nil")
	}
	b.ObjStart()
	b.PutID("getStoryPublicForwards")
	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("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 *GetStoryPublicForwardsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getStoryPublicForwards#6be82f32 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getStoryPublicForwards"); err != nil {
				return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: %w", err)
			}
		case "story_poster_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: 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 getStoryPublicForwards#6be82f32: field story_id: %w", err)
			}
			g.StoryID = value
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getStoryPublicForwards#6be82f32: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

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

// GetStoryPublicForwards invokes method getStoryPublicForwards#6be82f32 returning error if any.
func (c *Client) GetStoryPublicForwards(ctx context.Context, request *GetStoryPublicForwardsRequest) (*PublicForwards, error) {
	var result PublicForwards

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