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

// BusinessWorkHours represents TL type `businessWorkHours#8c92b098`.
// Specifies a set of Telegram Business opening hours¹.
//
// Links:
//  1. https://core.telegram.org/api/business#opening-hours
//
// See https://core.telegram.org/constructor/businessWorkHours for reference.
type BusinessWorkHours struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Ignored if set while invoking account.updateBusinessWorkHours¹, only returned by the
	// server in userFull².business_work_hours, indicating whether the business is currently
	// open according to the current time and the values in weekly_open and timezone.
	//
	// Links:
	//  1) https://core.telegram.org/method/account.updateBusinessWorkHours
	//  2) https://core.telegram.org/constructor/userFull
	OpenNow bool
	// An ID of one of the timezones returned by help.getTimezonesList¹.    The timezone ID
	// is contained timezone².id, a human-readable, localized name of the timezone is
	// available in timezone³.name and the timezone⁴.utc_offset field contains the UTC
	// offset in seconds, which may be displayed in hh:mm format by the client together with
	// the human-readable name (i.e. $name UTC -01:00).
	//
	// Links:
	//  1) https://core.telegram.org/method/help.getTimezonesList
	//  2) https://core.telegram.org/constructor/timezone
	//  3) https://core.telegram.org/constructor/timezone
	//  4) https://core.telegram.org/constructor/timezone
	TimezoneID string
	// A list of time intervals (max 28) represented by businessWeeklyOpen »¹, indicating
	// the opening hours of their business.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/businessWeeklyOpen
	WeeklyOpen []BusinessWeeklyOpen
}

// BusinessWorkHoursTypeID is TL type id of BusinessWorkHours.
const BusinessWorkHoursTypeID = 0x8c92b098

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

func (b *BusinessWorkHours) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Flags.Zero()) {
		return false
	}
	if !(b.OpenNow == false) {
		return false
	}
	if !(b.TimezoneID == "") {
		return false
	}
	if !(b.WeeklyOpen == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BusinessWorkHours from given interface.
func (b *BusinessWorkHours) FillFrom(from interface {
	GetOpenNow() (value bool)
	GetTimezoneID() (value string)
	GetWeeklyOpen() (value []BusinessWeeklyOpen)
}) {
	b.OpenNow = from.GetOpenNow()
	b.TimezoneID = from.GetTimezoneID()
	b.WeeklyOpen = from.GetWeeklyOpen()
}

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

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

// TypeInfo returns info about TL type.
func (b *BusinessWorkHours) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessWorkHours",
		ID:   BusinessWorkHoursTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OpenNow",
			SchemaName: "open_now",
			Null:       !b.Flags.Has(0),
		},
		{
			Name:       "TimezoneID",
			SchemaName: "timezone_id",
		},
		{
			Name:       "WeeklyOpen",
			SchemaName: "weekly_open",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *BusinessWorkHours) SetFlags() {
	if !(b.OpenNow == false) {
		b.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (b *BusinessWorkHours) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessWorkHours#8c92b098 as nil")
	}
	buf.PutID(BusinessWorkHoursTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessWorkHours) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessWorkHours#8c92b098 as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessWorkHours#8c92b098: field flags: %w", err)
	}
	buf.PutString(b.TimezoneID)
	buf.PutVectorHeader(len(b.WeeklyOpen))
	for idx, v := range b.WeeklyOpen {
		if err := v.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode businessWorkHours#8c92b098: field weekly_open element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BusinessWorkHours) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessWorkHours#8c92b098 to nil")
	}
	if err := buf.ConsumeID(BusinessWorkHoursTypeID); err != nil {
		return fmt.Errorf("unable to decode businessWorkHours#8c92b098: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BusinessWorkHours) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessWorkHours#8c92b098 to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessWorkHours#8c92b098: field flags: %w", err)
		}
	}
	b.OpenNow = b.Flags.Has(0)
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode businessWorkHours#8c92b098: field timezone_id: %w", err)
		}
		b.TimezoneID = value
	}
	{
		headerLen, err := buf.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode businessWorkHours#8c92b098: field weekly_open: %w", err)
		}

		if headerLen > 0 {
			b.WeeklyOpen = make([]BusinessWeeklyOpen, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BusinessWeeklyOpen
			if err := value.Decode(buf); err != nil {
				return fmt.Errorf("unable to decode businessWorkHours#8c92b098: field weekly_open: %w", err)
			}
			b.WeeklyOpen = append(b.WeeklyOpen, value)
		}
	}
	return nil
}

// SetOpenNow sets value of OpenNow conditional field.
func (b *BusinessWorkHours) SetOpenNow(value bool) {
	if value {
		b.Flags.Set(0)
		b.OpenNow = true
	} else {
		b.Flags.Unset(0)
		b.OpenNow = false
	}
}

// GetOpenNow returns value of OpenNow conditional field.
func (b *BusinessWorkHours) GetOpenNow() (value bool) {
	if b == nil {
		return
	}
	return b.Flags.Has(0)
}

// GetTimezoneID returns value of TimezoneID field.
func (b *BusinessWorkHours) GetTimezoneID() (value string) {
	if b == nil {
		return
	}
	return b.TimezoneID
}

// GetWeeklyOpen returns value of WeeklyOpen field.
func (b *BusinessWorkHours) GetWeeklyOpen() (value []BusinessWeeklyOpen) {
	if b == nil {
		return
	}
	return b.WeeklyOpen
}
