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

// SetBusinessOpeningHoursRequest represents TL type `setBusinessOpeningHours#e470a472`.
type SetBusinessOpeningHoursRequest struct {
	// The new opening hours of the business; pass null to remove the opening hours; up to 28
	// time intervals can be specified
	OpeningHours BusinessOpeningHours
}

// SetBusinessOpeningHoursRequestTypeID is TL type id of SetBusinessOpeningHoursRequest.
const SetBusinessOpeningHoursRequestTypeID = 0xe470a472

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

func (s *SetBusinessOpeningHoursRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.OpeningHours.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetBusinessOpeningHoursRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setBusinessOpeningHours",
		ID:   SetBusinessOpeningHoursRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OpeningHours",
			SchemaName: "opening_hours",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetBusinessOpeningHoursRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessOpeningHours#e470a472 as nil")
	}
	b.PutID(SetBusinessOpeningHoursRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetBusinessOpeningHoursRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessOpeningHours#e470a472 as nil")
	}
	if err := s.OpeningHours.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setBusinessOpeningHours#e470a472: field opening_hours: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetBusinessOpeningHoursRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setBusinessOpeningHours#e470a472 to nil")
	}
	if err := b.ConsumeID(SetBusinessOpeningHoursRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setBusinessOpeningHours#e470a472: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetBusinessOpeningHoursRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setBusinessOpeningHours#e470a472 to nil")
	}
	{
		if err := s.OpeningHours.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setBusinessOpeningHours#e470a472: field opening_hours: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetBusinessOpeningHoursRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setBusinessOpeningHours#e470a472 as nil")
	}
	b.ObjStart()
	b.PutID("setBusinessOpeningHours")
	b.Comma()
	b.FieldStart("opening_hours")
	if err := s.OpeningHours.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setBusinessOpeningHours#e470a472: field opening_hours: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetBusinessOpeningHoursRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setBusinessOpeningHours#e470a472 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setBusinessOpeningHours"); err != nil {
				return fmt.Errorf("unable to decode setBusinessOpeningHours#e470a472: %w", err)
			}
		case "opening_hours":
			if err := s.OpeningHours.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setBusinessOpeningHours#e470a472: field opening_hours: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetOpeningHours returns value of OpeningHours field.
func (s *SetBusinessOpeningHoursRequest) GetOpeningHours() (value BusinessOpeningHours) {
	if s == nil {
		return
	}
	return s.OpeningHours
}

// SetBusinessOpeningHours invokes method setBusinessOpeningHours#e470a472 returning error if any.
func (c *Client) SetBusinessOpeningHours(ctx context.Context, openinghours BusinessOpeningHours) error {
	var ok Ok

	request := &SetBusinessOpeningHoursRequest{
		OpeningHours: openinghours,
	}
	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
