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

// Passkey represents TL type `passkey#98613ebf`.
//
// See https://core.telegram.org/constructor/passkey for reference.
type Passkey struct {
	// Flags field of Passkey.
	Flags bin.Fields
	// ID field of Passkey.
	ID string
	// Name field of Passkey.
	Name string
	// Date field of Passkey.
	Date int
	// SoftwareEmojiID field of Passkey.
	//
	// Use SetSoftwareEmojiID and GetSoftwareEmojiID helpers.
	SoftwareEmojiID int64
	// LastUsageDate field of Passkey.
	//
	// Use SetLastUsageDate and GetLastUsageDate helpers.
	LastUsageDate int
}

// PasskeyTypeID is TL type id of Passkey.
const PasskeyTypeID = 0x98613ebf

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

func (p *Passkey) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Flags.Zero()) {
		return false
	}
	if !(p.ID == "") {
		return false
	}
	if !(p.Name == "") {
		return false
	}
	if !(p.Date == 0) {
		return false
	}
	if !(p.SoftwareEmojiID == 0) {
		return false
	}
	if !(p.LastUsageDate == 0) {
		return false
	}

	return true
}

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

// FillFrom fills Passkey from given interface.
func (p *Passkey) FillFrom(from interface {
	GetID() (value string)
	GetName() (value string)
	GetDate() (value int)
	GetSoftwareEmojiID() (value int64, ok bool)
	GetLastUsageDate() (value int, ok bool)
}) {
	p.ID = from.GetID()
	p.Name = from.GetName()
	p.Date = from.GetDate()
	if val, ok := from.GetSoftwareEmojiID(); ok {
		p.SoftwareEmojiID = val
	}

	if val, ok := from.GetLastUsageDate(); ok {
		p.LastUsageDate = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (p *Passkey) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "passkey",
		ID:   PasskeyTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "SoftwareEmojiID",
			SchemaName: "software_emoji_id",
			Null:       !p.Flags.Has(0),
		},
		{
			Name:       "LastUsageDate",
			SchemaName: "last_usage_date",
			Null:       !p.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (p *Passkey) SetFlags() {
	if !(p.SoftwareEmojiID == 0) {
		p.Flags.Set(0)
	}
	if !(p.LastUsageDate == 0) {
		p.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (p *Passkey) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode passkey#98613ebf as nil")
	}
	b.PutID(PasskeyTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *Passkey) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode passkey#98613ebf as nil")
	}
	p.SetFlags()
	if err := p.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode passkey#98613ebf: field flags: %w", err)
	}
	b.PutString(p.ID)
	b.PutString(p.Name)
	b.PutInt(p.Date)
	if p.Flags.Has(0) {
		b.PutLong(p.SoftwareEmojiID)
	}
	if p.Flags.Has(1) {
		b.PutInt(p.LastUsageDate)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (p *Passkey) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode passkey#98613ebf to nil")
	}
	{
		if err := p.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode passkey#98613ebf: field flags: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode passkey#98613ebf: field id: %w", err)
		}
		p.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode passkey#98613ebf: field name: %w", err)
		}
		p.Name = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode passkey#98613ebf: field date: %w", err)
		}
		p.Date = value
	}
	if p.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode passkey#98613ebf: field software_emoji_id: %w", err)
		}
		p.SoftwareEmojiID = value
	}
	if p.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode passkey#98613ebf: field last_usage_date: %w", err)
		}
		p.LastUsageDate = value
	}
	return nil
}

// GetID returns value of ID field.
func (p *Passkey) GetID() (value string) {
	if p == nil {
		return
	}
	return p.ID
}

// GetName returns value of Name field.
func (p *Passkey) GetName() (value string) {
	if p == nil {
		return
	}
	return p.Name
}

// GetDate returns value of Date field.
func (p *Passkey) GetDate() (value int) {
	if p == nil {
		return
	}
	return p.Date
}

// SetSoftwareEmojiID sets value of SoftwareEmojiID conditional field.
func (p *Passkey) SetSoftwareEmojiID(value int64) {
	p.Flags.Set(0)
	p.SoftwareEmojiID = value
}

// GetSoftwareEmojiID returns value of SoftwareEmojiID conditional field and
// boolean which is true if field was set.
func (p *Passkey) GetSoftwareEmojiID() (value int64, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(0) {
		return value, false
	}
	return p.SoftwareEmojiID, true
}

// SetLastUsageDate sets value of LastUsageDate conditional field.
func (p *Passkey) SetLastUsageDate(value int) {
	p.Flags.Set(1)
	p.LastUsageDate = value
}

// GetLastUsageDate returns value of LastUsageDate conditional field and
// boolean which is true if field was set.
func (p *Passkey) GetLastUsageDate() (value int, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(1) {
		return value, false
	}
	return p.LastUsageDate, true
}
