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

// Outline represents TL type `outline#7d2ec0be`.
type Outline struct {
	// The list of closed vector paths
	Paths []ClosedVectorPath
}

// OutlineTypeID is TL type id of Outline.
const OutlineTypeID = 0x7d2ec0be

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

func (o *Outline) Zero() bool {
	if o == nil {
		return true
	}
	if !(o.Paths == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (o *Outline) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "outline",
		ID:   OutlineTypeID,
	}
	if o == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Paths",
			SchemaName: "paths",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (o *Outline) Encode(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode outline#7d2ec0be as nil")
	}
	b.PutID(OutlineTypeID)
	return o.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (o *Outline) EncodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode outline#7d2ec0be as nil")
	}
	b.PutInt(len(o.Paths))
	for idx, v := range o.Paths {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare outline#7d2ec0be: field paths element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (o *Outline) DecodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't decode outline#7d2ec0be to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode outline#7d2ec0be: field paths: %w", err)
		}

		if headerLen > 0 {
			o.Paths = make([]ClosedVectorPath, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ClosedVectorPath
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare outline#7d2ec0be: field paths: %w", err)
			}
			o.Paths = append(o.Paths, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (o *Outline) EncodeTDLibJSON(b tdjson.Encoder) error {
	if o == nil {
		return fmt.Errorf("can't encode outline#7d2ec0be as nil")
	}
	b.ObjStart()
	b.PutID("outline")
	b.Comma()
	b.FieldStart("paths")
	b.ArrStart()
	for idx, v := range o.Paths {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode outline#7d2ec0be: field paths element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("outline"); err != nil {
				return fmt.Errorf("unable to decode outline#7d2ec0be: %w", err)
			}
		case "paths":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value ClosedVectorPath
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode outline#7d2ec0be: field paths: %w", err)
				}
				o.Paths = append(o.Paths, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode outline#7d2ec0be: field paths: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPaths returns value of Paths field.
func (o *Outline) GetPaths() (value []ClosedVectorPath) {
	if o == nil {
		return
	}
	return o.Paths
}
