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

// GeoPointAddress represents TL type `geoPointAddress#de4c5d93`.
// Address optionally associated to a geoPoint¹.
//
// Links:
//  1. https://core.telegram.org/constructor/geoPoint
//
// See https://core.telegram.org/constructor/geoPointAddress for reference.
type GeoPointAddress struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Two-letter ISO 3166-1 alpha-2 country code
	CountryISO2 string
	// State
	//
	// Use SetState and GetState helpers.
	State string
	// City
	//
	// Use SetCity and GetCity helpers.
	City string
	// Street
	//
	// Use SetStreet and GetStreet helpers.
	Street string
}

// GeoPointAddressTypeID is TL type id of GeoPointAddress.
const GeoPointAddressTypeID = 0xde4c5d93

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

func (g *GeoPointAddress) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.CountryISO2 == "") {
		return false
	}
	if !(g.State == "") {
		return false
	}
	if !(g.City == "") {
		return false
	}
	if !(g.Street == "") {
		return false
	}

	return true
}

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

// FillFrom fills GeoPointAddress from given interface.
func (g *GeoPointAddress) FillFrom(from interface {
	GetCountryISO2() (value string)
	GetState() (value string, ok bool)
	GetCity() (value string, ok bool)
	GetStreet() (value string, ok bool)
}) {
	g.CountryISO2 = from.GetCountryISO2()
	if val, ok := from.GetState(); ok {
		g.State = val
	}

	if val, ok := from.GetCity(); ok {
		g.City = val
	}

	if val, ok := from.GetStreet(); ok {
		g.Street = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (g *GeoPointAddress) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "geoPointAddress",
		ID:   GeoPointAddressTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CountryISO2",
			SchemaName: "country_iso2",
		},
		{
			Name:       "State",
			SchemaName: "state",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "City",
			SchemaName: "city",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "Street",
			SchemaName: "street",
			Null:       !g.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *GeoPointAddress) SetFlags() {
	if !(g.State == "") {
		g.Flags.Set(0)
	}
	if !(g.City == "") {
		g.Flags.Set(1)
	}
	if !(g.Street == "") {
		g.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (g *GeoPointAddress) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode geoPointAddress#de4c5d93 as nil")
	}
	b.PutID(GeoPointAddressTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GeoPointAddress) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode geoPointAddress#de4c5d93 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode geoPointAddress#de4c5d93: field flags: %w", err)
	}
	b.PutString(g.CountryISO2)
	if g.Flags.Has(0) {
		b.PutString(g.State)
	}
	if g.Flags.Has(1) {
		b.PutString(g.City)
	}
	if g.Flags.Has(2) {
		b.PutString(g.Street)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GeoPointAddress) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode geoPointAddress#de4c5d93 to nil")
	}
	if err := b.ConsumeID(GeoPointAddressTypeID); err != nil {
		return fmt.Errorf("unable to decode geoPointAddress#de4c5d93: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GeoPointAddress) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode geoPointAddress#de4c5d93 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode geoPointAddress#de4c5d93: field flags: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode geoPointAddress#de4c5d93: field country_iso2: %w", err)
		}
		g.CountryISO2 = value
	}
	if g.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode geoPointAddress#de4c5d93: field state: %w", err)
		}
		g.State = value
	}
	if g.Flags.Has(1) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode geoPointAddress#de4c5d93: field city: %w", err)
		}
		g.City = value
	}
	if g.Flags.Has(2) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode geoPointAddress#de4c5d93: field street: %w", err)
		}
		g.Street = value
	}
	return nil
}

// GetCountryISO2 returns value of CountryISO2 field.
func (g *GeoPointAddress) GetCountryISO2() (value string) {
	if g == nil {
		return
	}
	return g.CountryISO2
}

// SetState sets value of State conditional field.
func (g *GeoPointAddress) SetState(value string) {
	g.Flags.Set(0)
	g.State = value
}

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

// SetCity sets value of City conditional field.
func (g *GeoPointAddress) SetCity(value string) {
	g.Flags.Set(1)
	g.City = value
}

// GetCity returns value of City conditional field and
// boolean which is true if field was set.
func (g *GeoPointAddress) GetCity() (value string, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(1) {
		return value, false
	}
	return g.City, true
}

// SetStreet sets value of Street conditional field.
func (g *GeoPointAddress) SetStreet(value string) {
	g.Flags.Set(2)
	g.Street = value
}

// GetStreet returns value of Street conditional field and
// boolean which is true if field was set.
func (g *GeoPointAddress) GetStreet() (value string, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(2) {
		return value, false
	}
	return g.Street, true
}
