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

// AccountUpdateBusinessLocationRequest represents TL type `account.updateBusinessLocation#9e6b131a`.
// Businesses »¹ may advertise their location using this method, see here »² for more
// info.
// To remove business location information invoke the method without setting any of the
// parameters.
//
// Links:
//  1. https://core.telegram.org/api/business#location
//  2. https://core.telegram.org/api/business#location
//
// See https://core.telegram.org/method/account.updateBusinessLocation for reference.
type AccountUpdateBusinessLocationRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Optional, contains a set of geographical coordinates.
	//
	// Use SetGeoPoint and GetGeoPoint helpers.
	GeoPoint InputGeoPointClass
	// Mandatory when setting/updating the location, contains a textual description of the
	// address (max 96 UTF-8 chars).
	//
	// Use SetAddress and GetAddress helpers.
	Address string
}

// AccountUpdateBusinessLocationRequestTypeID is TL type id of AccountUpdateBusinessLocationRequest.
const AccountUpdateBusinessLocationRequestTypeID = 0x9e6b131a

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

func (u *AccountUpdateBusinessLocationRequest) Zero() bool {
	if u == nil {
		return true
	}
	if !(u.Flags.Zero()) {
		return false
	}
	if !(u.GeoPoint == nil) {
		return false
	}
	if !(u.Address == "") {
		return false
	}

	return true
}

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

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

	if val, ok := from.GetAddress(); ok {
		u.Address = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*AccountUpdateBusinessLocationRequest) TypeName() string {
	return "account.updateBusinessLocation"
}

// TypeInfo returns info about TL type.
func (u *AccountUpdateBusinessLocationRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.updateBusinessLocation",
		ID:   AccountUpdateBusinessLocationRequestTypeID,
	}
	if u == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GeoPoint",
			SchemaName: "geo_point",
			Null:       !u.Flags.Has(1),
		},
		{
			Name:       "Address",
			SchemaName: "address",
			Null:       !u.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (u *AccountUpdateBusinessLocationRequest) SetFlags() {
	if !(u.GeoPoint == nil) {
		u.Flags.Set(1)
	}
	if !(u.Address == "") {
		u.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (u *AccountUpdateBusinessLocationRequest) Encode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateBusinessLocation#9e6b131a as nil")
	}
	b.PutID(AccountUpdateBusinessLocationRequestTypeID)
	return u.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (u *AccountUpdateBusinessLocationRequest) EncodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't encode account.updateBusinessLocation#9e6b131a as nil")
	}
	u.SetFlags()
	if err := u.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.updateBusinessLocation#9e6b131a: field flags: %w", err)
	}
	if u.Flags.Has(1) {
		if u.GeoPoint == nil {
			return fmt.Errorf("unable to encode account.updateBusinessLocation#9e6b131a: field geo_point is nil")
		}
		if err := u.GeoPoint.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.updateBusinessLocation#9e6b131a: field geo_point: %w", err)
		}
	}
	if u.Flags.Has(0) {
		b.PutString(u.Address)
	}
	return nil
}

// Decode implements bin.Decoder.
func (u *AccountUpdateBusinessLocationRequest) Decode(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode account.updateBusinessLocation#9e6b131a to nil")
	}
	if err := b.ConsumeID(AccountUpdateBusinessLocationRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode account.updateBusinessLocation#9e6b131a: %w", err)
	}
	return u.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (u *AccountUpdateBusinessLocationRequest) DecodeBare(b *bin.Buffer) error {
	if u == nil {
		return fmt.Errorf("can't decode account.updateBusinessLocation#9e6b131a to nil")
	}
	{
		if err := u.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.updateBusinessLocation#9e6b131a: field flags: %w", err)
		}
	}
	if u.Flags.Has(1) {
		value, err := DecodeInputGeoPoint(b)
		if err != nil {
			return fmt.Errorf("unable to decode account.updateBusinessLocation#9e6b131a: field geo_point: %w", err)
		}
		u.GeoPoint = value
	}
	if u.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode account.updateBusinessLocation#9e6b131a: field address: %w", err)
		}
		u.Address = value
	}
	return nil
}

// SetGeoPoint sets value of GeoPoint conditional field.
func (u *AccountUpdateBusinessLocationRequest) SetGeoPoint(value InputGeoPointClass) {
	u.Flags.Set(1)
	u.GeoPoint = value
}

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

// SetAddress sets value of Address conditional field.
func (u *AccountUpdateBusinessLocationRequest) SetAddress(value string) {
	u.Flags.Set(0)
	u.Address = value
}

// GetAddress returns value of Address conditional field and
// boolean which is true if field was set.
func (u *AccountUpdateBusinessLocationRequest) GetAddress() (value string, ok bool) {
	if u == nil {
		return
	}
	if !u.Flags.Has(0) {
		return value, false
	}
	return u.Address, true
}

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

// AccountUpdateBusinessLocation invokes method account.updateBusinessLocation#9e6b131a returning error if any.
// Businesses »¹ may advertise their location using this method, see here »² for more
// info.
// To remove business location information invoke the method without setting any of the
// parameters.
//
// Links:
//  1. https://core.telegram.org/api/business#location
//  2. https://core.telegram.org/api/business#location
//
// See https://core.telegram.org/method/account.updateBusinessLocation for reference.
func (c *Client) AccountUpdateBusinessLocation(ctx context.Context, request *AccountUpdateBusinessLocationRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
