// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// HelpTimezonesListNotModified represents TL type `help.timezonesListNotModified#970708cc`.
// The timezone list has not changed.
//
// See https://core.telegram.org/constructor/help.timezonesListNotModified for reference.
type HelpTimezonesListNotModified struct {
}

// HelpTimezonesListNotModifiedTypeID is TL type id of HelpTimezonesListNotModified.
const HelpTimezonesListNotModifiedTypeID = 0x970708cc

// construct implements constructor of HelpTimezonesListClass.
func (t HelpTimezonesListNotModified) construct() HelpTimezonesListClass { return &t }

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

	_ HelpTimezonesListClass = &HelpTimezonesListNotModified{}
)

func (t *HelpTimezonesListNotModified) Zero() bool {
	if t == nil {
		return true
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*HelpTimezonesListNotModified) TypeName() string {
	return "help.timezonesListNotModified"
}

// TypeInfo returns info about TL type.
func (t *HelpTimezonesListNotModified) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "help.timezonesListNotModified",
		ID:   HelpTimezonesListNotModifiedTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (t *HelpTimezonesListNotModified) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode help.timezonesListNotModified#970708cc as nil")
	}
	b.PutID(HelpTimezonesListNotModifiedTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *HelpTimezonesListNotModified) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode help.timezonesListNotModified#970708cc as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *HelpTimezonesListNotModified) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode help.timezonesListNotModified#970708cc to nil")
	}
	if err := b.ConsumeID(HelpTimezonesListNotModifiedTypeID); err != nil {
		return fmt.Errorf("unable to decode help.timezonesListNotModified#970708cc: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *HelpTimezonesListNotModified) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode help.timezonesListNotModified#970708cc to nil")
	}
	return nil
}

// HelpTimezonesList represents TL type `help.timezonesList#7b74ed71`.
// Timezone information that may be used elsewhere in the API, such as to set Telegram
// Business opening hours »¹.
//
// Links:
//  1. https://core.telegram.org/api/business#opening-hours
//
// See https://core.telegram.org/constructor/help.timezonesList for reference.
type HelpTimezonesList struct {
	// Timezones
	Timezones []Timezone
	// Hash used for caching, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int
}

// HelpTimezonesListTypeID is TL type id of HelpTimezonesList.
const HelpTimezonesListTypeID = 0x7b74ed71

// construct implements constructor of HelpTimezonesListClass.
func (t HelpTimezonesList) construct() HelpTimezonesListClass { return &t }

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

	_ HelpTimezonesListClass = &HelpTimezonesList{}
)

func (t *HelpTimezonesList) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Timezones == nil) {
		return false
	}
	if !(t.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills HelpTimezonesList from given interface.
func (t *HelpTimezonesList) FillFrom(from interface {
	GetTimezones() (value []Timezone)
	GetHash() (value int)
}) {
	t.Timezones = from.GetTimezones()
	t.Hash = from.GetHash()
}

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

// TypeName returns name of type in TL schema.
func (*HelpTimezonesList) TypeName() string {
	return "help.timezonesList"
}

// TypeInfo returns info about TL type.
func (t *HelpTimezonesList) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "help.timezonesList",
		ID:   HelpTimezonesListTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Timezones",
			SchemaName: "timezones",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *HelpTimezonesList) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode help.timezonesList#7b74ed71 as nil")
	}
	b.PutID(HelpTimezonesListTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *HelpTimezonesList) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode help.timezonesList#7b74ed71 as nil")
	}
	b.PutVectorHeader(len(t.Timezones))
	for idx, v := range t.Timezones {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode help.timezonesList#7b74ed71: field timezones element with index %d: %w", idx, err)
		}
	}
	b.PutInt(t.Hash)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (t *HelpTimezonesList) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode help.timezonesList#7b74ed71 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode help.timezonesList#7b74ed71: field timezones: %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.Decode(b); err != nil {
				return fmt.Errorf("unable to decode help.timezonesList#7b74ed71: field timezones: %w", err)
			}
			t.Timezones = append(t.Timezones, value)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode help.timezonesList#7b74ed71: field hash: %w", err)
		}
		t.Hash = value
	}
	return nil
}

// GetTimezones returns value of Timezones field.
func (t *HelpTimezonesList) GetTimezones() (value []Timezone) {
	if t == nil {
		return
	}
	return t.Timezones
}

// GetHash returns value of Hash field.
func (t *HelpTimezonesList) GetHash() (value int) {
	if t == nil {
		return
	}
	return t.Hash
}

// HelpTimezonesListClassName is schema name of HelpTimezonesListClass.
const HelpTimezonesListClassName = "help.TimezonesList"

// HelpTimezonesListClass represents help.TimezonesList generic type.
//
// See https://core.telegram.org/type/help.TimezonesList for reference.
//
// Example:
//
//	g, err := tg.DecodeHelpTimezonesList(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.HelpTimezonesListNotModified: // help.timezonesListNotModified#970708cc
//	case *tg.HelpTimezonesList: // help.timezonesList#7b74ed71
//	default: panic(v)
//	}
type HelpTimezonesListClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() HelpTimezonesListClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// AsModified tries to map HelpTimezonesListClass to HelpTimezonesList.
	AsModified() (*HelpTimezonesList, bool)
}

// AsModified tries to map HelpTimezonesListNotModified to HelpTimezonesList.
func (t *HelpTimezonesListNotModified) AsModified() (*HelpTimezonesList, bool) {
	return nil, false
}

// AsModified tries to map HelpTimezonesList to HelpTimezonesList.
func (t *HelpTimezonesList) AsModified() (*HelpTimezonesList, bool) {
	return t, true
}

// DecodeHelpTimezonesList implements binary de-serialization for HelpTimezonesListClass.
func DecodeHelpTimezonesList(buf *bin.Buffer) (HelpTimezonesListClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case HelpTimezonesListNotModifiedTypeID:
		// Decoding help.timezonesListNotModified#970708cc.
		v := HelpTimezonesListNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode HelpTimezonesListClass: %w", err)
		}
		return &v, nil
	case HelpTimezonesListTypeID:
		// Decoding help.timezonesList#7b74ed71.
		v := HelpTimezonesList{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode HelpTimezonesListClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode HelpTimezonesListClass: %w", bin.NewUnexpectedID(id))
	}
}

// HelpTimezonesList boxes the HelpTimezonesListClass providing a helper.
type HelpTimezonesListBox struct {
	TimezonesList HelpTimezonesListClass
}

// Decode implements bin.Decoder for HelpTimezonesListBox.
func (b *HelpTimezonesListBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode HelpTimezonesListBox to nil")
	}
	v, err := DecodeHelpTimezonesList(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.TimezonesList = v
	return nil
}

// Encode implements bin.Encode for HelpTimezonesListBox.
func (b *HelpTimezonesListBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.TimezonesList == nil {
		return fmt.Errorf("unable to encode HelpTimezonesListClass as nil")
	}
	return b.TimezonesList.Encode(buf)
}
