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

// InputPaidMedia represents TL type `inputPaidMedia#19f0fc79`.
type InputPaidMedia struct {
	// Type of the media
	Type InputPaidMediaTypeClass
	// Photo or video to be sent
	Media InputFileClass
	// Media thumbnail; pass null to skip thumbnail uploading
	Thumbnail InputThumbnail
	// File identifiers of the stickers added to the media, if applicable
	AddedStickerFileIDs []int32
	// Media width
	Width int32
	// Media height
	Height int32
}

// InputPaidMediaTypeID is TL type id of InputPaidMedia.
const InputPaidMediaTypeID = 0x19f0fc79

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

func (i *InputPaidMedia) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Type == nil) {
		return false
	}
	if !(i.Media == nil) {
		return false
	}
	if !(i.Thumbnail.Zero()) {
		return false
	}
	if !(i.AddedStickerFileIDs == nil) {
		return false
	}
	if !(i.Width == 0) {
		return false
	}
	if !(i.Height == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InputPaidMedia) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputPaidMedia",
		ID:   InputPaidMediaTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "Media",
			SchemaName: "media",
		},
		{
			Name:       "Thumbnail",
			SchemaName: "thumbnail",
		},
		{
			Name:       "AddedStickerFileIDs",
			SchemaName: "added_sticker_file_ids",
		},
		{
			Name:       "Width",
			SchemaName: "width",
		},
		{
			Name:       "Height",
			SchemaName: "height",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputPaidMedia) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPaidMedia#19f0fc79 as nil")
	}
	b.PutID(InputPaidMediaTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputPaidMedia) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPaidMedia#19f0fc79 as nil")
	}
	if i.Type == nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field type is nil")
	}
	if err := i.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field type: %w", err)
	}
	if i.Media == nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field media is nil")
	}
	if err := i.Media.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field media: %w", err)
	}
	if err := i.Thumbnail.Encode(b); err != nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field thumbnail: %w", err)
	}
	b.PutInt(len(i.AddedStickerFileIDs))
	for _, v := range i.AddedStickerFileIDs {
		b.PutInt32(v)
	}
	b.PutInt32(i.Width)
	b.PutInt32(i.Height)
	return nil
}

// Decode implements bin.Decoder.
func (i *InputPaidMedia) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPaidMedia#19f0fc79 to nil")
	}
	if err := b.ConsumeID(InputPaidMediaTypeID); err != nil {
		return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *InputPaidMedia) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPaidMedia#19f0fc79 to nil")
	}
	{
		value, err := DecodeInputPaidMediaType(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field type: %w", err)
		}
		i.Type = value
	}
	{
		value, err := DecodeInputFile(b)
		if err != nil {
			return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field media: %w", err)
		}
		i.Media = value
	}
	{
		if err := i.Thumbnail.Decode(b); err != nil {
			return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field thumbnail: %w", err)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field added_sticker_file_ids: %w", err)
		}

		if headerLen > 0 {
			i.AddedStickerFileIDs = make([]int32, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field added_sticker_file_ids: %w", err)
			}
			i.AddedStickerFileIDs = append(i.AddedStickerFileIDs, value)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field width: %w", err)
		}
		i.Width = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field height: %w", err)
		}
		i.Height = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InputPaidMedia) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inputPaidMedia#19f0fc79 as nil")
	}
	b.ObjStart()
	b.PutID("inputPaidMedia")
	b.Comma()
	b.FieldStart("type")
	if i.Type == nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field type is nil")
	}
	if err := i.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("media")
	if i.Media == nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field media is nil")
	}
	if err := i.Media.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field media: %w", err)
	}
	b.Comma()
	b.FieldStart("thumbnail")
	if err := i.Thumbnail.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode inputPaidMedia#19f0fc79: field thumbnail: %w", err)
	}
	b.Comma()
	b.FieldStart("added_sticker_file_ids")
	b.ArrStart()
	for _, v := range i.AddedStickerFileIDs {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("width")
	b.PutInt32(i.Width)
	b.Comma()
	b.FieldStart("height")
	b.PutInt32(i.Height)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (i *InputPaidMedia) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inputPaidMedia#19f0fc79 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inputPaidMedia"); err != nil {
				return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: %w", err)
			}
		case "type":
			value, err := DecodeTDLibJSONInputPaidMediaType(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field type: %w", err)
			}
			i.Type = value
		case "media":
			value, err := DecodeTDLibJSONInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field media: %w", err)
			}
			i.Media = value
		case "thumbnail":
			if err := i.Thumbnail.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field thumbnail: %w", err)
			}
		case "added_sticker_file_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field added_sticker_file_ids: %w", err)
				}
				i.AddedStickerFileIDs = append(i.AddedStickerFileIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field added_sticker_file_ids: %w", err)
			}
		case "width":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field width: %w", err)
			}
			i.Width = value
		case "height":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode inputPaidMedia#19f0fc79: field height: %w", err)
			}
			i.Height = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (i *InputPaidMedia) GetType() (value InputPaidMediaTypeClass) {
	if i == nil {
		return
	}
	return i.Type
}

// GetMedia returns value of Media field.
func (i *InputPaidMedia) GetMedia() (value InputFileClass) {
	if i == nil {
		return
	}
	return i.Media
}

// GetThumbnail returns value of Thumbnail field.
func (i *InputPaidMedia) GetThumbnail() (value InputThumbnail) {
	if i == nil {
		return
	}
	return i.Thumbnail
}

// GetAddedStickerFileIDs returns value of AddedStickerFileIDs field.
func (i *InputPaidMedia) GetAddedStickerFileIDs() (value []int32) {
	if i == nil {
		return
	}
	return i.AddedStickerFileIDs
}

// GetWidth returns value of Width field.
func (i *InputPaidMedia) GetWidth() (value int32) {
	if i == nil {
		return
	}
	return i.Width
}

// GetHeight returns value of Height field.
func (i *InputPaidMedia) GetHeight() (value int32) {
	if i == nil {
		return
	}
	return i.Height
}
