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

// StoryFullID represents TL type `storyFullId#2da781a3`.
type StoryFullID struct {
	// Identifier of the chat that posted the story
	PosterChatID int64
	// Unique story identifier among stories of the chat
	StoryID int32
}

// StoryFullIDTypeID is TL type id of StoryFullID.
const StoryFullIDTypeID = 0x2da781a3

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

func (s *StoryFullID) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.PosterChatID == 0) {
		return false
	}
	if !(s.StoryID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StoryFullID) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "storyFullId",
		ID:   StoryFullIDTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "PosterChatID",
			SchemaName: "poster_chat_id",
		},
		{
			Name:       "StoryID",
			SchemaName: "story_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StoryFullID) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyFullId#2da781a3 as nil")
	}
	b.PutID(StoryFullIDTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoryFullID) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode storyFullId#2da781a3 as nil")
	}
	b.PutInt53(s.PosterChatID)
	b.PutInt32(s.StoryID)
	return nil
}

// Decode implements bin.Decoder.
func (s *StoryFullID) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyFullId#2da781a3 to nil")
	}
	if err := b.ConsumeID(StoryFullIDTypeID); err != nil {
		return fmt.Errorf("unable to decode storyFullId#2da781a3: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StoryFullID) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode storyFullId#2da781a3 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode storyFullId#2da781a3: field poster_chat_id: %w", err)
		}
		s.PosterChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode storyFullId#2da781a3: field story_id: %w", err)
		}
		s.StoryID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StoryFullID) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode storyFullId#2da781a3 as nil")
	}
	b.ObjStart()
	b.PutID("storyFullId")
	b.Comma()
	b.FieldStart("poster_chat_id")
	b.PutInt53(s.PosterChatID)
	b.Comma()
	b.FieldStart("story_id")
	b.PutInt32(s.StoryID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StoryFullID) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode storyFullId#2da781a3 to nil")
	}

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

// GetPosterChatID returns value of PosterChatID field.
func (s *StoryFullID) GetPosterChatID() (value int64) {
	if s == nil {
		return
	}
	return s.PosterChatID
}

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