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

// TemporaryPasswordState represents TL type `temporaryPasswordState#3804c7e2`.
type TemporaryPasswordState struct {
	// True, if a temporary password is available
	HasPassword bool
	// Time left before the temporary password expires, in seconds
	ValidFor int32
}

// TemporaryPasswordStateTypeID is TL type id of TemporaryPasswordState.
const TemporaryPasswordStateTypeID = 0x3804c7e2

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

func (t *TemporaryPasswordState) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.HasPassword == false) {
		return false
	}
	if !(t.ValidFor == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TemporaryPasswordState) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "temporaryPasswordState",
		ID:   TemporaryPasswordStateTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "HasPassword",
			SchemaName: "has_password",
		},
		{
			Name:       "ValidFor",
			SchemaName: "valid_for",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TemporaryPasswordState) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode temporaryPasswordState#3804c7e2 as nil")
	}
	b.PutID(TemporaryPasswordStateTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TemporaryPasswordState) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode temporaryPasswordState#3804c7e2 as nil")
	}
	b.PutBool(t.HasPassword)
	b.PutInt32(t.ValidFor)
	return nil
}

// Decode implements bin.Decoder.
func (t *TemporaryPasswordState) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode temporaryPasswordState#3804c7e2 to nil")
	}
	if err := b.ConsumeID(TemporaryPasswordStateTypeID); err != nil {
		return fmt.Errorf("unable to decode temporaryPasswordState#3804c7e2: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *TemporaryPasswordState) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode temporaryPasswordState#3804c7e2 to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode temporaryPasswordState#3804c7e2: field has_password: %w", err)
		}
		t.HasPassword = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode temporaryPasswordState#3804c7e2: field valid_for: %w", err)
		}
		t.ValidFor = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TemporaryPasswordState) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode temporaryPasswordState#3804c7e2 as nil")
	}
	b.ObjStart()
	b.PutID("temporaryPasswordState")
	b.Comma()
	b.FieldStart("has_password")
	b.PutBool(t.HasPassword)
	b.Comma()
	b.FieldStart("valid_for")
	b.PutInt32(t.ValidFor)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *TemporaryPasswordState) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode temporaryPasswordState#3804c7e2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("temporaryPasswordState"); err != nil {
				return fmt.Errorf("unable to decode temporaryPasswordState#3804c7e2: %w", err)
			}
		case "has_password":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode temporaryPasswordState#3804c7e2: field has_password: %w", err)
			}
			t.HasPassword = value
		case "valid_for":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode temporaryPasswordState#3804c7e2: field valid_for: %w", err)
			}
			t.ValidFor = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetHasPassword returns value of HasPassword field.
func (t *TemporaryPasswordState) GetHasPassword() (value bool) {
	if t == nil {
		return
	}
	return t.HasPassword
}

// GetValidFor returns value of ValidFor field.
func (t *TemporaryPasswordState) GetValidFor() (value int32) {
	if t == nil {
		return
	}
	return t.ValidFor
}
