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

// TimeZones represents TL type `timeZones#7c4881b9`.
type TimeZones struct {
	// A list of time zones
	TimeZones []TimeZone
}

// TimeZonesTypeID is TL type id of TimeZones.
const TimeZonesTypeID = 0x7c4881b9

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

func (t *TimeZones) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.TimeZones == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TimeZones) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "timeZones",
		ID:   TimeZonesTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TimeZones",
			SchemaName: "time_zones",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TimeZones) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode timeZones#7c4881b9 as nil")
	}
	b.PutID(TimeZonesTypeID)
	return t.EncodeBare(b)
}

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

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

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

		if headerLen > 0 {
			t.TimeZones = make([]TimeZone, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value TimeZone
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare timeZones#7c4881b9: field time_zones: %w", err)
			}
			t.TimeZones = append(t.TimeZones, value)
		}
	}
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("timeZones"); err != nil {
				return fmt.Errorf("unable to decode timeZones#7c4881b9: %w", err)
			}
		case "time_zones":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value TimeZone
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode timeZones#7c4881b9: field time_zones: %w", err)
				}
				t.TimeZones = append(t.TimeZones, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode timeZones#7c4881b9: field time_zones: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTimeZones returns value of TimeZones field.
func (t *TimeZones) GetTimeZones() (value []TimeZone) {
	if t == nil {
		return
	}
	return t.TimeZones
}
