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

// PhoneNumberInfo represents TL type `phoneNumberInfo#d2c398a1`.
type PhoneNumberInfo struct {
	// Information about the country to which the phone number belongs; may be null
	Country CountryInfo
	// The part of the phone number denoting country calling code or its part
	CountryCallingCode string
	// The phone number without country calling code formatted accordingly to local rules.
	// Expected digits are returned as '-', but even more digits might be entered by the user
	FormattedPhoneNumber string
	// True, if the phone number was bought at https://fragment.com and isn't tied to a SIM
	// card. Information about the phone number can be received using getCollectibleItemInfo
	IsAnonymous bool
}

// PhoneNumberInfoTypeID is TL type id of PhoneNumberInfo.
const PhoneNumberInfoTypeID = 0xd2c398a1

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

func (p *PhoneNumberInfo) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Country.Zero()) {
		return false
	}
	if !(p.CountryCallingCode == "") {
		return false
	}
	if !(p.FormattedPhoneNumber == "") {
		return false
	}
	if !(p.IsAnonymous == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *PhoneNumberInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "phoneNumberInfo",
		ID:   PhoneNumberInfoTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Country",
			SchemaName: "country",
		},
		{
			Name:       "CountryCallingCode",
			SchemaName: "country_calling_code",
		},
		{
			Name:       "FormattedPhoneNumber",
			SchemaName: "formatted_phone_number",
		},
		{
			Name:       "IsAnonymous",
			SchemaName: "is_anonymous",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PhoneNumberInfo) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberInfo#d2c398a1 as nil")
	}
	b.PutID(PhoneNumberInfoTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PhoneNumberInfo) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberInfo#d2c398a1 as nil")
	}
	if err := p.Country.Encode(b); err != nil {
		return fmt.Errorf("unable to encode phoneNumberInfo#d2c398a1: field country: %w", err)
	}
	b.PutString(p.CountryCallingCode)
	b.PutString(p.FormattedPhoneNumber)
	b.PutBool(p.IsAnonymous)
	return nil
}

// Decode implements bin.Decoder.
func (p *PhoneNumberInfo) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberInfo#d2c398a1 to nil")
	}
	if err := b.ConsumeID(PhoneNumberInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PhoneNumberInfo) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberInfo#d2c398a1 to nil")
	}
	{
		if err := p.Country.Decode(b); err != nil {
			return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: field country: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: field country_calling_code: %w", err)
		}
		p.CountryCallingCode = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: field formatted_phone_number: %w", err)
		}
		p.FormattedPhoneNumber = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: field is_anonymous: %w", err)
		}
		p.IsAnonymous = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PhoneNumberInfo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode phoneNumberInfo#d2c398a1 as nil")
	}
	b.ObjStart()
	b.PutID("phoneNumberInfo")
	b.Comma()
	b.FieldStart("country")
	if err := p.Country.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode phoneNumberInfo#d2c398a1: field country: %w", err)
	}
	b.Comma()
	b.FieldStart("country_calling_code")
	b.PutString(p.CountryCallingCode)
	b.Comma()
	b.FieldStart("formatted_phone_number")
	b.PutString(p.FormattedPhoneNumber)
	b.Comma()
	b.FieldStart("is_anonymous")
	b.PutBool(p.IsAnonymous)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PhoneNumberInfo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode phoneNumberInfo#d2c398a1 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("phoneNumberInfo"); err != nil {
				return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: %w", err)
			}
		case "country":
			if err := p.Country.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: field country: %w", err)
			}
		case "country_calling_code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: field country_calling_code: %w", err)
			}
			p.CountryCallingCode = value
		case "formatted_phone_number":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: field formatted_phone_number: %w", err)
			}
			p.FormattedPhoneNumber = value
		case "is_anonymous":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode phoneNumberInfo#d2c398a1: field is_anonymous: %w", err)
			}
			p.IsAnonymous = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCountry returns value of Country field.
func (p *PhoneNumberInfo) GetCountry() (value CountryInfo) {
	if p == nil {
		return
	}
	return p.Country
}

// GetCountryCallingCode returns value of CountryCallingCode field.
func (p *PhoneNumberInfo) GetCountryCallingCode() (value string) {
	if p == nil {
		return
	}
	return p.CountryCallingCode
}

// GetFormattedPhoneNumber returns value of FormattedPhoneNumber field.
func (p *PhoneNumberInfo) GetFormattedPhoneNumber() (value string) {
	if p == nil {
		return
	}
	return p.FormattedPhoneNumber
}

// GetIsAnonymous returns value of IsAnonymous field.
func (p *PhoneNumberInfo) GetIsAnonymous() (value bool) {
	if p == nil {
		return
	}
	return p.IsAnonymous
}
