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

// BusinessOpeningHoursInterval represents TL type `businessOpeningHoursInterval#bdf05654`.
type BusinessOpeningHoursInterval struct {
	// The minute's sequence number in a week, starting on Monday, marking the start of the
	// time interval during which the business is open; 0-7*24*60
	StartMinute int32
	// The minute's sequence number in a week, starting on Monday, marking the end of the
	// time interval during which the business is open; 1-8*24*60
	EndMinute int32
}

// BusinessOpeningHoursIntervalTypeID is TL type id of BusinessOpeningHoursInterval.
const BusinessOpeningHoursIntervalTypeID = 0xbdf05654

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

func (b *BusinessOpeningHoursInterval) 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 *BusinessOpeningHoursInterval) String() string {
	if b == nil {
		return "BusinessOpeningHoursInterval(nil)"
	}
	type Alias BusinessOpeningHoursInterval
	return fmt.Sprintf("BusinessOpeningHoursInterval%+v", Alias(*b))
}

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

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

// TypeInfo returns info about TL type.
func (b *BusinessOpeningHoursInterval) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessOpeningHoursInterval",
		ID:   BusinessOpeningHoursIntervalTypeID,
	}
	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 *BusinessOpeningHoursInterval) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessOpeningHoursInterval#bdf05654 as nil")
	}
	buf.PutID(BusinessOpeningHoursIntervalTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessOpeningHoursInterval) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessOpeningHoursInterval#bdf05654 as nil")
	}
	buf.PutInt32(b.StartMinute)
	buf.PutInt32(b.EndMinute)
	return nil
}

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BusinessOpeningHoursInterval) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode businessOpeningHoursInterval#bdf05654 as nil")
	}
	buf.ObjStart()
	buf.PutID("businessOpeningHoursInterval")
	buf.Comma()
	buf.FieldStart("start_minute")
	buf.PutInt32(b.StartMinute)
	buf.Comma()
	buf.FieldStart("end_minute")
	buf.PutInt32(b.EndMinute)
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BusinessOpeningHoursInterval) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode businessOpeningHoursInterval#bdf05654 to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("businessOpeningHoursInterval"); err != nil {
				return fmt.Errorf("unable to decode businessOpeningHoursInterval#bdf05654: %w", err)
			}
		case "start_minute":
			value, err := buf.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode businessOpeningHoursInterval#bdf05654: field start_minute: %w", err)
			}
			b.StartMinute = value
		case "end_minute":
			value, err := buf.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode businessOpeningHoursInterval#bdf05654: field end_minute: %w", err)
			}
			b.EndMinute = value
		default:
			return buf.Skip()
		}
		return nil
	})
}

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

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