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

// GetStoryRequest represents TL type `getStory#882168f2`.
type GetStoryRequest struct {
	// Identifier of the chat that posted the story
	StoryPosterChatID int64
	// Story identifier
	StoryID int32
	// Pass true to get only locally available information without sending network requests
	OnlyLocal bool
}

// GetStoryRequestTypeID is TL type id of GetStoryRequest.
const GetStoryRequestTypeID = 0x882168f2

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

func (g *GetStoryRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.StoryPosterChatID == 0) {
		return false
	}
	if !(g.StoryID == 0) {
		return false
	}
	if !(g.OnlyLocal == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetStoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getStory",
		ID:   GetStoryRequestTypeID,
	}
	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:       "OnlyLocal",
			SchemaName: "only_local",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetStoryRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStory#882168f2 as nil")
	}
	b.PutID(GetStoryRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetStoryRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getStory#882168f2 as nil")
	}
	b.PutInt53(g.StoryPosterChatID)
	b.PutInt32(g.StoryID)
	b.PutBool(g.OnlyLocal)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetStoryRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStory#882168f2 to nil")
	}
	if err := b.ConsumeID(GetStoryRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getStory#882168f2: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetStoryRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getStory#882168f2 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getStory#882168f2: field story_poster_chat_id: %w", err)
		}
		g.StoryPosterChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getStory#882168f2: field story_id: %w", err)
		}
		g.StoryID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getStory#882168f2: field only_local: %w", err)
		}
		g.OnlyLocal = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetStoryRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getStory#882168f2 as nil")
	}
	b.ObjStart()
	b.PutID("getStory")
	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("only_local")
	b.PutBool(g.OnlyLocal)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetStoryRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getStory#882168f2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getStory"); err != nil {
				return fmt.Errorf("unable to decode getStory#882168f2: %w", err)
			}
		case "story_poster_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getStory#882168f2: 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 getStory#882168f2: field story_id: %w", err)
			}
			g.StoryID = value
		case "only_local":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getStory#882168f2: field only_local: %w", err)
			}
			g.OnlyLocal = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// GetOnlyLocal returns value of OnlyLocal field.
func (g *GetStoryRequest) GetOnlyLocal() (value bool) {
	if g == nil {
		return
	}
	return g.OnlyLocal
}

// GetStory invokes method getStory#882168f2 returning error if any.
func (c *Client) GetStory(ctx context.Context, request *GetStoryRequest) (*Story, error) {
	var result Story

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