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

// PhotoSize represents TL type `photoSize#18e56d39`.
type PhotoSize struct {
	// Image type (see https://core.telegram.org/constructor/photoSize)
	Type string
	// Information about the image file
	Photo File
	// Image width
	Width int32
	// Image height
	Height int32
	// Sizes of progressive JPEG file prefixes, which can be used to preliminarily show the
	// image; in bytes
	ProgressiveSizes []int32
}

// PhotoSizeTypeID is TL type id of PhotoSize.
const PhotoSizeTypeID = 0x18e56d39

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

func (p *PhotoSize) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Type == "") {
		return false
	}
	if !(p.Photo.Zero()) {
		return false
	}
	if !(p.Width == 0) {
		return false
	}
	if !(p.Height == 0) {
		return false
	}
	if !(p.ProgressiveSizes == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PhotoSize) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "photoSize",
		ID:   PhotoSizeTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "Photo",
			SchemaName: "photo",
		},
		{
			Name:       "Width",
			SchemaName: "width",
		},
		{
			Name:       "Height",
			SchemaName: "height",
		},
		{
			Name:       "ProgressiveSizes",
			SchemaName: "progressive_sizes",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PhotoSize) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode photoSize#18e56d39 as nil")
	}
	b.PutID(PhotoSizeTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PhotoSize) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode photoSize#18e56d39 as nil")
	}
	b.PutString(p.Type)
	if err := p.Photo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode photoSize#18e56d39: field photo: %w", err)
	}
	b.PutInt32(p.Width)
	b.PutInt32(p.Height)
	b.PutInt(len(p.ProgressiveSizes))
	for _, v := range p.ProgressiveSizes {
		b.PutInt32(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PhotoSize) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode photoSize#18e56d39 to nil")
	}
	if err := b.ConsumeID(PhotoSizeTypeID); err != nil {
		return fmt.Errorf("unable to decode photoSize#18e56d39: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PhotoSize) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode photoSize#18e56d39 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode photoSize#18e56d39: field type: %w", err)
		}
		p.Type = value
	}
	{
		if err := p.Photo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode photoSize#18e56d39: field photo: %w", err)
		}
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode photoSize#18e56d39: field width: %w", err)
		}
		p.Width = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode photoSize#18e56d39: field height: %w", err)
		}
		p.Height = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode photoSize#18e56d39: field progressive_sizes: %w", err)
		}

		if headerLen > 0 {
			p.ProgressiveSizes = 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 photoSize#18e56d39: field progressive_sizes: %w", err)
			}
			p.ProgressiveSizes = append(p.ProgressiveSizes, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PhotoSize) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode photoSize#18e56d39 as nil")
	}
	b.ObjStart()
	b.PutID("photoSize")
	b.Comma()
	b.FieldStart("type")
	b.PutString(p.Type)
	b.Comma()
	b.FieldStart("photo")
	if err := p.Photo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode photoSize#18e56d39: field photo: %w", err)
	}
	b.Comma()
	b.FieldStart("width")
	b.PutInt32(p.Width)
	b.Comma()
	b.FieldStart("height")
	b.PutInt32(p.Height)
	b.Comma()
	b.FieldStart("progressive_sizes")
	b.ArrStart()
	for _, v := range p.ProgressiveSizes {
		b.PutInt32(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PhotoSize) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode photoSize#18e56d39 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("photoSize"); err != nil {
				return fmt.Errorf("unable to decode photoSize#18e56d39: %w", err)
			}
		case "type":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode photoSize#18e56d39: field type: %w", err)
			}
			p.Type = value
		case "photo":
			if err := p.Photo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode photoSize#18e56d39: field photo: %w", err)
			}
		case "width":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode photoSize#18e56d39: field width: %w", err)
			}
			p.Width = value
		case "height":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode photoSize#18e56d39: field height: %w", err)
			}
			p.Height = value
		case "progressive_sizes":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int32()
				if err != nil {
					return fmt.Errorf("unable to decode photoSize#18e56d39: field progressive_sizes: %w", err)
				}
				p.ProgressiveSizes = append(p.ProgressiveSizes, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode photoSize#18e56d39: field progressive_sizes: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (p *PhotoSize) GetType() (value string) {
	if p == nil {
		return
	}
	return p.Type
}

// GetPhoto returns value of Photo field.
func (p *PhotoSize) GetPhoto() (value File) {
	if p == nil {
		return
	}
	return p.Photo
}

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

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

// GetProgressiveSizes returns value of ProgressiveSizes field.
func (p *PhotoSize) GetProgressiveSizes() (value []int32) {
	if p == nil {
		return
	}
	return p.ProgressiveSizes
}
