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

// BusinessLocation represents TL type `businessLocation#bf54af5a`.
type BusinessLocation struct {
	// The location; may be null if not specified
	Location Location
	// Location address; 1-96 characters
	Address string
}

// BusinessLocationTypeID is TL type id of BusinessLocation.
const BusinessLocationTypeID = 0xbf54af5a

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

func (b *BusinessLocation) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Location.Zero()) {
		return false
	}
	if !(b.Address == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BusinessLocation) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessLocation",
		ID:   BusinessLocationTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Location",
			SchemaName: "location",
		},
		{
			Name:       "Address",
			SchemaName: "address",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BusinessLocation) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessLocation#bf54af5a as nil")
	}
	buf.PutID(BusinessLocationTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessLocation) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessLocation#bf54af5a as nil")
	}
	if err := b.Location.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessLocation#bf54af5a: field location: %w", err)
	}
	buf.PutString(b.Address)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (b *BusinessLocation) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessLocation#bf54af5a to nil")
	}
	{
		if err := b.Location.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessLocation#bf54af5a: field location: %w", err)
		}
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode businessLocation#bf54af5a: field address: %w", err)
		}
		b.Address = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BusinessLocation) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode businessLocation#bf54af5a as nil")
	}
	buf.ObjStart()
	buf.PutID("businessLocation")
	buf.Comma()
	buf.FieldStart("location")
	if err := b.Location.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode businessLocation#bf54af5a: field location: %w", err)
	}
	buf.Comma()
	buf.FieldStart("address")
	buf.PutString(b.Address)
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

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

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("businessLocation"); err != nil {
				return fmt.Errorf("unable to decode businessLocation#bf54af5a: %w", err)
			}
		case "location":
			if err := b.Location.DecodeTDLibJSON(buf); err != nil {
				return fmt.Errorf("unable to decode businessLocation#bf54af5a: field location: %w", err)
			}
		case "address":
			value, err := buf.String()
			if err != nil {
				return fmt.Errorf("unable to decode businessLocation#bf54af5a: field address: %w", err)
			}
			b.Address = value
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetLocation returns value of Location field.
func (b *BusinessLocation) GetLocation() (value Location) {
	if b == nil {
		return
	}
	return b.Location
}

// GetAddress returns value of Address field.
func (b *BusinessLocation) GetAddress() (value string) {
	if b == nil {
		return
	}
	return b.Address
}
