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

// PublicForwardMessage represents TL type `publicForwardMessage#317b3d2`.
type PublicForwardMessage struct {
	// Information about the message
	Message Message
}

// PublicForwardMessageTypeID is TL type id of PublicForwardMessage.
const PublicForwardMessageTypeID = 0x317b3d2

// construct implements constructor of PublicForwardClass.
func (p PublicForwardMessage) construct() PublicForwardClass { return &p }

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

	_ PublicForwardClass = &PublicForwardMessage{}
)

func (p *PublicForwardMessage) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Message.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PublicForwardMessage) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "publicForwardMessage",
		ID:   PublicForwardMessageTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Message",
			SchemaName: "message",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PublicForwardMessage) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode publicForwardMessage#317b3d2 as nil")
	}
	b.PutID(PublicForwardMessageTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PublicForwardMessage) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode publicForwardMessage#317b3d2 as nil")
	}
	if err := p.Message.Encode(b); err != nil {
		return fmt.Errorf("unable to encode publicForwardMessage#317b3d2: field message: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PublicForwardMessage) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode publicForwardMessage#317b3d2 to nil")
	}
	if err := b.ConsumeID(PublicForwardMessageTypeID); err != nil {
		return fmt.Errorf("unable to decode publicForwardMessage#317b3d2: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PublicForwardMessage) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode publicForwardMessage#317b3d2 to nil")
	}
	{
		if err := p.Message.Decode(b); err != nil {
			return fmt.Errorf("unable to decode publicForwardMessage#317b3d2: field message: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PublicForwardMessage) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode publicForwardMessage#317b3d2 as nil")
	}
	b.ObjStart()
	b.PutID("publicForwardMessage")
	b.Comma()
	b.FieldStart("message")
	if err := p.Message.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode publicForwardMessage#317b3d2: field message: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PublicForwardMessage) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode publicForwardMessage#317b3d2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("publicForwardMessage"); err != nil {
				return fmt.Errorf("unable to decode publicForwardMessage#317b3d2: %w", err)
			}
		case "message":
			if err := p.Message.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode publicForwardMessage#317b3d2: field message: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMessage returns value of Message field.
func (p *PublicForwardMessage) GetMessage() (value Message) {
	if p == nil {
		return
	}
	return p.Message
}

// PublicForwardStory represents TL type `publicForwardStory#7fdf26af`.
type PublicForwardStory struct {
	// Information about the story
	Story Story
}

// PublicForwardStoryTypeID is TL type id of PublicForwardStory.
const PublicForwardStoryTypeID = 0x7fdf26af

// construct implements constructor of PublicForwardClass.
func (p PublicForwardStory) construct() PublicForwardClass { return &p }

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

	_ PublicForwardClass = &PublicForwardStory{}
)

func (p *PublicForwardStory) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Story.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PublicForwardStory) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "publicForwardStory",
		ID:   PublicForwardStoryTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Story",
			SchemaName: "story",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PublicForwardStory) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode publicForwardStory#7fdf26af as nil")
	}
	b.PutID(PublicForwardStoryTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PublicForwardStory) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode publicForwardStory#7fdf26af as nil")
	}
	if err := p.Story.Encode(b); err != nil {
		return fmt.Errorf("unable to encode publicForwardStory#7fdf26af: field story: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PublicForwardStory) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode publicForwardStory#7fdf26af to nil")
	}
	if err := b.ConsumeID(PublicForwardStoryTypeID); err != nil {
		return fmt.Errorf("unable to decode publicForwardStory#7fdf26af: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PublicForwardStory) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode publicForwardStory#7fdf26af to nil")
	}
	{
		if err := p.Story.Decode(b); err != nil {
			return fmt.Errorf("unable to decode publicForwardStory#7fdf26af: field story: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PublicForwardStory) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode publicForwardStory#7fdf26af as nil")
	}
	b.ObjStart()
	b.PutID("publicForwardStory")
	b.Comma()
	b.FieldStart("story")
	if err := p.Story.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode publicForwardStory#7fdf26af: field story: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PublicForwardStory) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode publicForwardStory#7fdf26af to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("publicForwardStory"); err != nil {
				return fmt.Errorf("unable to decode publicForwardStory#7fdf26af: %w", err)
			}
		case "story":
			if err := p.Story.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode publicForwardStory#7fdf26af: field story: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStory returns value of Story field.
func (p *PublicForwardStory) GetStory() (value Story) {
	if p == nil {
		return
	}
	return p.Story
}

// PublicForwardClassName is schema name of PublicForwardClass.
const PublicForwardClassName = "PublicForward"

// PublicForwardClass represents PublicForward generic type.
//
// Example:
//
//	g, err := tdapi.DecodePublicForward(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.PublicForwardMessage: // publicForwardMessage#317b3d2
//	case *tdapi.PublicForwardStory: // publicForwardStory#7fdf26af
//	default: panic(v)
//	}
type PublicForwardClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() PublicForwardClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodePublicForward implements binary de-serialization for PublicForwardClass.
func DecodePublicForward(buf *bin.Buffer) (PublicForwardClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case PublicForwardMessageTypeID:
		// Decoding publicForwardMessage#317b3d2.
		v := PublicForwardMessage{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PublicForwardClass: %w", err)
		}
		return &v, nil
	case PublicForwardStoryTypeID:
		// Decoding publicForwardStory#7fdf26af.
		v := PublicForwardStory{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PublicForwardClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode PublicForwardClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONPublicForward implements binary de-serialization for PublicForwardClass.
func DecodeTDLibJSONPublicForward(buf tdjson.Decoder) (PublicForwardClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "publicForwardMessage":
		// Decoding publicForwardMessage#317b3d2.
		v := PublicForwardMessage{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PublicForwardClass: %w", err)
		}
		return &v, nil
	case "publicForwardStory":
		// Decoding publicForwardStory#7fdf26af.
		v := PublicForwardStory{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode PublicForwardClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode PublicForwardClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// PublicForward boxes the PublicForwardClass providing a helper.
type PublicForwardBox struct {
	PublicForward PublicForwardClass
}

// Decode implements bin.Decoder for PublicForwardBox.
func (b *PublicForwardBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode PublicForwardBox to nil")
	}
	v, err := DecodePublicForward(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.PublicForward = v
	return nil
}

// Encode implements bin.Encode for PublicForwardBox.
func (b *PublicForwardBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.PublicForward == nil {
		return fmt.Errorf("unable to encode PublicForwardClass as nil")
	}
	return b.PublicForward.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for PublicForwardBox.
func (b *PublicForwardBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode PublicForwardBox to nil")
	}
	v, err := DecodeTDLibJSONPublicForward(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.PublicForward = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for PublicForwardBox.
func (b *PublicForwardBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.PublicForward == nil {
		return fmt.Errorf("unable to encode PublicForwardClass as nil")
	}
	return b.PublicForward.EncodeTDLibJSON(buf)
}
