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

// InputStoryAreas represents TL type `inputStoryAreas#b8ba29fe`.
type InputStoryAreas struct {
	// List of input story areas. Currently, a story can have
	Areas []InputStoryArea
}

// InputStoryAreasTypeID is TL type id of InputStoryAreas.
const InputStoryAreasTypeID = 0xb8ba29fe

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

func (i *InputStoryAreas) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Areas == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (i *InputStoryAreas) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "inputStoryAreas",
		ID:   InputStoryAreasTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Areas",
			SchemaName: "areas",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *InputStoryAreas) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputStoryAreas#b8ba29fe as nil")
	}
	b.PutID(InputStoryAreasTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *InputStoryAreas) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode inputStoryAreas#b8ba29fe as nil")
	}
	b.PutInt(len(i.Areas))
	for idx, v := range i.Areas {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare inputStoryAreas#b8ba29fe: field areas element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (i *InputStoryAreas) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode inputStoryAreas#b8ba29fe to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode inputStoryAreas#b8ba29fe: field areas: %w", err)
		}

		if headerLen > 0 {
			i.Areas = make([]InputStoryArea, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value InputStoryArea
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare inputStoryAreas#b8ba29fe: field areas: %w", err)
			}
			i.Areas = append(i.Areas, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (i *InputStoryAreas) EncodeTDLibJSON(b tdjson.Encoder) error {
	if i == nil {
		return fmt.Errorf("can't encode inputStoryAreas#b8ba29fe as nil")
	}
	b.ObjStart()
	b.PutID("inputStoryAreas")
	b.Comma()
	b.FieldStart("areas")
	b.ArrStart()
	for idx, v := range i.Areas {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode inputStoryAreas#b8ba29fe: field areas 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 (i *InputStoryAreas) DecodeTDLibJSON(b tdjson.Decoder) error {
	if i == nil {
		return fmt.Errorf("can't decode inputStoryAreas#b8ba29fe to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("inputStoryAreas"); err != nil {
				return fmt.Errorf("unable to decode inputStoryAreas#b8ba29fe: %w", err)
			}
		case "areas":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value InputStoryArea
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode inputStoryAreas#b8ba29fe: field areas: %w", err)
				}
				i.Areas = append(i.Areas, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode inputStoryAreas#b8ba29fe: field areas: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetAreas returns value of Areas field.
func (i *InputStoryAreas) GetAreas() (value []InputStoryArea) {
	if i == nil {
		return
	}
	return i.Areas
}
