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

// BusinessLocation represents TL type `businessLocation#ac5c1af7`.
// Represents the location of a Telegram Business »¹.
//
// Links:
//  1. https://core.telegram.org/api/business#location
//
// See https://core.telegram.org/constructor/businessLocation for reference.
type BusinessLocation struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Geographical coordinates (optional).
	//
	// Use SetGeoPoint and GetGeoPoint helpers.
	GeoPoint GeoPointClass
	// Textual description of the address (mandatory).
	Address string
}

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

// 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.Flags.Zero()) {
		return false
	}
	if !(b.GeoPoint == nil) {
		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))
}

// FillFrom fills BusinessLocation from given interface.
func (b *BusinessLocation) FillFrom(from interface {
	GetGeoPoint() (value GeoPointClass, ok bool)
	GetAddress() (value string)
}) {
	if val, ok := from.GetGeoPoint(); ok {
		b.GeoPoint = val
	}

	b.Address = from.GetAddress()
}

// 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:       "GeoPoint",
			SchemaName: "geo_point",
			Null:       !b.Flags.Has(0),
		},
		{
			Name:       "Address",
			SchemaName: "address",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (b *BusinessLocation) SetFlags() {
	if !(b.GeoPoint == nil) {
		b.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (b *BusinessLocation) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessLocation#ac5c1af7 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#ac5c1af7 as nil")
	}
	b.SetFlags()
	if err := b.Flags.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode businessLocation#ac5c1af7: field flags: %w", err)
	}
	if b.Flags.Has(0) {
		if b.GeoPoint == nil {
			return fmt.Errorf("unable to encode businessLocation#ac5c1af7: field geo_point is nil")
		}
		if err := b.GeoPoint.Encode(buf); err != nil {
			return fmt.Errorf("unable to encode businessLocation#ac5c1af7: field geo_point: %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#ac5c1af7 to nil")
	}
	if err := buf.ConsumeID(BusinessLocationTypeID); err != nil {
		return fmt.Errorf("unable to decode businessLocation#ac5c1af7: %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#ac5c1af7 to nil")
	}
	{
		if err := b.Flags.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode businessLocation#ac5c1af7: field flags: %w", err)
		}
	}
	if b.Flags.Has(0) {
		value, err := DecodeGeoPoint(buf)
		if err != nil {
			return fmt.Errorf("unable to decode businessLocation#ac5c1af7: field geo_point: %w", err)
		}
		b.GeoPoint = value
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode businessLocation#ac5c1af7: field address: %w", err)
		}
		b.Address = value
	}
	return nil
}

// SetGeoPoint sets value of GeoPoint conditional field.
func (b *BusinessLocation) SetGeoPoint(value GeoPointClass) {
	b.Flags.Set(0)
	b.GeoPoint = value
}

// GetGeoPoint returns value of GeoPoint conditional field and
// boolean which is true if field was set.
func (b *BusinessLocation) GetGeoPoint() (value GeoPointClass, ok bool) {
	if b == nil {
		return
	}
	if !b.Flags.Has(0) {
		return value, false
	}
	return b.GeoPoint, true
}

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

// GetGeoPointAsNotEmpty returns mapped value of GeoPoint conditional field and
// boolean which is true if field was set.
func (b *BusinessLocation) GetGeoPointAsNotEmpty() (*GeoPoint, bool) {
	if value, ok := b.GetGeoPoint(); ok {
		return value.AsNotEmpty()
	}
	return nil, false
}
