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

// BusinessWeeklyOpen represents TL type `businessWeeklyOpen#120b1ab9`.
// A time interval, indicating the opening hours of a business.
// Note that opening hours specified by the user must be appropriately validated and
// transformed before uploading them to the server, as specified here »¹.
//
// Links:
//  1. https://core.telegram.org/api/business#opening-hours
//
// See https://core.telegram.org/constructor/businessWeeklyOpen for reference.
type BusinessWeeklyOpen struct {
	// Start minute in minutes of the week, 0 to 7*24*60 inclusively.
	StartMinute int
	// End minute in minutes of the week, 1 to 8*24*60 inclusively (8 and not 7 because this
	// allows to specify intervals that, for example, start on Sunday 21:00 and end on Monday
	// 04:00 (6*24*60+21*60 to 7*24*60+4*60) without passing an invalid end_minute <
	// start_minute). See here »¹ for more info.
	//
	// Links:
	//  1) https://core.telegram.org/api/business#opening-hours
	EndMinute int
}

// BusinessWeeklyOpenTypeID is TL type id of BusinessWeeklyOpen.
const BusinessWeeklyOpenTypeID = 0x120b1ab9

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

func (b *BusinessWeeklyOpen) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.StartMinute == 0) {
		return false
	}
	if !(b.EndMinute == 0) {
		return false
	}

	return true
}

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

// FillFrom fills BusinessWeeklyOpen from given interface.
func (b *BusinessWeeklyOpen) FillFrom(from interface {
	GetStartMinute() (value int)
	GetEndMinute() (value int)
}) {
	b.StartMinute = from.GetStartMinute()
	b.EndMinute = from.GetEndMinute()
}

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

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

// TypeInfo returns info about TL type.
func (b *BusinessWeeklyOpen) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessWeeklyOpen",
		ID:   BusinessWeeklyOpenTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StartMinute",
			SchemaName: "start_minute",
		},
		{
			Name:       "EndMinute",
			SchemaName: "end_minute",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BusinessWeeklyOpen) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessWeeklyOpen#120b1ab9 as nil")
	}
	buf.PutID(BusinessWeeklyOpenTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessWeeklyOpen) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessWeeklyOpen#120b1ab9 as nil")
	}
	buf.PutInt(b.StartMinute)
	buf.PutInt(b.EndMinute)
	return nil
}

// Decode implements bin.Decoder.
func (b *BusinessWeeklyOpen) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessWeeklyOpen#120b1ab9 to nil")
	}
	if err := buf.ConsumeID(BusinessWeeklyOpenTypeID); err != nil {
		return fmt.Errorf("unable to decode businessWeeklyOpen#120b1ab9: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BusinessWeeklyOpen) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessWeeklyOpen#120b1ab9 to nil")
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode businessWeeklyOpen#120b1ab9: field start_minute: %w", err)
		}
		b.StartMinute = value
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode businessWeeklyOpen#120b1ab9: field end_minute: %w", err)
		}
		b.EndMinute = value
	}
	return nil
}

// GetStartMinute returns value of StartMinute field.
func (b *BusinessWeeklyOpen) GetStartMinute() (value int) {
	if b == nil {
		return
	}
	return b.StartMinute
}

// GetEndMinute returns value of EndMinute field.
func (b *BusinessWeeklyOpen) GetEndMinute() (value int) {
	if b == nil {
		return
	}
	return b.EndMinute
}
