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

// OpenStoryRequest represents TL type `openStory#d678d9c3`.
type OpenStoryRequest struct {
	// The identifier of the chat that posted the opened story
	StoryPosterChatID int64
	// The identifier of the story
	StoryID int32
}

// OpenStoryRequestTypeID is TL type id of OpenStoryRequest.
const OpenStoryRequestTypeID = 0xd678d9c3

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

func (o *OpenStoryRequest) Zero() bool {
	if o == nil {
		return true
	}
	if !(o.StoryPosterChatID == 0) {
		return false
	}
	if !(o.StoryID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (o *OpenStoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "openStory",
		ID:   OpenStoryRequestTypeID,
	}
	if o == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryPosterChatID",
			SchemaName: "story_poster_chat_id",
		},
		{
			Name:       "StoryID",
			SchemaName: "story_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (o *OpenStoryRequest) Encode(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode openStory#d678d9c3 as nil")
	}
	b.PutID(OpenStoryRequestTypeID)
	return o.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (o *OpenStoryRequest) EncodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode openStory#d678d9c3 as nil")
	}
	b.PutInt53(o.StoryPosterChatID)
	b.PutInt32(o.StoryID)
	return nil
}

// Decode implements bin.Decoder.
func (o *OpenStoryRequest) Decode(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't decode openStory#d678d9c3 to nil")
	}
	if err := b.ConsumeID(OpenStoryRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode openStory#d678d9c3: %w", err)
	}
	return o.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (o *OpenStoryRequest) DecodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't decode openStory#d678d9c3 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode openStory#d678d9c3: field story_poster_chat_id: %w", err)
		}
		o.StoryPosterChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode openStory#d678d9c3: field story_id: %w", err)
		}
		o.StoryID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (o *OpenStoryRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if o == nil {
		return fmt.Errorf("can't encode openStory#d678d9c3 as nil")
	}
	b.ObjStart()
	b.PutID("openStory")
	b.Comma()
	b.FieldStart("story_poster_chat_id")
	b.PutInt53(o.StoryPosterChatID)
	b.Comma()
	b.FieldStart("story_id")
	b.PutInt32(o.StoryID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (o *OpenStoryRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if o == nil {
		return fmt.Errorf("can't decode openStory#d678d9c3 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("openStory"); err != nil {
				return fmt.Errorf("unable to decode openStory#d678d9c3: %w", err)
			}
		case "story_poster_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode openStory#d678d9c3: field story_poster_chat_id: %w", err)
			}
			o.StoryPosterChatID = value
		case "story_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode openStory#d678d9c3: field story_id: %w", err)
			}
			o.StoryID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// OpenStory invokes method openStory#d678d9c3 returning error if any.
func (c *Client) OpenStory(ctx context.Context, request *OpenStoryRequest) error {
	var ok Ok

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